BloodAxe/OpenCV-Tutorial

getting memory warning

kalpesh22m opened this issue · 2 comments

I am using opencv framework for flood fill . It works perfectly but after some time getting memory warning ..

I am testing my app for leakages. I notice that my app grows by 3 to 4 mb each time I run my opencv functions.

Code For IPL image to UIImage:

  • (UIImage )UIImageFromIplImage:(IplImage)image {

CGColorSpaceRef colorSpace;

if (image->nChannels == 1) {
colorSpace = CGColorSpaceCreateDeviceGray();
}
else {
colorSpace = CGColorSpaceCreateDeviceRGB();
cvCvtColor(image, image, CV_BGR2RGB);
}

NSData *data = [NSData dataWithBytes:image->imageData length:image->imageSize];
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
CGImageRef imageRef = CGImageCreate(image->width,
image->height,
image->depth,
image->depth * image->nChannels,
image->widthStep,
colorSpace,
kCGImageAlphaNone|kCGBitmapByteOrderDefault,
provider,
NULL,
false,
kCGRenderingIntentDefault
);
UIImage *ret = [UIImage imageWithCGImage:imageRef];

CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);

data=nil;
return ret;
}

Code For UIImage to IPL image

  • (IplImage_)IplImageFromUIImage:(UIImage_)image {

CGImageRef imageRef = image.CGImage;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
IplImage *iplimage = cvCreateImage(cvSize(image.size.width,image.size.height), IPL_DEPTH_8U, 4 );

CGContextRef contextRef = CGBitmapContextCreate(
iplimage->imageData,
iplimage->width,
iplimage->height,
iplimage->depth,
iplimage->widthStep,
colorSpace,
kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault
);

CGContextDrawImage(contextRef,CGRectMake(0, 0, image.size.width, image.size.height),imageRef);

CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);

retImage = cvCreateImage(cvGetSize(iplimage), IPL_DEPTH_8U, 3);
cvCvtColor(iplimage, retImage, CV_RGBA2BGR);
cvReleaseImage(&iplimage);
iplimage =nil;
return retImage;
}

screen shot 2014-12-29 at 1 49 38 pm

Thanks for the report. In fact this tutorial is very old and it needs refactoring to utilize ARC and OpenCV to UIImage conversion that appeared in OpenCV 2.4.9.

ARC has been enabled for OBJ-C types and with a new OpenCV version this issue should be fixed by now.