views:

23

answers:

0

I have been wondering for quite some time if there is a way to find the locations of all the pixels that match a certain pixel set in a UIImage.

What I have so far is a simple code to find the RGB8 values for the pixel I have selected:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:displayed]; //NSLog(@"%f,%f",location.x,location.y);

// First get the image into your data buffer CGImageRef imageRef = [imageView CGImage]; NSUInteger width = CGImageGetWidth(imageRef); NSUInteger height = CGImageGetHeight(imageRef); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); unsigned char *rawData = malloc(height * width * 4); NSUInteger bytesPerPixel = 4; NSUInteger bytesPerRow = bytesPerPixel * width; NSUInteger bitsPerComponent = 8; CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(colorSpace);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

// Now your rawData contains the image data in the RGBA8888 pixel format. int byteIndex = (bytesPerRow * location.x) + location.y * bytesPerPixel;

    CGFloat red   = (rawData[byteIndex]     * 1.0);
    CGFloat green = (rawData[byteIndex + 1] * 1.0);
    CGFloat blue  = (rawData[byteIndex + 2] * 1.0);
    CGFloat alpha = (rawData[byteIndex + 3] * 1.0);
    byteIndex += 4;

   // UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
NSLog(@"%f, %f, %f, %f",red, green, blue, alpha);

Now what I want to do is scan the image for all pixel with the same RGB8 Values or even better with similar values and then find their locations (x,y).

If anyone knows I'd greatly appreciate any help. I will feature the name inside my application if it ever makes it to the app store :/