请帮忙解决这个问题:
OpenCV Error: Insufficient memory (Failed to allocate 921604 bytes) in unknown function, file ........\ocv\opencv\modules\core\src\alloc.cpp, line 52
我的方法之一是使用 cv::clone
和 pointer
代码是:
每100ms有一个定时器; 在定时器事件中,我调用了这个方法:
void DialogApplication::filterhijau(const Mat &image, Mat &result) {
cv::Mat resultfilter = image.clone();
int nlhijau = image.rows;
int nchijau = image.cols*image.channels();;
for(int j=0; j<nlhijau; j++) {
uchar *data2=resultfilter.ptr<uchar> (j); //alamat setiap line pada result
for(int i=0; i<nchijau; i++) {
*data2++ = 0; //element B
*data2++ = 255; //element G
*data2++ = 0; //element R
}
// free(data2); //I add this line but the program hung up
}
cv::addWeighted(resultfilter,0.3,image,0.5,0,resultfilter);
result=resultfilter;
}
最佳答案
cv::Mat
的clone()
方法执行数据的硬拷贝。所以问题是为每个 filterhijau()
分配一个新图像,在调用此方法数百次后,您的应用程序将占用数百 MB(如果不是 GB),从而抛出 内存不足错误。
看来您需要重新设计当前的方法,以使其占用更少的 RAM 内存。
关于c++ - 如何修复内存不足错误(openCV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14888431/