OpenCV 3.2.0 - Insufficient Memory during use of detectMultiScale()
I'm using OpenCV 3.2.0 on Xcode 8.2.1+ and trying to create an object detection application on the iPad. The iPad that is in use for testing is an iPad Air 2, which has 2GB of RAM. We have detectors that have already been trained. I want to be able to detect using the detectors prebuilt with images taken on the iPad.
However I cannot get my code to work on the iPad due to the fact that the detectMultiScale requires about 6.4GB of memory to be allocated to run.
Here is a snippet of the code in Objective-C++:
#import <opencv2 opencv.hpp=""> #import "OpenCVWrapper.h" #import <opencv2 imgcodecs="" ios.h=""> -(void) detectObject:(UIImage *)image { double scaleFactor = 1.01; int mergeThreshold = 100; cv::Mat imageMat, imageGray; UIImageToMat(image, imageMat); cv::cvtColor(imageMat, imageGray, cv::COLOR_BGR2GRAY); // This is where we get the error, within this method objectDetector.detectMultiScale(imageMat, _boxes, scaleFactor, mergeThreshold); }
And the error that I get:
POC-Planogram(282,0x1b1481c40) malloc: *** mach_vm_map(size=6395445248) failed (error code=3) *** error: can't allocate region *** set a breakpoint in malloc_error_break to debug OpenCV Error: Insufficient memory (Failed to allocate 6395435520 bytes) in OutOfMemoryError, file /Volumes/build-storage/build/master_iOS-mac/opencv/modules/core/src/alloc.cpp, line 52 OpenCV Error: Assertion failed (u != 0) in create, file /Volumes/build-storage/build/master_iOS-mac/opencv/modules/core/src/matrix.cpp, line 433 libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Volumes/build-storage/build/master_iOS-mac/opencv/modules/core/src/matrix.cpp:433: error: (-215) u != 0 in function create
A couple of questions that I have concerning this topic are:
- Does this function in OpenCV require that amount of RAM as a minimum to run.
- if it does not, is there perhaps a configuration file inside OpenCV that can be changed to reduce the RAM it uses.
- Is there something in code that can be added to reduce the amount of RAM that needs to be allocated?
Thank you in advance.
What is the size of your image?
@Eduardo The average image size would be around 2-4 MB. While running it in Simulator with Instruments or just Xcode Debug navigator open, it shows that the RAM being used is about 6GB. I have tried reducing the quality of image to bring the size down to about 700 kB. This brought the memory usage down about 2GB to ~4GB. However, it still does not run on the iPad and throws that error since it wants to allocate 6.4GB.