3
votes

I want to get the locations of the pixels in a UIImage whose color matches a specified color.

For example, I want to find the position(x, y) of the pixels of this color: [UIColor colorWithRed:255 green:0 blue:0 alpha:1].

CGPoint point = getFirstPixelOfColor([UIColor colorWithRed:255 green:0 blue:0 alpha:1].CGColor);
1
You need to write some code for that, start from stackoverflow.com/questions/144250/…Tarek Hallak
I want to find the points which match the specified color, but not to find color of the specified point.Suge
Read @jano's comment down there.Tarek Hallak

1 Answers

2
votes

Add UIImage-Utils to your project. Then get the color of x,y like this:

CGFloat imageWidth = image.size.width;
NSData *imageData = [UIImage bytesFromImage:image]
Byte *bytes = (Byte *)imageData.bytes;
CGFloat red    = bytes[redOffset   (x, y, imageWidth)] / 255.0f;
CGFloat green  = bytes[greenOffset (x, y, imageWidth)] / 255.0f;
CGFloat blue   = bytes[blueOffset  (x, y, imageWidth)] / 255.0f;
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];

Store the x,y of the colors matching the color you are looking for. Repeat for every x,y in the image.