3

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;
}
romy budhi
  • 49
  • 1
  • 1
  • 3
  • Each call to this function is creating a new image. How many calls are you making to this function? – carlosdc Feb 15 '13 at 04:56
  • instead of `result = resultfilter`, do this `result = resultfilter.clone(); resultfilter.release()`. And **never ever** free the internal data pointer of `cv::Mat` object like this: `free(data2)` – sgarizvi Feb 15 '13 at 05:09
  • @carlosdc: I think hundreds because the program run well, but after 2 minutes there is an error display. – romy budhi Feb 15 '13 at 08:10
  • @sgar91: Thanks for correction. But after result = resultfilter.clone(), it is mean we create a new image again. After trying there is still an error message. Is there any other possibilities? – romy budhi Feb 15 '13 at 08:12
  • 1
    You usually do not need Mat::release(), because the memory is freed automatically when the instance of cv::Mat runs out of scope (RAII). Please show a complete code to reproduce your problem. ( http://sscce.org/ ). Without we just have to guess that you are storing all the result images you create with filterhijau in some kind of container. – Tobias Hermann Feb 15 '13 at 08:31
  • I just use a timer every 100ms. In the timer, I call that method to filter out other green color. – romy budhi Feb 18 '13 at 00:07

2 Answers2

3

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.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Do you mean that clone() should be replace by other?. I just use a timer and in the timer I call filterhijau() method. – romy budhi Feb 18 '13 at 00:10
  • No. I mean you need to re-think how your current program works and redesign it so you don't occupy so much RAM. Do you really need to store all those images at the same time in the memory? Sounds like you could release them after they have been processed so they don't stay around wasting useful RAM space. – karlphillip Feb 18 '13 at 21:29
  • @karlphilip: Actually no need to store the image. I did not find cv::Release in C++ like CvRelease in C. – romy budhi Feb 22 '13 at 01:36
-1

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()

N.B. I know it's not the most optimum solution for the problem but, it was enough for me.

Mostafa Wael
  • 2,750
  • 1
  • 21
  • 23
  • reducing in size will not cut it because its a flawed method approach and you will end-up with the same error but later in the process. This besides your answering in the wrong programming language. – ZF007 Nov 14 '20 at 14:54
  • I know this is not the best approach but, I was only suggesting a possible approach that was fine for me. – Mostafa Wael Nov 15 '20 at 12:35