将RGB图像转换为灰度图像和将灰度图像转换为RGB图像?

5

我已经成功地将一张图片转换为灰度图像,我想要将这张灰度图像还原成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;
}

2
灰度图像转换为RGB图像 - nicael
我想将灰度图像恢复为原始图像。 - Arvind
3
您应该保存原始图像,不要用新的灰度图像“覆盖”它。当您需要再次使用它时,只需使用原始图像即可。 - dlp
1
如果你能够将灰度图像恢复成彩色,那么你将会成为一个非常富有的人。想象一下所有以黑白方式记录的电影都可以变成彩色。 ;) - BlueVoodoo
2
你不能转换。要还原,只需保存原始图像,就像Mihai Popa所指出的那样。 - nicael
我有原始图像,将会恢复它,只是好奇是否有任何方法!谢谢大家!!! - Arvind
1个回答

7
回答有关“转换”的问题,您的灰度代码:
uint8_t gray = (uint8_t) ((30 * rgbaPixel[RED] + 59 * rgbaPixel[GREEN] + 11 * rgbaPixel[BLUE]) / 100);

给出了以下关系:

S = 0.3R + 0.59G + 0.11B

从 RGB 到 S 的转换只涉及一个未知数(S)和一个方程(没问题!)。然而,从 S 转换回 RGB 像是尝试通过一个方程来求出三个未知数(RGB),这是不可能的。
将彩色图像转换为灰度图像的一种方法是将灰度视为强度,然后设置 R=G=B=S,但是这不会正确还原您的颜色(显然)。因此,将彩色图像转换为灰度图像是不可逆的函数,就像对数字平方一样(它是正的还是负的?)——信息丢失不能恢复。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接