Home > Software engineering >  Novice about opencv3.1 use objectDetection don't understand
Novice about opencv3.1 use objectDetection don't understand

Time:09-24

New one, according to the guidance of reference books, copy... \ opencv \ sources \ data \ haarcascades haarcascade_eye_tree_eyeglasses. Of XML and haarcascade_frontalface_alt. The XML to the source file folder, respectively after the use... \ opencv \ sources \ samples \ CPP \ tutorial_code \ objectDetection inside two functions objectDetection CPP and objectDetection2. CPP debug, found that is destroyed, for a moment then added getchar (); And waitKey (); Don't work, it is where I went wrong?
The library objectDetection code is as follows:
# include "opencv2/objdetect HPP"
# include "opencv2/videoio HPP"
# include "opencv2/highgui. HPP"
# include "opencv2/imgproc HPP"

#include
#include

using namespace std;
Using the 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
The capture. Open (1);
if ( ! The capture. IsOpened ()) {printf (" -- -- (!) Error opening video capture \ n "); return -1; }

While (the capture. Read (frame))
{
If (frame. The empty ())
{
Printf (" -- -- (!) No captured frame - Break!" );
break;
}

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

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

return 0;
}

/* * @ function detectAndDisplay */
Void detectAndDisplay (Mat frame)
{
STD: : vector 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 {
Point center (faces [I]. X + faces [I] width/2, faces [I] y + faces [I] height/2);
The ellipse (frame, center, the 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 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 & lt; Eyes. The size (); J++)
{
Point eye_center (faces [I]. X + eyes [j]. Journal of x + eyes [j]. Journal of width/2, faces [I] y + eyes [j]. J y + eyes [j]. Journal of height/2);
Int the radius=cvRound ((eyes [j]. Journal of width + eyes [j]. Journal of height) * 0.25);
Circle (radius, frame, eye_center, Scalar (255, 0, 0), 4, 8, 0).
}
}
//- Show what you got
Imshow (window_name, frame);


}
And objectDetection2 code:
/* *
* @ file objectDetection2. CPP
* @ author a. its (-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
#include

using namespace std;
Using the 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
The capture. Open (1);
if ( ! The capture. IsOpened ()) {printf (" -- -- (!) Error opening video capture \ n "); return -1; }

While (the capture. Read (frame))
{
If (frame. The empty ())
{
Printf (" -- -- (!) No captured frame - Break!" );
break;
}

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

//- bail out if the escape was pressed
C=waitKey int (10);
If ((char) c==27) {break; }
}
Getchar ();
return 0;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related