简体   繁体   English

cvHaarDetectObjects()方法有什么作用?

[英]What does cvHaarDetectObjects() method do?

Please can some expert person explain me whether we can use the cvHaarDetectObjects() method to detect squares and get width and heights? 有些专家可以解释一下我是否可以使用cvHaarDetectObjects()方法来检测方块并获得宽度和高度? 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. cvHaarDetectObjects()不仅用于检测面部的对象或形状,还取决于HaarCascade分类器。

If you pass face haarcascade xml then it will return an array of faces or also can use eye , nose , etc HaarCascade XML file. 如果你传递face haarcascade xml然后它将返回一个面数组或者也可以使用eyenose等HaarCascade XML文件。 You can make also custom haarcascade xml by creating your own positive and negative samples using opencv_traincascade.exe 您可以自定义也haarcascade xml创建使用自己的阳性和阴性样品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 opencv doc上的详细信息

for rectangle detection : 用于矩形检测:

there is an example for rectangle detection in OpenCV , they use it to detect the squares in a chessboard. OpenCV有一个矩形检测的例子,它们用它来检测棋盘中的方块。 Have a look to squares.c in ..\\OpenCV\\samples\\c\\ directory. 看看.. \\ OpenCV \\ samples \\ c \\目录中的squares.c

see this chessboard detection sample in opencv 在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. 检查分类器xml文件是否有效。

cvHaarDetectObjects returns multiple faces detected in an image. cvHaarDetectObjects返回图像中检测到的多个面。 You have to declare an array of CvSeq to store the result, not just a single CvSeq. 您必须声明一个CvSeq数组来存储结果,而不仅仅是单个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 http://opencv.willowgarage.com/wiki/FaceDetection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM