【问题标题】:(iphone) grayscaled image is too dark, can I change the opacity of gray scale image?(iphone) 灰度图像太暗,我可以更改灰度图像的不透明度吗?
【发布时间】:2011-08-30 06:10:27
【问题描述】:

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

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

+ (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;
}

【问题讨论】:

    标签: iphone image grayscale


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 2018-03-27
      • 2012-02-05
      • 2012-11-23
      • 1970-01-01
      • 2012-07-18
      相关资源
      最近更新 更多