(iphone)灰度图像太暗,我可以改变灰度图像的不透明度吗?

时间:2011-05-21 14:35:28

标签: iphone image grayscale

我使用以下代码将彩色图像转换为灰度图像。

生成的图像为灰色,但太暗 我可以改变它的不透明度吗? (不确定术语“不透明度”是否适合它)

+ (UIImage *)convertImageToGrayScale: (UIImage*) image
{
    // Create image rectangle with current image width/height                                                                                                                                                                               
    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);

    // Grayscale color space                                                                                                                                                                                                                
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

    // Create bitmap content with current image size and grayscale colorspace                                                                                                                                                               
    CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);

    // Draw image into current context, with specified rectangle                                                                                                                                                                            
    // using previously defined context (with grayscale colorspace)                                                                                                                                                                         
    CGContextDrawImage(context, imageRect, [image CGImage]);

    // Create bitmap image info from pixel data in current context                                                                                                                                                                          
    CGImageRef imageRef = CGBitmapContextCreateImage(context);

    // Create a new UIImage object                                                                                                                                                                                                          
    UIImage *newImage = [UIImage imageWithCGImage:imageRef];

    // Release colorspace, context and bitmap information                                                                                                                                                                                   
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    CFRelease(imageRef);

    // Return the new grayscale image                                                                                                                                                                                                       
    return newImage;
}

1 个答案:

答案 0 :(得分:0)

仅转换为灰度有时会导致图像太暗,因为RGB->灰度转换可能无法保留图像的luminosity(感知亮度)。您有两种选择:(1)转换为灰度后使图像变亮(您可以尝试simple-iphone-image-processing); (2)转换图像保持光度。

在保留亮度的同时从RGB转换为灰度的一种常用方法是根据以下函数设置灰度值(Y):

Y = 0.30 x红色+ 0.59 x绿色+ 0.11 x蓝色

这是有效的,因为人眼感知给定的绿色强度比相同强度的红色或蓝色(大约是那个比例)“更亮”。