gpt4 book ai didi

java - cvHaarDetectObjects() 方法有什么作用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:06:55 26 4
gpt4 key购买 nike

请专家解释一下,是否可以使用cvHaarDetectObjects()方法来检测正方形并获取宽度和高度?我找到了一个使用这种方法进行人脸检测的代码,但我需要知道我是否可以将它用于矩形检测。

    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);
}

当我使用上面的方法时,它抛出以下异常

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)

请提供简单的代码示例。

最佳答案

cvHaarDetectObjects() 不仅用于面部检测对象或形状,它依赖于 HaarCascade 分类器。

如果你传递face haarcascade xml,那么它会返回一个面孔数组,或者也可以使用eyenose等HaarCascade XML文件.您还可以通过使用 opencv_traincascade.exe

创建自己的正样本和负样本来制作自定义 haarcascade xml
CvSeq faces = cvHaarDetectObjects(grayImage, classifier, storage,
1.1, 3, CV_HAAR_DO_CANNY_PRUNING);

for (int i = 0; i < faces.total(); i++) {
// its ok
}

关于 opencv doc 的详细信息

矩形检测:

there is an example for rectangle detection in OpenCV, they use it to detect the squares in a chessboard. Have a look to squares.c in ..\OpenCV\samples\c\ directory.

看这个chessboard detection sample in opencv

Invalid classifier cascade in unknown function 错误表示您传递的分类器格式不正确或缺少某些内容。检查您的分类器 xml 文件是否有效。

关于java - cvHaarDetectObjects() 方法有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11207840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com