开发者

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

开发者 https://www.devze.com 2023-03-07 05:57 出处:网络
I use the following code to turn color image to grayscale image. The resulting image is gray, but too dark.

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 col开发者_如何学编程orSpace = 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;
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消