3

I am looking for a way to get a histogram of an image on the iPhone. The OpenCV library is way too big to be included in my app (OpenCV is about 70MB compiled), but I can use OpenGL. However, I have no idea on how to do either of these.

I have found how to get the pixels of the image, but cannot form a histogram. This seems like it should be simple, but I don't know how to store uint8_t into an array.

Here is the relevant question/answer for finding pixels:

Getting RGB pixel data from CGImage

2 Answers 2

0

The uint8_t* is just a pointer to a c array containing the bytes of the given color, i.e. {r, g, b, a} or whatever the color byte layout is for your image buffer.

So, referencing the link you provided, and the definition of histogram:

//Say we're in the inner loop and we have a given pixel in rgba format
const uint8_t* pixel = &bytes[row * bpr + col * bytes_per_pixel];
//Now save to histogram_counts uint32_t[4] planes r,g,b,a
//or you could just do one for brightness
//If you want to do data besides rgba, use bytes_per_pixel instead of 4
for (int i=0; i<4; i++) {
    //Increment count of pixels with this value
    histogram_counts[i][pixel[i]]++;
}
1
  • How do I define the histogram_counts array? Then how would I output this entire array into an NSString?
    – Flipper
    Aug 9, 2011 at 12:49
0

You can take RGB color of your image with CGRef. Look at the below method which I used for this.

- (UIImage *)processUsingPixels:(UIImage*)inputImage {

// 1. Get the raw pixels of the image
UInt32 * inputPixels;

CGImageRef inputCGImage = [inputImage CGImage];
NSUInteger inputWidth = CGImageGetWidth(inputCGImage);
NSUInteger inputHeight = CGImageGetHeight(inputCGImage);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

NSUInteger bytesPerPixel = 4;
NSUInteger bitsPerComponent = 8;

NSUInteger inputBytesPerRow = bytesPerPixel * inputWidth;

inputPixels = (UInt32 *)calloc(inputHeight * inputWidth, sizeof(UInt32));

CGContextRef context = CGBitmapContextCreate(inputPixels, inputWidth, inputHeight,
                                             bitsPerComponent, inputBytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

// 3. Convert the image to Black & White
for (NSUInteger j = 0; j < inputHeight; j++) {
    for (NSUInteger i = 0; i < inputWidth; i++) {
        UInt32 * currentPixel = inputPixels + (j * inputWidth) + i;
        UInt32 color = *currentPixel;

        // Average of RGB = greyscale
        UInt32 averageColor = (R(color) + G(color) + B(color)) / 3.0;

        *currentPixel = RGBAMake(averageColor, averageColor, averageColor, A(color));
    }
}

// 4. Create a new UIImage
CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage * processedImage = [UIImage imageWithCGImage:newCGImage];

// 5. Cleanup!
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);

   return processedImage;
}

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.