What does the cvHaarDetectObjects () method do?

Please, can one of the experts explain to me if we can use the cvHaarDetectObjects () method to detect squares and get width and height? I found code that uses this method to detect a face, but I need to know if I can use it to detect a rectangle.

String src="src/squiredetection/MY.JPG"; IplImage grabbedImage = cvLoadImage(src); IplImage grayImage = IplImage.create(grabbedImage.width(), grabbedImage.height(), IPL_DEPTH_8U, 1); cvCvtColor(grabbedImage, grayImage, CV_BGR2GRAY); CvSeq faces = cvHaarDetectObjects(grayImage, cascade, storage, 1.1, 3, 0);//* for (int i = 0; i < faces.total(); i++) { CvRect r = new CvRect(cvGetSeqElem(faces, i)); cvRectangle(grabbedImage, cvPoint(rx(), ry()), cvPoint(rx()+r.width(), ry()+r.height()), CvScalar.RED, 1, CV_AA, 0); /* hatPoints[0].x = rx-r.width/10; hatPoints[0].y = ry-r.height/10; hatPoints[1].x = r.x+r.width*11/10; hatPoints[1].y = ry-r.height/10; hatPoints[2].x = r.x+r.width/2; hatPoints[2].y = ry-r.height/2;*/ // cvFillConvexPoly(grabbedImage, hatPoints, hatPoints.length, CvScalar.GREEN, CV_AA, 0); } 

when i use the method above it throws the following exception

 OpenCV Error: Bad argument (Invalid classifier cascade) in unknown function, file C:\slave\WinInstallerMegaPack\src\opencv\modules\objdetect\src\haar.cpp, line 1036 Exception in thread "main" java.lang.RuntimeException: C:\slave\WinInstallerMegaPack\src\opencv\modules\objdetect\src\haar.cpp:1036: error: (-5) Invalid classifier cascade at com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects(Native Method) at com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects(opencv_objdetect.java:243) at squiredetection.Test2.main(Test2.java:52 I have put * on this line) 

Please be kind to give a simple code example for this.

+6
source share
2 answers

cvHaarDetectObjects() used to detect objects or shapes not only for faces, but also depends on the HaarCascade classifier.

If you go face haarcascade xml then it will return an array of faces or it will also be able to use the eye , nose file, etc. HaarCascade XML file. You can also create custom haarcascade xml by creating your own positive and negative samples using opencv_traincascade.exe

 CvSeq faces = cvHaarDetectObjects(grayImage, classifier, storage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING); for (int i = 0; i < faces.total(); i++) { // its ok } 

more about opencv doc

to detect a rectangle:

There is an example for defining a rectangle in OpenCV , they use it to detect squares in a chessboard. See squares.c in .. \ OpenCV \ samples \ c \ directory.

see this checkerboard detection pattern in opencv

An invalid classifier cascade in an unknown function error means that the classifier you missed is incorrectly formatted or something is missing. Check if your XML classifier file is valid.

+6
source

cvHaarDetectObjects returns several faces detected in the image. You must declare a CvSeq array to store the result, not just one CvSeq.

 // There can be more than one face in an image. // So create a growable sequence of faces. // Detect the objects and store them in the sequence CvSeq* faces = cvHaarDetectObjects( img, cascade, storage, 1.1, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(40, 40) ); 

The code above was extracted from this site:

http://opencv.willowgarage.com/wiki/FaceDetection

+2
source

Source: https://habr.com/ru/post/919027/


All Articles