簡體   English   中英

將RGB圖像轉換為灰度圖像並將灰度轉換為RGB圖像?

[英]Convert RGB Image to Grayscale and Grayscale to RGB Image?

我已成功將圖像轉換為灰度圖像,我想將灰度圖像恢復為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;
}

要回答有關conversion的問題,您的灰度代碼:

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

給出關系:

S = 0.3R + 0.59G + 0.11B

RGB轉到S涉及用一個方程式(fine!)解決一個未知數( S )。 轉換回來就像在給定一個方程式的情況下嘗試嘗試三個未知數( RGB )一樣。

進行灰度着色的一種方法是將灰度視為強度,然后設置

R = G = B = S但這不會正確(顯然)恢復您的顏色。 簡而言之,轉換為灰度是不可逆的功能,就像對數字求平方(它是正數還是負數)一樣-信息丟失並且無法檢索。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM