I am currently trying to find a way to find some rgba value from a UIImageview in iOS. So for example, I want to find all black 0-0-0
pixels on a white background 1-1-1
and output their co-ordinates.
My current code (adapted from another stackoverflow post):
UIImage *myPicture = [UIImage imageNamed:@"somePicture.png"];
CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(myPicture.CGImage));
myWidth = CGImageGetWidth(myPicture.CGImage);
myHeight = CGImageGetHeight(myPicture.CGImage);
const UInt8 *pixels = CFDataGetBytePtr(pixelData);
UInt8 blackThreshold = 10;
int bytesPerPixel_ = 4;
for( x = 0; x < myWidth; x++)
for( y = 0; y < myHeight; y++)
{
{
int pixelStartIndex = (x + (y * myWidth)) * bytesPerPixel_;
UInt8 redVal = pixels[pixelStartIndex + 1];
UInt8 greenVal = pixels[pixelStartIndex + 2];
UInt8 blueVal = pixels[pixelStartIndex + 3];
if(redVal < blackThreshold && blueVal < blackThreshold && greenVal < blackThreshold) {
NSLog(@"x coordinate = %@", x);
NSLog(@"y coordinate = %@", y);
}
}
}
}
Could someone look at the code and provide some feedback with regards to a possible solution?