基于级联分类器的人脸检测示例"face_detection.cpp":
#include <opencv2/opencv.hpp>
int main(int argc,char *argv[])
{
// load face classifier
cv::CascadeClassifier faceCascade;
faceCascade.load(argv[3]);
cv::Mat image = cv::imread(argv[1]);
std::vector<cv::Rect> faces;
faceCascade.detectMultiScale(image, faces, 1.1, 3, 0, cv::Size(30, 30));
for (size_t i = 0; i < faces.size(); i++)
{
cv::rectangle(image, faces[i], cv::Scalar(0, 255, 0), 2);
}
cv::imwrite(argv[2], image);
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.16.3)
project(OpenCV_face_detection_demo VERSION 1.0.0 LANGUAGES CXX)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS}) # Not needed for CMake >= 2.8.11
# several variables will be set by OpenCVConfig.cmake
message(STATUS "OpenCV library status:")
message(STATUS " config: ${OpenCV_DIR}")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
message(STATUS " install path: ${OpenCV_INSTALL_PATH}")
add_executable(OpenCV_demo.out ${CMAKE_CURRENT_SOURCE_DIR}/src/face_detection.cpp)
target_link_libraries(OpenCV_demo.out ${OpenCV_LIBS}) # or "OpenCV_LIBRARIES"
编译执行:
mkdir build && cd build
cmake ..
make
./OpenCV_demo.out ./lean.jpg ./lean_face.jpg haarcascade_frontalface_alt.xml