Tuesday, March 21, 2017

Open CV memory stacked (not released properly)

Leave a Comment

I am using 3rd party library for image processing, this method seems to be the cause of large memory usage (+30MB) everytime it executed, and it won't release properly. Repeated use of it ends up crashing the app (memory overload).

+ (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat {     NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize() * cvMat.total()];      CGColorSpaceRef colorSpace;      if (cvMat.elemSize() == 1) {         colorSpace = CGColorSpaceCreateDeviceGray();     } else {         colorSpace = CGColorSpaceCreateDeviceRGB();     }      CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);      CGImageRef imageRef = CGImageCreate(cvMat.cols,                                     // Width                                         cvMat.rows,                                     // Height                                         8,                                              // Bits per component                                         8 * cvMat.elemSize(),                           // Bits per pixel                                         cvMat.step[0],                                  // Bytes per row                                         colorSpace,                                     // Colorspace                                         kCGImageAlphaNone | kCGBitmapByteOrderDefault,  // Bitmap info flags                                         provider,                                       // CGDataProviderRef                                         NULL,                                           // Decode                                         false,                                          // Should interpolate                                         kCGRenderingIntentDefault);                     // Intent      // UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];     UIImage *image = [UIImage imageWithCGImage:imageRef];      CGImageRelease(imageRef);     CGDataProviderRelease(provider);     CGColorSpaceRelease(colorSpace);      return image; } 

I suspect the problem is here: (__bridge CFDataRef)data. I cant use CFRelease on it cause it make app crash. Project is running with ARC.

EDIT:

It seems the same code is also in openCV official website: http://docs.opencv.org/2.4/doc/tutorials/ios/image_manipulation/image_manipulation.html

Gah!

EDIT 2 Here is the code how I use it (actually below code is also a part of the 3rd party lib, but i added some lines).

 cv::Mat undistorted = cv::Mat( cvSize(maxWidth,maxHeight), CV_8UC4); // here nothing         cv::Mat original = [MMOpenCVHelper cvMatFromUIImage:_adjustedImage]; // here +30MB          //NSLog(@"%f %f %f %f",ptBottomLeft.x,ptBottomRight.x,ptTopRight.x,ptTopLeft.x);         cv::warpPerspective(original, undistorted,                             cv::getPerspectiveTransform(src, dst), cvSize(maxWidth, maxHeight)); // here +16MB (PROBLEM)            _cropRect.hidden=YES;          @autoreleasepool {             _sourceImageView.image=[MMOpenCVHelper UIImageFromCVMat:undistorted]; // here +15MB (PROBLEM)         }                   original.release(); // here -30MB (THIS IS OK)         undistorted.release(); // here nothing 

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment