简体   繁体   中英

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

One of my method using cv::clone and pointer

The code is:

There is a timer every 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. 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.

It seems like you need to redesign your current approach so it occupies less RAM memory.

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 :

# 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM