gpt4 book ai didi

c++ - OpenCv 断言失败

转载 作者:太空宇宙 更新时间:2023-11-03 22:01:12 24 4
gpt4 key购买 nike

我正在开发用于在 2D 图像中查找人脸的应用程序,稍后我想在同一图像中查找嘴巴,但我现在遇到了一些问题。到目前为止,这是我的代码:

for (int i = 0; i < faces.size(); i++)
{
Point pt1(faces[i].x, faces[i].y);
Point pt2((faces[i].x + faces[i].height), (faces[i].y + faces[i].width));
rectangle(frame, pt1, pt2, Scalar(255,0 , 0), 2, 8, 0);

//I WANT ROI(FOR MOUTH DETECTION) TO BE ONLY HALF OF THE RECTANGLE WITH FACE
Rect mouthROI;
mouthROI.x = (faces[i].x);
mouthROI.y = faces[i].y*(1.5);
mouthROI.width = (faces[i].x + faces[i].height);
mouthROI.height = (faces[i].y + faces[i].width);

//I CHECK IF NEW RECTANGLE IS EXACTLY BOTTOM HALF OF PREVIOUS ONE
Point ptAA(mouthROI.x, mouthROI.y);
Point ptBB(mouthROI.width, mouthROI.height);
rectangle(frame, ptBB, ptAA, Scalar(0,0 , 255), 2, 2, 0);


Mat image_roi = frame(mouthROI);

cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist( frame_gray, frame_gray );

// DETECTING MOUTH INSIDE ROI OF EARLIER DETECTED FACE
mouth_cascade.detectMultiScale(image_roi, mouths, 1.1, 2, 0, Size(30, 30));

for(int i = 0; i < mouths.size(); i++)
{
Point pt1(mouths[i].x, mouths[i].y); // Display detected faces on main window - live stream from camera
Point pt2((mouths[i].x + mouths[i].height), (mouths[i].y + mouths[i].width));
rectangle(frame, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);
}

}

不幸的是,这段代码不起作用。我收到这样的错误:

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat(const cv::Mat&, const cv::Rect&), file C:/build/2_4_PackSlave-win32-vc11-shared/opencv/modules/core/src/matrix.cpp, line 323

我该如何解决这个错误。谢谢大家的帮助!

最佳答案

您初始化 mouthROI 的方式不正确。应该是这样的,

<previous code>
mouthROI.width = (faces[i].height);
mouthROI.height = (faces[i].width);

Point ptAA(mouthROI.x, mouthROI.y);
Point ptBB(mouthROI.x+mouthROI.width, mouthROI.y+mouthROI.height);

请记住,cv::Rect 不采用矩形的位置,它采用左上角的位置以及宽度高度..引用documentation了解更多详情。

HTH

关于c++ - OpenCv 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21248611/

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