OpenCV代码不会检测人脸的数量,也不会在脸周围放一个圆圈
opencv code does not detect the number of faces neither puts a circle around the face
我的代码:
#include "opencv2/opencv.hpp"
#include <iostream>
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
using namespace std;
using namespace cv;
int main()
{
VideoCapture cap(0);
CascadeClassifier cascade, netCascade;
double scale = 1;
netCascade.load("/opt/opencv/data/haarcascades/haarcascade_frontalcatface.xml");
cascade.load("/opt/opencv/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml");
//cap.open();
if(!cap.isOpened())
{
cout<<"error opening video file"<<endl;
return -1;
}
while(1)
{
Mat img;
cap>>img;
if(img.empty())
{
break;
}
//Mat img1 = img.clone();
vector<Rect> faces, faces2;
Mat grey, smallImg;
cvtColor(img, grey, COLOR_BGR2GRAY);
double fx = 1 / scale;
resize(grey, smallImg, Size(), fx, fx, INTER_LINEAR);
equalizeHist(smallImg, smallImg);
cascade.detectMultiScale(smallImg, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30,30));
cout<<"number of faces = "<<faces.size()<<endl;
for(size_t i = 0; i < faces.size(); i++)
{
Rect r = faces[i];
Mat smallImgROI;
vector<Rect> nestedObjects;
Point center;
Scalar color = Scalar(255, 0, 0); // Color for Drawing tool
int radius;
double aspect_ratio = (double)r.width/r.height;
if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
{
center.x = cvRound((r.x + r.width*0.5)*scale);
center.y = cvRound((r.y + r.height*0.5)*scale);
radius = cvRound((r.width + r.height)*0.25*scale);
circle( img, center, radius, color, 3, 8, 0 );
}
else
rectangle( img, cv::Point(cvRound(r.x*scale), cvRound(r.y*scale)),
cv::Point(cvRound((r.x + r.width-1)*scale),
cvRound((r.y + r.height-1)*scale)), color, 3, 8, 0);
if( netCascade.empty() )
continue;
smallImgROI = smallImg( r );
}
imshow("video",img);
char c = (char) waitKey(25);
if(c == 27 || c == 'q' || c == 'Q')
{
break;
}
}
cap.release();
destroyAllWindows();
return 0;
}
首先打开笔记本电脑的摄像头。然后捕获视频。然后分配垫架。然后转换为灰度图像,调整大小。分配一个与 xml 文件匹配的人脸向量。使用 face.size(( 查找面孔数。使用 cout 在命令行上打印面孔数(faces.size(((。在脸上画圆圈。但是当我坐在它前面时,面孔数被打印为 0。我认为它应该打印 1。此外,它不会在脸周围画圆圈。我哪里做错了?当我的脸靠近屏幕时,它有时会在我的两只眼睛周围显示 2 个圆圈,但并非总是如此。有时,faces.size(( 返回 3。这怎么可能具有值 3。为什么它不总是画圆?
若要检测人脸,必须调用人脸级联分类器上的detectMultiScale
。
#include "opencv2/opencv.hpp"
#include <iostream>
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
using namespace std;
using namespace cv;
int main()
{
VideoCapture cap(0);
CascadeClassifier face_cc;
double scale = 1;
face_cc.load("/opt/opencv/data/haarcascades/haarcascade_frontalcatface.xml");
//cap.open();
if(!cap.isOpened())
{
cout<<"error opening video file"<<endl;
return -1;
}
while(1)
{
Mat img;
cap>>img;
if(img.empty())
{
break;
}
//Mat img1 = img.clone();
vector<Rect> faces, faces2;
Mat grey, smallImg;
cvtColor(img, grey, COLOR_BGR2GRAY);
double fx = 1 / scale;
resize(grey, smallImg, Size(), fx, fx, INTER_LINEAR);
equalizeHist(smallImg, smallImg);
//cascade.detectMultiScale(smallImg, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30,30));
//detect faces
face_cc.detectMultiScale( smallImg, faces );
cout<<"number of faces = "<<faces.size()<<endl;
for(size_t i = 0; i < faces.size(); i++)
{
Rect r = faces[i];
Mat smallImgROI;
vector<Rect> nestedObjects;
Point center;
Scalar color = Scalar(255, 0, 0); // Color for Drawing tool
int radius;
double aspect_ratio = (double)r.width/r.height;
if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
{
center.x = cvRound((r.x + r.width*0.5)*scale);
center.y = cvRound((r.y + r.height*0.5)*scale);
radius = cvRound((r.width + r.height)*0.25*scale);
circle( img, center, radius, color, 3, 8, 0 );
}
else
{
rectangle( img, cv::Point(cvRound(r.x*scale), cvRound(r.y*scale)),
cv::Point(cvRound((r.x + r.width-1)*scale),
cvRound((r.y + r.height-1)*scale)), color, 3, 8, 0);
}
smallImgROI = smallImg( r );
}
imshow("video",img);
char c = (char) waitKey(25);
if(c == 27 || c == 'q' || c == 'Q')
{
break;
}
}
cap.release();
destroyAllWindows();
return 0;
}
相关文章:
- 如何在 c++ 中比较 2 个链表并将匹配的数据放入另一个链表中
- OpenCV代码不会检测人脸的数量,也不会在脸周围放一个圆圈
- 我的 cout 上有一个奇怪的输出,它把答案放在第一位,然后在我调用它的地方放一个奇怪的输出.我该怎么办?
- 结构新手,我对如何从 void 函数中返回值并将其放入另一个函数感到困惑
- 如何将一个单词拆分成字母,并将它们放入一个无序的列表/集合中
- 为什么具有两个元素的发起器语法将一个元素而不是两个元素放入字符串向量中?
- Opencv,在图像中的对象周围绘制一个框
- IBM Rhapsody c++ 代码生成 - 为什么总是在状态图周围放置一个活动状态
- 如何从字符串创建一个web::uri以放入client.connect()
- 显示数组的问题.一切都放在一个单独的行中,但不放入单独的列中
- 从 c++ 库中获取一个字符串,周围有一个 java 包装器
- 使用镶嵌在图片中的单词周围画一个矩形
- 我可以放一个lambdas的无锁队列吗?
- 如何从C++程序中删除多个空格,并放一个空格代替它
- 如何允许用户在opengl中缩放一个对象而不缩放另一个对象
- 如何检测并在眼睛的虹膜区域周围画一个圆圈
- 为什么在逗号分隔的值列表周围放括号会改变赋值
- 在桌面图标后面放一个窗口
- 为什么在头文件的第一行放一个随机数
- 在要初始化的成员变量后面放一个括号