C++ Opencv c++;人脸检测代码不工作
我正在尝试运行下面的人脸检测代码,但其中有一个问题C++ Opencv c++;人脸检测代码不工作,c++,opencv,face-detection,C++,Opencv,Face Detection,我正在尝试运行下面的人脸检测代码,但其中有一个问题 circle( image, center,Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360,Scalar( 255, 0, 255 ), 4, 8, 0 ); 完整代码: #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/img
circle( image, center,Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360,Scalar( 255, 0, 255 ), 4, 8, 0 );
完整代码:
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include<highgui.h>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
IplImage* img = cvLoadImage( "1.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
Mat image=cvarrToMat(img);
CascadeClassifier face_cascade;
face_cascade.load( "haarcascade_frontalface_alt2.xml" );
std::vector<Rect> faces;
face_cascade.detectMultiScale( image, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
// Draw circles on the detected faces
for( int i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
circle( image, center,Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360,Scalar( 255, 0, 255 ), 4, 8, 0 );
}
imshow("Detected Face",image);
waitKey(0);
return 0;
}
#包括“opencv2/objdetect/objdetect.hpp”
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间cv;
int main(int argc,字符**argv)
{
IplImage*img=cvLoadImage(“1.jpg”);
cvNamedWindow(“示例1”,CV\u窗口\u自动调整大小);
cvShowImage(“示例1”,img);
Mat图像=cvarrToMat(img);
层叠式分级机;
face_cascade.load(“haarcascade_frontalface_alt2.xml”);
向量面;
面部级联。检测多尺度(图像,面部,1.1,2,0 | CV|u HAAR|u尺度|图像,大小(30,30));
//在检测到的面上绘制圆
对于(int i=0;i
请检查face\u cascade.load(“haarcascade\u frontalface\u alt2.xml”)的返回值
。否则,如果找不到文件haarcascade\u frontalface\u alt2.xml
,则不会发生任何事情
我可能没有与您相同版本的opencv,但这里有一个基于您的代码,可以生成可接受的输出。可能需要更改include文件和haarcascade_frontalface_alt2.xml
的路径
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include<opencv/highgui.h>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
IplImage* img = cvLoadImage( "2.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
Mat image=cvarrToMat(img);
CascadeClassifier face_cascade;
// face_cascade.load( "haarcascade_frontalface_alt2.xml" );
String face_cascade_name = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml";
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
std::vector<Rect> faces;
face_cascade.detectMultiScale( image, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
// Draw circles on the detected faces
for( int i = 0; i < faces.size(); i++ )
{
Point center=Point( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
circle( image, center,faces[i].width/2,Scalar( 255, 0, 255 ), 4, 8, 0 );
}
imshow("Detected Face",image);
try {
imwrite("alpha.jpg", image);
}
catch (runtime_error& ex) {
fprintf(stderr, "Exception converting image to jpg format: %s\n", ex.what());
return 1;
}
waitKey(0);
return 0;
}
基于此,我得出以下结论:
请检查
face\u cascade.load(“haarcascade\u frontalface\u alt2.xml”)的返回值
。否则,如果找不到文件haarcascade\u frontalface\u alt2.xml
,则不会发生任何事情
我可能没有与您相同版本的opencv,但这里有一个基于您的代码,可以生成可接受的输出。可能需要更改include文件和haarcascade_frontalface_alt2.xml
的路径
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include<opencv/highgui.h>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
IplImage* img = cvLoadImage( "2.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
Mat image=cvarrToMat(img);
CascadeClassifier face_cascade;
// face_cascade.load( "haarcascade_frontalface_alt2.xml" );
String face_cascade_name = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml";
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
std::vector<Rect> faces;
face_cascade.detectMultiScale( image, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
// Draw circles on the detected faces
for( int i = 0; i < faces.size(); i++ )
{
Point center=Point( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
circle( image, center,faces[i].width/2,Scalar( 255, 0, 255 ), 4, 8, 0 );
}
imshow("Detected Face",image);
try {
imwrite("alpha.jpg", image);
}
catch (runtime_error& ex) {
fprintf(stderr, "Exception converting image to jpg format: %s\n", ex.what());
return 1;
}
waitKey(0);
return 0;
}
基于此,我得出以下结论:
请检查
face\u cascade.load(“haarcascade\u frontalface\u alt2.xml”)的返回值
。否则,如果找不到文件haarcascade\u frontalface\u alt2.xml
,则不会发生任何事情
我可能没有与您相同版本的opencv,但这里有一个基于您的代码,可以生成可接受的输出。可能需要更改include文件和haarcascade_frontalface_alt2.xml
的路径
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include<opencv/highgui.h>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
IplImage* img = cvLoadImage( "2.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
Mat image=cvarrToMat(img);
CascadeClassifier face_cascade;
// face_cascade.load( "haarcascade_frontalface_alt2.xml" );
String face_cascade_name = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml";
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
std::vector<Rect> faces;
face_cascade.detectMultiScale( image, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
// Draw circles on the detected faces
for( int i = 0; i < faces.size(); i++ )
{
Point center=Point( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
circle( image, center,faces[i].width/2,Scalar( 255, 0, 255 ), 4, 8, 0 );
}
imshow("Detected Face",image);
try {
imwrite("alpha.jpg", image);
}
catch (runtime_error& ex) {
fprintf(stderr, "Exception converting image to jpg format: %s\n", ex.what());
return 1;
}
waitKey(0);
return 0;
}
基于此,我得出以下结论:
请检查
face\u cascade.load(“haarcascade\u frontalface\u alt2.xml”)的返回值
。否则,如果找不到文件haarcascade\u frontalface\u alt2.xml
,则不会发生任何事情
我可能没有与您相同版本的opencv,但这里有一个基于您的代码,可以生成可接受的输出。可能需要更改include文件和haarcascade_frontalface_alt2.xml
的路径
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include<opencv/highgui.h>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
IplImage* img = cvLoadImage( "2.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
Mat image=cvarrToMat(img);
CascadeClassifier face_cascade;
// face_cascade.load( "haarcascade_frontalface_alt2.xml" );
String face_cascade_name = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml";
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
std::vector<Rect> faces;
face_cascade.detectMultiScale( image, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
// Draw circles on the detected faces
for( int i = 0; i < faces.size(); i++ )
{
Point center=Point( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
circle( image, center,faces[i].width/2,Scalar( 255, 0, 255 ), 4, 8, 0 );
}
imshow("Detected Face",image);
try {
imwrite("alpha.jpg", image);
}
catch (runtime_error& ex) {
fprintf(stderr, "Exception converting image to jpg format: %s\n", ex.what());
return 1;
}
waitKey(0);
return 0;
}
基于此,我得出以下结论:
这是因为您没有正确调用,它不接受10个输入参数:
//! draws the circle outline or a solid circle in the image
CV_EXPORTS_W void circle(CV_IN_OUT Mat& img, Point center, int radius,
const Scalar& color, int thickness=1,
int lineType=8, int shift=0);
对于您的情况,您应该按如下方式进行调用:
circle(image, center, faces[i].width*0.5, Scalar(255, 0, 255), 4, 8, 0);
这是因为您没有正确调用,它不接受10个输入参数:
//! draws the circle outline or a solid circle in the image
CV_EXPORTS_W void circle(CV_IN_OUT Mat& img, Point center, int radius,
const Scalar& color, int thickness=1,
int lineType=8, int shift=0);
对于您的情况,您应该按如下方式进行调用:
circle(image, center, faces[i].width*0.5, Scalar(255, 0, 255), 4, 8, 0);
这是因为您没有正确调用,它不接受10个输入参数:
//! draws the circle outline or a solid circle in the image
CV_EXPORTS_W void circle(CV_IN_OUT Mat& img, Point center, int radius,
const Scalar& color, int thickness=1,
int lineType=8, int shift=0);
对于您的情况,您应该按如下方式进行调用:
circle(image, center, faces[i].width*0.5, Scalar(255, 0, 255), 4, 8, 0);
这是因为您没有正确调用,它不接受10个输入参数:
//! draws the circle outline or a solid circle in the image
CV_EXPORTS_W void circle(CV_IN_OUT Mat& img, Point center, int radius,
const Scalar& color, int thickness=1,
int lineType=8, int shift=0);
对于您的情况,您应该按如下方式进行调用:
circle(image, center, faces[i].width*0.5, Scalar(255, 0, 255), 4, 8, 0);
您得到的错误文本是什么?编辑您的问题以将其包括在内。请不要使用IplImage*。您得到的错误文本是什么?编辑您的问题以将其包括在内。请不要使用IplImage*。您得到的错误文本是什么?编辑您的问题以将其包括在内。请不要使用IplImage*。您得到的错误文本是什么?编辑您的问题以包含它。请不要使用IplImage*。非常感谢我执行了代码,两个图像都会显示(在人脸检测之前和之后),但第二个图像中没有环绕人脸的圆圈image@user3072028确保您已成功加载了haarcascade_frontalface_alt2.xml。如何确保?对不起,我是新来的opencv@user3072028您可以检查
face\u cascade.load(“haarcascade\u frontalface\u alt2.xml”)
的返回值是否为真。非常感谢我执行了代码,两个图像都显示出来(在人脸检测之前和之后)但在第二张脸上没有圆圈image@user3072028确保您已成功加载了haarcascade_frontalface_alt2.xml。如何确保?对不起,我是新来的opencv@user3072028您可以检查face\u cascade.load(“haarcascade\u frontalface\u alt2.xml”)
的返回值是否为真。非常感谢我执行了代码,两个图像都显示出来(在人脸检测之前和之后)但在第二张脸上没有圆圈image@user3072028确保您已成功加载了haarcascade_frontalface_alt2.xml。如何确保?对不起,我是新来的opencv@user3072028您可以检查face\u cascade.load(“haarcascade\u frontalface\u alt2.xml”)
的返回值是否为真。非常感谢我执行了代码,两个图像都显示出来(在人脸检测之前和之后)但在第二张脸上没有圆圈image@user3072028确保您已成功加载了haarcascade_frontalface_alt2.xml。如何确保?对不起,我是新来的opencv@user3072028您可以检查face\u cascade.load(“haarcascade\u frontalface\u alt2.xml”)
的返回值是否为真。