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?

Tony Hematite
  • 149
  • 3
  • 14
  • 1
    Could you show some code and the output result or error that you're getting? – Daniel Martín Jan 11 '13 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. – Tony Hematite Jan 11 '13 at 13:09
  • Why isn't it working? What is going wrong? What have you tried? – Ryan Poolos Jan 11 '13 at 14:21
  • I've updated my question to be a little more informative, showing my current code. The first few lines (UIImage - CGImage) are wrong. – Tony Hematite Jan 11 '13 at 14:35
  • Edited again to outline errors and added more specific issues. – Tony Hematite Jan 11 '13 at 15:19
  • 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: http://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 '13 at 15:30
  • Yep, I changed the approach and tried to show that in my updates. Thanks for for the help brad. – Tony Hematite Jan 11 '13 at 15:35
  • The solution to this code is the one I'm currently trying to adapt. http://stackoverflow.com/questions/8955110/detect-black-pixel-in-image-ios?rq=1 – Tony Hematite Jan 11 '13 at 15:39
  • 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 '13 at 19:03

1 Answers1

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.

Lukas
  • 1,346
  • 7
  • 24
  • 49
Tony Hematite
  • 149
  • 3
  • 14