新手关于opencv3.1使用objectDetection的不解之处

kourouDaniel7 2016-11-14 10:51:17
新手一枚。根据工具书的指导,复制...\opencv\sources\data\haarcascades之中的haarcascade_eye_tree_eyeglasses.xml和haarcascade_frontalface_alt.xml到源文件文件夹之中,后分别使用...\opencv\sources\samples\cpp\tutorial_code\objectDetection里面两个函数objectDetection.cpp以及objectDetection2.cpp进行调试,发现都是闪了一下之后就灭了。添加了getchar();和waitKey();都没有见效,是我哪里出错了吗?
库中的objectDetection代码如下:
#include "opencv2/objdetect.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/** Function Headers */
void detectAndDisplay( Mat frame );

/** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";

/** @function main */
int main( void )
{
VideoCapture capture;
Mat frame;

//-- 1. Load the cascades
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade\n"); return -1; };
if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading eyes cascade\n"); return -1; };

//-- 2. Read the video stream
capture.open( -1 );
if ( ! capture.isOpened() ) { printf("--(!)Error opening video capture\n"); return -1; }

while ( capture.read(frame) )
{
if( frame.empty() )
{
printf(" --(!) No captured frame -- Break!");
break;
}

//-- 3. Apply the classifier to the frame
detectAndDisplay( frame );

int c = waitKey(10000);
if( (char)c == 27 ) { break; } // escape
}

return 0;
}

/** @function detectAndDisplay */
void detectAndDisplay( Mat frame )
{
std::vector<Rect> faces;
Mat frame_gray;

cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );

//-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );

for ( size_t i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 );
ellipse( frame, center, Size( faces[i].width/2, faces[i].height/2 ), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );

Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;

//-- In each face, detect eyes
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CASCADE_SCALE_IMAGE, Size(30, 30) );

for ( size_t j = 0; j < eyes.size(); j++ )
{
Point eye_center( faces[i].x + eyes[j].x + eyes[j].width/2, faces[i].y + eyes[j].y + eyes[j].height/2 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, eye_center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
}
}
//-- Show what you got
imshow( window_name, frame );


}
和objectDetection2代码:
/**
* @file objectDetection2.cpp
* @author A. Huaman ( based in the classic facedetect.cpp in samples/c )
* @brief A simplified version of facedetect.cpp, show how to load a cascade classifier and how to find objects (Face + eyes) in a video stream - Using LBP here
*/
#include "opencv2/objdetect.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/** Function Headers */
void detectAndDisplay( Mat frame );

/** Global variables */
String face_cascade_name = "lbpcascade_frontalface.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";
/**
* @function main
*/
int main( void )
{
VideoCapture capture;
Mat frame;

//-- 1. Load the cascade
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade\n"); return -1; };
if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading eyes cascade\n"); return -1; };

//-- 2. Read the video stream
capture.open( -1 );
if ( ! capture.isOpened() ) { printf("--(!)Error opening video capture\n"); return -1; }

while ( capture.read(frame) )
{
if( frame.empty() )
{
printf(" --(!) No captured frame -- Break!");
break;
}

//-- 3. Apply the classifier to the frame
detectAndDisplay( frame );

//-- bail out if escape was pressed
int c = waitKey(10);
if( (char)c == 27 ) { break; }
}
getchar();
return 0;
}

/**
* @function detectAndDisplay
*/
void detectAndDisplay( Mat frame )
{
std::vector<Rect> faces;
Mat frame_gray;

cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );

//-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0, Size(80, 80) );

for( size_t i = 0; i < faces.size(); i++ )
{
Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;

//-- In each face, detect eyes
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CASCADE_SCALE_IMAGE, Size(30, 30) );
if( eyes.size() == 2)
{
//-- Draw the face
Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 );
ellipse( frame, center, Size( faces[i].width/2, faces[i].height/2 ), 0, 0, 360, Scalar( 255, 0, 0 ), 2, 8, 0 );

for( size_t j = 0; j < eyes.size(); j++ )
{ //-- Draw the eyes
Point eye_center( faces[i].x + eyes[j].x + eyes[j].width/2, faces[i].y + eyes[j].y + eyes[j].height/2 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, eye_center, radius, Scalar( 255, 0, 255 ), 3, 8, 0 );
}
}

}
//-- Show what you got
imshow( window_name, frame);
}
...全文
1051 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
闪闪12138 2018-02-28
  • 打赏
  • 举报
回复
用我的这个吧,亲测有效。 /** * @file objectDetection.cpp * @author A. Huaman ( based in the classic facedetect.cpp in samples/c ) * @brief A simplified version of facedetect.cpp, show how to load a cascade classifier and how to find objects (Face + eyes) in a video stream */ #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include <stdio.h> using namespace std; using namespace cv; /** Function Headers */ void detectAndDisplay(Mat frame); /** Global variables */ //-- Note, either copy these two files from opencv/data/haarscascades to your current folder, or change these locations String face_cascade_name = "D:/Program Files/opencv2017/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml"; String eyes_cascade_name = "D:/Program Files/opencv2017/opencv/sources/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml"; CascadeClassifier face_cascade; CascadeClassifier eyes_cascade; string window_name = "Capture - Face detection"; RNG rng(12345); /** * @function main */ int main(void) { VideoCapture capture; Mat frame; //-- 1. Load the cascades if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading\n"); return -1; }; if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading\n"); return -1; }; // -- 2. Read the video stream capture.open(0); if (capture.isOpened()) { for (;;) { capture >> frame; //-- 3. Apply the classifier to the frame if (!frame.empty()) { detectAndDisplay(frame); } else { printf(" --(!) No captured frame -- Break!"); break; } int c = waitKey(10); if ((char)c == 'c') { break; } } } return 0; } /** * @function detectAndDisplay */ void detectAndDisplay(Mat frame) { std::vector<Rect> faces; Mat frame_gray; cvtColor(frame, frame_gray, COLOR_BGR2GRAY); equalizeHist(frame_gray, frame_gray); //-- Detect faces face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30)); for (size_t i = 0; i < faces.size(); i++) { Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2); ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 2, 8, 0); Mat faceROI = frame_gray(faces[i]); std::vector<Rect> eyes; // -- In each face, detect eyes eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30)); for (size_t j = 0; j < eyes.size(); j++) { Point eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2); int radius = cvRound((eyes[j].width + eyes[j].height)*0.25); circle(frame, eye_center, radius, Scalar(255, 0, 0), 3, 8, 0); } } //-- Show what you got imshow(window_name, frame); }
赵4老师 2017-02-06
  • 打赏
  • 举报
回复
String faceName="../../data/haarcascades/haarcascade_frontalface_alt2.xml";
String eyesName="../../data/haarcascades/haarcascade_mcs_eyepair_big.xml";
	CascadeClassifier face,	eyes;
	if (!face.load(faceName)) {cerr<<"Could not load classifier cascade for face:"<<faceName<<endl;return -1;}
	if (!eyes.load(eyesName)) {cerr<<"Could not load classifier cascade for eyes:"<<eyesName<<endl;return -1;}
赵4老师 2017-02-06
  • 打赏
  • 举报
回复
仅供参考:
D:\FaceDetector
+---data
|   \---haarcascades
|           haarcascade_frontalface_alt2.xml
|           haarcascade_mcs_eyepair_big.xml
|
\---demo
    \---bin
            0-14.avi
            FaceDetector.exe
            opencv_calib3d230.dll
            opencv_contrib230.dll
            opencv_core230.dll
            opencv_features2d230.dll
            opencv_ffmpeg.dll
            opencv_flann230.dll
            opencv_gpu230.dll
            opencv_highgui230.dll
            opencv_imgproc230.dll
            opencv_legacy230.dll
            opencv_ml230.dll
            opencv_objdetect230.dll
            opencv_ts230.dll
            opencv_video230.dll

赵4老师 2016-11-27
  • 打赏
  • 举报
回复
VS IDE中,在不明白的符号上点鼠标右键,选转到定义。
kourouDaniel7 2016-11-27
  • 打赏
  • 举报
回复
引用 6 楼 hello_stranger 的回复:
main函数里面打断点 1.看看两个xml文件读取到没有,两个文件读取的xml是不同的 2.看看视频读取到没有 基本上就这两个地方了
两个xml文件没错。我现在还不懂读到没有是什么意思QAQ。。。以及打断点。应该怎么打呢
赵4老师 2016-11-17
  • 打赏
  • 举报
回复
waitKey(0) 和 getchar() 不是一回事!
lx624909677 2016-11-17
  • 打赏
  • 举报
回复
如果还不行,那就加断点跟一下看看是哪儿退出了
lx624909677 2016-11-17
  • 打赏
  • 举报
回复
那你加两个getchar试试
hello_stranger 2016-11-17
  • 打赏
  • 举报
回复
main函数里面打断点 1.看看两个xml文件读取到没有,两个文件读取的xml是不同的 2.看看视频读取到没有 基本上就这两个地方了
kourouDaniel7 2016-11-16
  • 打赏
  • 举报
回复
加在第二个代码 依然不变呀 还是闪了一下就没有了 况且第二个代码本身就带有getchar不是吗
lx624909677 2016-11-15
  • 打赏
  • 举报
回复
waitKey(0)

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧