Memory issue in imread function

asked 2018-07-11 01:56:33 -0600

Purva gravatar image

updated 2018-07-11 06:34:59 -0600

During flushing of 45 to 50 images in sequence, opencv imread function is giving error of "OpenCV Error: Insufficient memory (Failed to allocate memory) in OutOfMemoryError. Please provide the solution to this problem, that how to release memory of opencv imread during cancellation of one imread function in between.

edit retag flag offensive close merge delete

Comments

please show your coding attempt, then we can refer to that.

(edit your question, add it there)

and maybe the 1st question would be: do you really need all of them in memory (at the same time) ?

berak gravatar imageberak ( 2018-07-11 02:00:24 -0600 )edit

The scenario is as follow:

There is a large image of about 10MB and I am reading it using imread in thread. After reading another processing is carried out using resize, grayscale and other functions of opencv. Now suppose while reading that 10MB image we want to cancel that process and directly read the next image. I am able to cancel the previous reading process by keeping it in a thread, but after doing so for 45 to 50 images opencv shows the memory error.

Here I do not want the previous image in memory only the present input image is required. Also I have used Mat.release function to release the mat objects used in the software at every cancel of thread.

Purva gravatar imagePurva ( 2018-07-11 02:32:16 -0600 )edit

Pseudo code:

Mat Input_Image;

main () { Thread_to_Process_Images() if(interrupt_event_occures) { cancel(Thread_to_Process_Images); //cancel thread to process images Action_On_Thread_Cancel(); Thread_to_Process_Images(); //Process next image from the list } }

*Thread_to_Process_Images() { Input_Image = imread(Imageinsequence,1); //Other opencv operations on Input_Image; }

void Action_On_Thread_Cancel() { Input_Image.release();
}

Purva gravatar imagePurva ( 2018-07-11 02:32:43 -0600 )edit

your pseudo code is unfortunately quite useless.

berak gravatar imageberak ( 2018-07-11 02:42:58 -0600 )edit

I am trying to explain again if it is not clear.

Pseudo code:

  1. Create a Thread
  2. Open 10MB BMP image in mat form using imread(image name and CV_LOAD_IMAGE_COLOR)
  3. While reading above image if there is a new image then I am killing above thread and releasing memory using image.release API.
  4. Now create new thread and try to read image as explain in above steps.

So ultimately, I want to process new image immediately and cancel the processing of previous image but we need your help to understand whether above method is correct or not.

Here, it seems image.release API is not clearing all memory and after processing of 45-50 images open CV is giving error: Insufficient memory (Failed to allocate memory) otherwise things are working fine.

Purva gravatar imagePurva ( 2018-07-12 02:55:12 -0600 )edit

So, I would like to know what best practice is to cancel the process running in imread function.

I am using Linux OS ARM based processor, Open-CV version 3.2.0 cross compiled with arm-linux-gnueabihf compiler.

Purva gravatar imagePurva ( 2018-07-12 02:55:38 -0600 )edit

as mentioned here already,

  • you don't have enough memory to do it this way
  • you can't "cancel" an imread() operation.
  • cancelling your thread will most likely lead to the effect, that the release() is no more executed (not what you want !) again, you have no control about when or where it's cancelled.


imho, it's a (broken) design problem. do you absolutely need multi-threading ? (do you know enough about it ?)

the idea of "cancelling" threads seems to be especially wrong, it's only adding more (and wasted !) work to your pipeline.

it's probably better to have a limited thread pool (like 3 only), and instead of cancelling a previous job, let it finish, and only start a new one, if resources are available for it.

berak gravatar imageberak ( 2018-07-12 03:15:33 -0600 )edit