【问题标题】:color balance on the iPhoneiPhone上的色彩平衡
【发布时间】:2011-07-21 19:27:35
【问题描述】:

我正在拍摄一张图片,通过屏幕上下文加载它,然后逐个像素地更改它。我对图像应用了许多不同的滤镜,但我需要做的最后一件事是改变色彩平衡(类似于 Photoshop)以使红色更蓝。

下面的代码显示了我是如何拍摄图像、获取数据以及逐像素检查 r/g/b 值的:

CGImageRef sourceImage = theImage.image.CGImage;

CFDataRef theData;
theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));

UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(theData);

int dataLength = CFDataGetLength(theData);


int red = 0;
int green = 1;
int blue = 2;


for (int index = 0; index < dataLength; index += 4) {

    int r = pixelData[index + red];
    int g = pixelData[index + green];
    int b = pixelData[index + blue];

    // the color balancing would go here...

    if (r < 0) r = 0;
    if (g < 0) g = 0;
    if (b < 0) b = 0;

    if (r > 255) r = 255;
    if (g > 255) g = 255;
    if (b > 255) b = 255;

    pixelData[index + red] = r;
    pixelData[index + green] = g;
    pixelData[index + blue] = b;

}



CGContextRef context;
context = CGBitmapContextCreate(pixelData,
                                CGImageGetWidth(sourceImage),
                                CGImageGetHeight(sourceImage),
                                8,
                                CGImageGetBytesPerRow(sourceImage),
                                CGImageGetColorSpace(sourceImage),
                                kCGImageAlphaPremultipliedLast);

CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newCGImage];

CGContextRelease(context);
CFRelease(theData);
CGImageRelease(newCGImage);

theImage.image = newImage;

我正在对像素数据进行许多其他操作(设置色阶、去饱和度),但我需要将红色值移向青色。

在 Photoshop 中,我可以通过:

图像:调整:色彩平衡 并将其设置为 -30 0 0

我找不到解释如何执行这种颜色偏移的算法或示例。我试图从每个红色值中减去 30,或者将硬最大值设置为 255 - 30 (225),但那些似乎会剪裁颜色而不是改变值......现在,我只是在乱搞,但是指向引用的指针会很有帮助。

(注意:我无法使用 OpenGL 解决方案,因为我必须采用相同的算法并将其转换为 PHP/gd 以用于此应用程序的 Web 服务器版本)

【问题讨论】:

    标签: iphone objective-c colors cgimage


    【解决方案1】:

    您需要做的是减去红色,然后校正“亮度”以匹配旧颜色,无论您选择何种亮度度量。像这样的东西应该可以工作:

    // First, calculate the current lightness.
    float oldL = r * 0.30 + g * 0.59 + b * 0.11;
    
    // Adjust the color components. This changes lightness.
    r = r - 30;
    if (r < 0) r = 0;
    
    // Now correct the color back to the old lightness.
    float newL = r * 0.30 + g * 0.59 + b * 0.11;
    if (newL > 0) {
        r = r * oldL / newL;
        g = g * oldL / newL;
        b = b * oldL / newL;
    }
    

    注意,当给定纯红色时,它的行为有些奇怪(它会保持不变)。 OTOH,GIMP(2.6.11 版)在其色彩平衡工具中做同样的事情,所以我不太担心。

    【讨论】:

    • 嗨!例如谢谢!我可以在哪里阅读有关类似公式的更多信息?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 2014-06-12
    • 2015-05-23
    • 2014-02-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多