[英]How to fix the insufficient memory error (openCV)
Please help how to handle this problem:请帮助如何处理这个问题:
OpenCV Error: Insufficient memory (Failed to allocate 921604 bytes) in unknown function, file ........\\ocv\\opencv\\modules\\core\\src\\alloc.cpp, line 52 OpenCV 错误:未知函数中的内存不足(无法分配 921604 字节),文件 ........\\ocv\\opencv\\modules\\core\\src\\alloc.cpp,第 52 行
One of my method using cv::clone
and pointer
我使用cv::clone
和pointer
The code is:代码是:
There is a timer every 100ms;每100ms有一个定时器; In the timer event, I call this method:在定时器事件中,我调用这个方法:
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;
}
The clone()
method of a cv::Mat
performs a hard copy of the data. cv::Mat
的clone()
方法执行数据的硬拷贝。 So the problem is that for each filterhijau()
a new image is allocated, and after hundreds of calls to this method your application will have occupied hundreds of MBs (if not GBs), thus throwing the Insufficient Memory error.所以问题是,为每个filterhijau()
分配了一个新图像,在数百次调用此方法后,您的应用程序将占用数百 MB(如果不是 GB),从而引发Insufficient Memory错误。
It seems like you need to redesign your current approach so it occupies less RAM memory.似乎您需要重新设计当前的方法,以便它占用更少的 RAM 内存。
I faced this error before, I solved it by reducing the size of the image while reading them and sacrificed some resolution.我之前遇到过这个错误,我通过在阅读图像时减小图像的大小并牺牲一些分辨率来解决它。
It was something like this in Python :在 Python 中是这样的:
# Open the Video
cap = cv2.VideoCapture(videoName + '.mp4')
i = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.resize(frame, (900, 900))
# append the frames to the list
images.append(frame)
i += 1
cap.release()
NB I know it's not the most optimum solution for the problem but, it was enough for me. NB我知道这不是解决问题的最佳解决方案,但是对我来说已经足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.