Memory issue in imread function
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.
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) ?
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.
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();
}
your pseudo code is unfortunately quite useless.
I am trying to explain again if it is not clear.
Pseudo code:
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.
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.
as mentioned here already,
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.