CascadeClassifier :: detectMultiScale不适用于C ++ [英] CascadeClassifier::detectMultiScale doesn't work with C++
问题描述
我正在使用OpenCV和CascadeClassifier::detectMultiScale
进行面部检测.我的问题是,这似乎导致输出vector<Rect>
上的内存损坏.该向量正确填充了Rect,但是当释放该向量时会导致崩溃.
I'm using OpenCV and CascadeClassifier::detectMultiScale
for facial detection. My problem is that it seems to cause memory corruption on the output vector<Rect>
. The vector is correctly filled with Rects, but it causes a crash when the vector is deallocated.
这仅在编译Debug版本时发生.该错误消息是调试断言失败",这使我想知道发布版本中是否还会出现问题,并且断言根本没有被检查.
This only occurs when compiling a Debug build. The error message is a Debug Assertion Failed, which makes me wonder if there is a problem that also occurs in Release build, and the assert simply isn't checked.
这可能是OpenCV的错误吗?还是仅仅是我在处理向量方面做错了事?
Could this be a bug with OpenCV? Or is it just that I'm doing something wrong with how I handle my vectors?
以下代码段显示了一个示例代码,用于重现该错误:
The following code snippet shows an example code to reproduce the bug:
#include <opencv2/opencv.hpp>
using namespace cv;
int main(array<System::String ^> ^args)
{
VideoCapture video(0);
Mat frame;
CascadeClassifier classifier("haarcascade_frontalface_default.xml");
while (waitKey(1000 / 30) != 'q')
{
video >> frame;
vector<Rect> faces;
classifier.detectMultiScale(frame, faces);
for (int i = 0; i < faces.size(); i++)
rectangle(frame, faces[i], Scalar(255, 255, 255));
imshow("Camera", frame);
} // <<< The crash occurs here when the faces vector is released
}
我收到以下错误消息:
调试断言失败!
程序:MyProgram.exe 文件:minkernel \ crts \ ucrt \ src \ appcrt \ heap \ debug_heap.cpp 线:892
Program: MyProgram.exe File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp Line: 892
表达式:is_block_type_valid(header-> _ block_use)
Expression: is_block_type_valid(header->_block_use)
推荐答案
我遇到了同样的问题.我通过将取消引用的全局指针传递给函数来解决了这个问题.
I had this same issue. I solved it by passing a dereferenced global pointer to the function.
即
std::vector<cv::Rect>* faces = nullptr;
void init()
{
faces = new std::vector<cv::Rect>; //never call delete whatever you do
}
void findSomeFaces()
{
cascade->detectMultiScale(image_source, *faces);
}
这篇关于CascadeClassifier :: detectMultiScale不适用于C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!