Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've been trying to do the same thing a lot of time ago, using just Core Graphics, and it can be done, but trust me, the effect is not as smooth and soft as the user expects to be. So, i knew how to work with OpenCV, (Open Computer Vision Library), and as it was written in C, i knew i could ise it on the iPhone. Doing what you want to do with OpenCV is extremely easy. First you need a couple of functions to convert a UIImage to an IplImage wich is the type used in OpenCV to represent images of all kinds, and the other way.</p> <pre><code>+ (IplImage *)CreateIplImageFromUIImage:(UIImage *)image { CGImageRef imageRef = image.CGImage; //This is the function you use to convert a UIImage -&gt; IplImage CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); IplImage *iplimage = cvCreateImage(cvSize(image.size.width, image.size.height), IPL_DEPTH_8U, 4); CGContextRef contextRef = CGBitmapContextCreate(iplimage-&gt;imageData, iplimage-&gt;width, iplimage-&gt;height, iplimage-&gt;depth, iplimage-&gt;widthStep, colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault); CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), imageRef); CGContextRelease(contextRef); CGColorSpaceRelease(colorSpace); return iplimage;} + (UIImage *)UIImageFromIplImage:(IplImage *)image { //Convert a IplImage -&gt; UIImage CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); NSData * data = [[NSData alloc] initWithBytes:image-&gt;imageData length:image-&gt;imageSize]; //NSData *data = [NSData dataWithBytes:image-&gt;imageData length:image-&gt;imageSize]; CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data); CGImageRef imageRef = CGImageCreate(image-&gt;width, image-&gt;height, image-&gt;depth, image-&gt;depth * image-&gt;nChannels, image-&gt;widthStep, colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault, provider, NULL, false, kCGRenderingIntentDefault); UIImage *ret = [[UIImage alloc] initWithCGImage:imageRef]; CGImageRelease(imageRef); CGDataProviderRelease(provider); CGColorSpaceRelease(colorSpace); [data release]; return ret;} </code></pre> <p>Now that you have both the basic functions you need you can do whatever you want with your IplImage: this is what you want:</p> <pre><code>+(UIImage *)erasePointinUIImage:(IplImage *)image :(CGPoint)point :(int)r{ //r is the radious of the erasing int a = point.x; int b = point.y; int position; int minX,minY,maxX,maxY; minX = (a-r&gt;0)?a-r:0; minY = (b-r&gt;0)?b-r:0; maxX = ((a+r) &lt; (image-&gt;width))? a+r : (image-&gt;width); maxY = ((b+r) &lt; (image-&gt;height))? b+r : (image-&gt;height); for (int i = minX; i &lt; maxX ; i++) { for(int j=minY; j&lt;maxY;j++) { position = ((j-b)*(j-b))+((i-a)*(i-a)); if (position &lt;= r*r) { uchar* ptr =(uchar*)(image-&gt;imageData) + (j*image-&gt;widthStep + i*image-&gt;nChannels); ptr[1] = ptr[2] = ptr[3] = ptr[4] = 0; } } } UIImage * res = [self UIImageFromIplImage:image]; return res;} </code></pre> <p>Sorry for the formatting.</p> <p>If you want to know how to port OpenCV to the iPhone <a href="http://niw.at/articles/2009/03/14/using-opencv-on-iphone/en" rel="nofollow noreferrer">Yoshimasa Niwa's </a></p> <p>If you want to check out an app currently working with OpenCV on the AppStore go get :<a href="http://itunes.apple.com/app/flags-faces/id371891114?mt=8" rel="nofollow noreferrer">Flags&amp;Faces</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload