Please can some expert person explain me whether we can use the cvHaarDetectObjects() method to detect squares and get width and heights? I found a code that use this method for face-detection but I need to know whether I can use it for rectangle detection.
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(r.x(), r.y()), cvPoint(r.x()+r.width(), r.y()+r.height()), CvScalar.RED, 1, CV_AA, 0);
/* hatPoints[0].x = r.x-r.width/10; hatPoints[0].y = r.y-r.height/10;
hatPoints[1].x = r.x+r.width*11/10; hatPoints[1].y = r.y-r.height/10;
hatPoints[2].x = r.x+r.width/2; hatPoints[2].y = r.y-r.height/2;*/
// cvFillConvexPoly(grabbedImage, hatPoints, hatPoints.length, CvScalar.GREEN, CV_AA, 0);
}
when I use above method it throws 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 enough to give simple code example for that.
cvHaarDetectObjects()
is used for detecting objects or shapes not only for faces, it depends on HaarCascade
classifier.
If you pass face haarcascade xml
then it will return an array of faces or also can use eye
, nose
, etc HaarCascade XML file. You can make also 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
}
detail on opencv doc
for rectangle detection :
there is an example for rectangle detection in
OpenCV
, they use it to detect the squares in a chessboard. Have a look tosquares.c
in ..\OpenCV\samples\c\ directory.
see this chessboard detection sample in opencv
Invalid classifier cascade in unknown function error means the classifier you passed is not correctly formatted or something is missing. Check if your classifier xml file is valid.
cvHaarDetectObjects
returns multiple faces detected in an image. You have to declare an array of CvSeq to store the result, not just a single 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With