c++
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>
int main(int argc, char** argv) {
cv::Mat image = cv::imread("d:/2.jpg");
// 检查图像是否加载成功
if (image.empty()) {
std::cout << "Could not open or find the image!" << std::endl;
return -1;
}
// 加载预训练的Haar Cascade分类器用于人脸检测
// 你可能需要根据你的OpenCV安装路径修改此路径
// 例如: "haarcascade_frontalface_default.xml"
// 或者 "/usr/local/share/opencv4/haarcascades/haarcascade_frontalface_default.xml"
cv::CascadeClassifier face_cascade;
std::string cascade_path = "haarcascade_frontalface_default.xml"; // 假设它在同一目录或可访问
if (!face_cascade.load(cascade_path)) {
std::cout << "Error loading face cascade XML file. Make sure 'haarcascade_frontalface_default.xml' is in the same directory or provide its full path." << std::endl;
return -1;
}
// 将图像转换为灰度图
cv::Mat gray_image;
cv::cvtColor(image, gray_image, cv::COLOR_BGR2GRAY);
// 均衡直方图以获得更好的对比度
cv::equalizeHist(gray_image, gray_image);
// 检测人脸
std::vector<cv::Rect> faces;
face_cascade.detectMultiScale(gray_image, faces, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));
// 在人脸周围绘制绿色矩形
for (const auto& face : faces) {
cv::rectangle(image, face, cv::Scalar(0, 255, 0), 2); // 绿色 (B, G, R)
}
// 显示结果
cv::imshow("Detected Faces", image);
// 无限期等待按键
cv::waitKey(0);
// 销毁所有OpenCV窗口
cv::destroyAllWindows();
return 0;
}
或者
#include <opencv2/opencv.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
// 加载人脸检测器(Haar级联分类器)
CascadeClassifier face_cascade;
if (!face_cascade.load("haarcascade_frontalface_default.xml")) {
cout << "Error loading face cascade classifier!" << endl;
return -1;
}
// 读取输入图片
Mat image = imread("d:/3.jpg");
if (image.empty()) {
cout << "Could not open or find the image" << endl;
return -1;
}
// 将图像转换为灰度图,因为人脸检测器需要灰度图像
Mat gray_image;
cvtColor(image, gray_image, COLOR_BGR2GRAY);
equalizeHist(gray_image, gray_image);
// 检测人脸
vector<Rect> faces;
face_cascade.detectMultiScale(gray_image, faces, 1.1, 3, 0, Size(30, 30));
// 在检测到的人脸周围绘制矩形
for (size_t i = 0; i < faces.size(); i++) {
rectangle(image, faces[i], Scalar(0, 255, 0), 2);
}
// 显示结果
imshow("Face Detection", image);
waitKey(0);
// 保存结果
if (!imwrite("output.jpg", image)) {
cout << "Error saving the image!" << endl;
return -1;
}
return 0;
}
python
import cv2
# 加载预训练的人脸分类器
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt2.xml')
# 加载图片
image_path = "d:/2.jpg" # 替换为你的图片路径
image = cv2.imread(image_path)
if image is None:
print("无法加载图片,请检查路径!")
exit()
# 转为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 检测人脸
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
# 绘制检测到的人脸
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# 显示结果
cv2.imshow("Detected Faces", image)
cv2.waitKey(0)
cv2.destroyAllWindows()