0

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?

9
  • 1
    Could you show some code and the output result or error that you're getting? Jan 11, 2013 at 13:00
  • I'm not getting any errors as such, I'm just trying to find a way to solve the problem. The code is on the link I posted if you're interested. Jan 11, 2013 at 13:09
  • Why isn't it working? What is going wrong? What have you tried? Jan 11, 2013 at 14:21
  • 1
    For one thing, the above code doesn't match the example you originally linked. You never create a bitmap context, draw into that context, or read the data from that context, as they do in their example. You're trying to directly pass a CGImageRef into a function that takes a CFDataRef, which isn't going to work. You have to follow the letter of that example, or look at the examples here: stackoverflow.com/questions/3284185/get-pixel-color-of-uiimage . As a warning, the most highly-voted answer there assumes a particular pixel format, which might not be the case in your image.
    – Brad Larson
    Jan 11, 2013 at 15:30
  • 1
    @TonyHematite - Right, but you're still trying to pass in a CGImage to a function that takes CFData. They are completely different types of objects, which is why this is failing. You need to first extract the CFData from your CGImage, either by redrawing the CGImage into a bitmap context (the safest way) or by using the CGDataProviderCopyData() line in the question you linked.
    – Brad Larson
    Jan 11, 2013 at 19:03

1 Answer 1

2

After a little debugging I solved the problem.

Here's my code for coordinates of darker pixels:

 CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
    int myWidth = CGImageGetWidth(image.CGImage);
    int myHeight = CGImageGetHeight(image.CGImage);
    const UInt8 *pixels = CFDataGetBytePtr(pixelData);
    UInt8 blackThreshold = 10 ;
    //UInt8 alphaThreshold = 100;
    int bytesPerPixel_ = 4;
    for(int x = 0; x < myWidth; x++)
    {
        for(int y = 0; y < myHeight; y++)
        {
            int pixelStartIndex = (x + (y * myWidth)) * bytesPerPixel_;
            UInt8 alphaVal = pixels[pixelStartIndex];
            UInt8 redVal = pixels[pixelStartIndex + 1];
            UInt8 greenVal = pixels[pixelStartIndex + 2];
            UInt8 blueVal = pixels[pixelStartIndex + 3];

            if(redVal < blackThreshold || blueVal < blackThreshold || greenVal < blackThreshold || alphaVal < blackThreshold)
            {
                NSLog(@"x %d, y = %d", x, y);
            }
        }
    }

Thanks for the help guys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.