2010-07-19 68 views
7

我想为我的iPhone应用程序中显示的UIImage实现“自动水平”选项。在我尝试自己实现它之前,我想知道是否应该使用API​​中的任何图像处理方法,用于直方图等。或者,我应该只抓取底层的CGImage并处理它? (我是新来的iPhone开发。)UIImage - 实现自动水平算法

感谢

MV

+0

呃,什么是汽车级别? – vodkhang 2010-07-19 06:12:45

+0

“自动色阶”是大多数图像编辑程序(如Photoshop)中的一个选项,可改善图像的色调范围。 – 2010-07-19 08:41:17

+0

这看起来很有希望: http://code.google.com/p/simple-iphone-image-processing/ – 2010-07-28 08:42:12

回答

7

一个非常简单的方法是使用一个CGImageRef的解码阵列,但这只能帮助一个范围映射(无γ,等)

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) 
           ); 

其中白点为0.0和之间的浮1.0,它决定了哪个亮度将映射到输出中的纯白色,并且blackPoint也是一个浮点,它决定哪个亮度映射到纯黑色。

解码阵列的元素对应于色彩空间的组件,因此此代码仅适用于RBG图像。您可以将组件设置为不同的白色和黑色值,以创建简单的颜色校正。

就可以计算出白点以及具有以下功能的黑点(W/O色彩校正):

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); 
} 
1

我不认为有现有的API,做图像处理,但我敢肯定有第三方库那里。

0

你可以尝试CImg图书馆......他们对docshistogramequalize功能。 equalize()函数具有相当不错的结果,来自示例图像。该许可证是LGPL,可能不适合您的项目,但与在应用程序商店销售兼容。