3

I m the beginner in iphone software development. I developing the application on skin cancer in which i want to calculate or count red color pixel from UIImage which is captured by iphone camera.It is possible to count red pixel from UIImage?

Tirth
  • 7,801
  • 9
  • 55
  • 88
  • See this question: http://stackoverflow.com/questions/448125/how-to-get-pixel-data-from-a-uiimage-cocoa-touch-or-cgimage-core-graphics – Can Berk Güder Feb 26 '10 at 16:39
  • 2
    define "red" `#FF0000` is pure red, but what about `#FF0001`? is that red? what about `#FF0100`? Or `#FE0000`? Or how about `#FF1C41`? That's still reddish... – Dave DeLong Nov 28 '10 at 23:15
  • @ Dave DeLong, thanks for given you me suggestion but i find out the tremendous solution over it. – Tirth Nov 29 '10 at 04:39

1 Answers1

8

Since this is a question that is asked almost weekly, I decided to make a little example project that shows how to do this. You can look at the code at:

http://github.com/st3fan/iphone-experiments/tree/master/Miscellaneous/PixelAccess/

The important bit is the following code, which takes a UIImage and then counts the number of pure red pixels. It is an example and you can use it and modify it for your own algorithms:

/**
 * Structure to keep one pixel in RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA format
 */

struct pixel {
    unsigned char r, g, b, a;
};

/**
 * Process the image and return the number of pure red pixels in it.
 */

- (NSUInteger) processImage: (UIImage*) image
{
    NSUInteger numberOfRedPixels = 0;

    // Allocate a buffer big enough to hold all the pixels

    struct pixel* pixels = (struct pixel*) calloc(1, image.size.width * image.size.height * sizeof(struct pixel));
    if (pixels != nil)
    {
        // Create a new bitmap

        CGContextRef context = CGBitmapContextCreate(
            (void*) pixels,
            image.size.width,
            image.size.height,
            8,
            image.size.width * 4,
            CGImageGetColorSpace(image.CGImage),
            kCGImageAlphaPremultipliedLast
        );

        if (context != NULL)
        {
            // Draw the image in the bitmap

            CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), image.CGImage);

            // Now that we have the image drawn in our own buffer, we can loop over the pixels to
            // process it. This simple case simply counts all pixels that have a pure red component.

            // There are probably more efficient and interesting ways to do this. But the important
            // part is that the pixels buffer can be read directly.

            NSUInteger numberOfPixels = image.size.width * image.size.height;

            while (numberOfPixels > 0) {
                if (pixels->r == 255) {
                    numberOfRedPixels++;
                }
                pixels++;
                numberOfPixels--;
            }

            CGContextRelease(context);
        }

        free(pixels);
    }

    return numberOfRedPixels;
}

A simple example on how to call this:

- (IBAction) processImage
{
    NSUInteger numberOfRedPixels = [self processImage: [UIImage imageNamed: @"DutchFlag.png"]];
    label_.text = [NSString stringWithFormat: @"There are %d red pixels in the image", numberOfRedPixels];
}

The example project on Github contains a complete working example.

Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88
  • Hi St3fan,The above code giving me only constant red pixels(8000) value even if i changed the photo. I changing many images but it giving me same count value. – Tirth Feb 27 '10 at 07:13
  • hi St3fan, from above code i changing the image DutchFlag.png instead of that my own .png image but i getting error(Iphone simulator goes to debugger). – Tirth Mar 02 '10 at 08:43
  • Hi, the above code is also supporting with .jpg or other extensions? – Tirth Mar 02 '10 at 09:05
  • Hi St3fan, I going to make application in which i want to make process camera captured .jpg skin image. Can it work fine with skin image pixels intensities? – Tirth Mar 03 '10 at 12:15
  • Rajendra, the code is just an example on how to access the pixels. You will have to write the correct filters and algorithms for your specific project. – Stefan Arentz Mar 03 '10 at 13:44
  • St3fan, which steps should i have to be follow for RGB color counting from camera captured image from iphone? – Tirth Mar 04 '10 at 06:30
  • 1
    Rajendra, maybe you should read a book about image processing. – Stefan Arentz Mar 04 '10 at 13:54
  • I don't see how this ever worked. The value of `pixels` is being incremented so it cannot be used in the `free(pixels);`. The original pointer returned by `calloc` needs to be used in the `free()`. My solution is to duplicate the original pointer and increment that copy in the loop. Was life different a decade ago? :-) I'm using Xcode 13.2.1 and iOS 15. – Jeff Mar 05 '22 at 21:47