ios - 将 RGB 图像转换为灰度并将灰度转换为 RGB 图像?

标签 ios objective-c image ios7

我已成功将图像转换为灰度图像,我想将灰度图像恢复为 RGB 图像。请帮忙。提前致谢。

-(UIImage *) toGrayscale
    {

    const int RED = 1;
    const int GREEN = 2;
    const int BLUE = 3;

        Create image rectangle with current image width/height
        CGRect imageRect = CGRectMake(0, 0, self.size.width * self.scale, self.size.height * self.scale);

        int width = imageRect.size.width;
        int height = imageRect.size.height;

        // the pixels will be painted to this array
        uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));

        // clear the pixels so any transparency is preserved
        memset(pixels, 0, width * height * sizeof(uint32_t));

        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

        // create a context with RGBA pixels
        CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace,
                                                     kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);

        // paint the bitmap to our context which will fill in the pixels array
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), [self CGImage]);

        for(int y = 0; y < height; y++) {
            for(int x = 0; x < width; x++) {
                uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];

                // convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
                uint8_t gray = (uint8_t) ((30 * rgbaPixel[RED] + 59 * rgbaPixel[GREEN] + 11 * rgbaPixel[BLUE]) / 100);

                // set the pixels to gray
                rgbaPixel[RED] = gray;
                rgbaPixel[GREEN] = gray;
                rgbaPixel[BLUE] = gray;
            }
        }

        // create a new CGImageRef from our context with the modified pixels
        CGImageRef image = CGBitmapContextCreateImage(context);

        // we're done with the context, color space, and pixels
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        free(pixels);

        // make a new UIImage to return
        UIImage *resultUIImage = [UIImage imageWithCGImage:image
                                                     scale:self.scale
                                               orientation:UIImageOrientationUp];

        // we're done with image now too
        CGImageRelease(image);

        return resultUIImage;
}

最佳答案

要回答关于转换的问题,您的灰度代码:

uint8_t gray = (uint8_t) ((30 * rgbaPixel[RED] + 59 * rgbaPixel[GREEN] + 11 * rgbaPixel[BLUE]) / 100);

给出关系:

S = 0.3R + 0.59G + 0.11B

RGBS 涉及用一个方程求解一个未知数 (S)(很好!)。 转换 就像在给定一个不可能的方程式的情况下尝试三个未知数 (RGB)。

进行灰度着色的一种技巧是将灰度视为强度,并设置

R = G = B = S - 但这不会正确恢复您的颜色(显然)。 所以简而言之,转换为灰度是一个不可逆的函数,就像一个数字的平方(是正数还是负数?)- 信息丢失并且无法检索。

关于ios - 将 RGB 图像转换为灰度并将灰度转换为 RGB 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23133469/

相关文章:

iphone - 如何在 UITabBar 中垂直居中图像

c++ - 问题访问/编辑 Mat 图像中的像素位置

wordpress - 更改 "before and after"WordPress 插件中的图像初始宽度

php - 您在应用程序中使用什么 MySQL 日期/时间类型,为什么?

iphone - 将 youtube channel 加载到 uitableview

java - 如何在java中将16位灰度图像转换为RGB图像?

ios - 转到 UITabController 中的不同选项卡

ios - 在 xcode4 中链接现有项目时出现问题

ios - 是否有 UIImagePickerController 替代品,我可以从已经在弹出窗口中的 Controller 使用?

objective-c - 如何在启用沙盒的助手应用程序中终止我的应用程序?