UIImage - automatic level algorithm implementation

I would like to implement the "auto levels" option for UIImage, which I show in my iPhone application. Before I try to implement it myself, I was wondering if there are any image processing methods in the APIs that I should use for histograms, etc. Or should I just grab the base CGImage and process it? (I'm new to the iPhone.)

thanks

IN.

+7
algorithm iphone image-processing
source share
4 answers

One very simple approach is to use the CGImageRef decoding array, but this can only help to display the range (no gamma, etc.).

const CGFloat decode[6] = {blackPoint,whitePoint,blackPoint,whitePoint,blackPoint,whitePoint}; decodedImage = CGImageCreate(CGImageGetWidth(origImage), CGImageGetHeight(origImage), CGImageGetBitsPerComponent(origImage), CGImageGetBitsPerPixel(origImage), CGImageGetBytesPerRow(origImage), CGImageGetColorSpace(origImage), CGImageGetBitmapInfo(origImage), CGImageGetDataProvider(origImage), decode, YES, CGImageGetRenderingIntent(origImage) ); 

Where whitePoint is a float between 0.0 and 1.0, which determines which brightness will be displayed on pure white at the output, and blackPoint is also a float, which determines what brightness is displayed on pure black.

The elements of the decoding array correspond to the components of the color space, so this code will only work for RBG images. You can set the components to different white and black values ​​to create simple color grading.

You can calculate whitePoint and blackPoint with the following function (without color correction):

 void CalculateAutocorretionValues(CGImageRef image, CGFloat *whitePoint, CGFloat *blackPoint) { UInt8* imageData = malloc(100 * 100 * 4); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef ctx = CGBitmapContextCreate(imageData, 100, 100, 8, 4 * 100, colorSpace, kCGImageAlphaNoneSkipLast); CGColorSpaceRelease(colorSpace); CGContextDrawImage(ctx, CGRectMake(0, 0, 100, 100), image); int histogramm[256]; bzero(histogramm, 256 * sizeof(int)); for (int i = 0; i < 100 * 100 * 4; i += 4) { UInt8 value = (imageData[i] + imageData[i+1] + imageData[i+2]) / 3; histogramm[value]++; } CGContextRelease(ctx); free(imageData); int black = 0; int counter = 0; // count up to 200 (2%) values from the black side of the histogramm to find the black point while ((counter < 200) && (black < 256)) { counter += histogramm[black]; black ++; } int white = 255; counter = 0; // count up to 200 (2%) values from the white side of the histogramm to find the white point while ((counter < 200) && (white > 0)) { counter += histogramm[white]; white --; } *blackPoint = 0.0 - (black / 256.0); *whitePoint = 1.0 + ((255-white) / 256.0); } 
+7
source share

I do not think that there are existing APIs that perform image processing, but I am sure that there are third-party libraries there.

+1
source share

This explains automatic levels (e.g. histogram alignment):

http://www.generation5.org/content/2004/histogramEqualization.asp

They also have a Java implementation.

+1
source share

You can try the CImg library ... They have a histogram and align functions on docs . The equalize () function has pretty decent results, starting with a sample image. LGPL license, which may not be suitable for your project, but compatible with sale in the application store.

0
source share

All Articles