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

4
我使用以下代码将彩色图像转换为灰度图像。
生成的图像是灰色的,但太暗了。 我可以更改它的不透明度吗?(不确定“opacity”一词是否正确)
+ (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

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

保持亮度的一种常见方法是根据以下函数设置灰度值(Y):

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

这有效是因为人眼感知到相同强度的绿色比红色或蓝色更“明亮”(大约按照该比例)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接