(iphone)灰度图像太暗,我可以改变灰度图像的不透明度吗? [英] (iphone) grayscaled image is too dark, can I change the opacity of gray scale image?

查看:297
本文介绍了(iphone)灰度图像太暗,我可以改变灰度图像的不透明度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use the following code to turn color image to grayscale image.

生成的图像为灰色,但太暗。

我可以改变它的不透明度? (不确定术语不透明度是否是正确的词)

The resulting image is gray, but too dark.
Can I change the opacity of it? (not sure if the term "opacity" is the right word for it)

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


推荐答案

只需转换为灰度即可导致图像太暗,因为RGB->灰度转换可能无法保留光度(感知亮度) )图像。您有两种选择:(1)转换为灰度后提亮图像(您可以尝试简单iphone图像处理); (2)转换图像保持亮度。

Just converting to grayscale can sometimes cause your image to be too dark because the RGB->Grayscale conversion may not preserve luminosity (perceived brightness) of the image. You have two options: (1) brighten the image after conversion to greyscale (you could try simple-iphone-image-processing); and (2) convert the image preserving luminosity.

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

One common way to convert from RGB to grayscale while preserving luminosity is to set your gray value (Y) according to the following function:

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

Y = 0.30 x Red + 0.59 x Green + 0.11 x Blue

这样做是因为人类眼睛感知给定的绿色强度比红色或蓝色的相同强度更亮(大约相当)。

This works because the human eye perceives a given intensity of green to be "brighter" than the same intensity of red or blue (in approximately that ratio).

这篇关于(iphone)灰度图像太暗,我可以改变灰度图像的不透明度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆