(iphone) grayscaled image is too dark, can I change the opacity of gray scale image?

自作多情 提交于 2019-12-07 09:41:21

问题


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

回答1:


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.

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 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).



来源:https://stackoverflow.com/questions/6082396/iphone-grayscaled-image-is-too-dark-can-i-change-the-opacity-of-gray-scale-im

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!