将RGB图像转换为灰度图像,并将灰度图像转换为RGB图像? [英] Convert RGB Image to Grayscale and Grayscale to RGB Image?
问题描述
我已成功将图像转换为灰度图像,我想将灰度图像恢复为RGB图像.请帮忙.预先感谢.
I have successfully converted an Image to Grayscale, I want to revert the Grayscale Image back to RGB Image. Please help. Thanks in advance.
-(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;
}
推荐答案
要回答有关转换的问题,您的灰度代码是
To answer the question about converting, your code for grayscale:
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
).
Go go from RGB
to S
involves solving for one unknown (S
) with one equation (fine!).
To convert back is like trying for three unknowns (RGB
) given one equation which isn't possible.
进行灰度着色的一种方法是将灰度视为强度,然后进行设置
One hack to doing a grayscale colorisation is to consider grayscale as just intensity, and set
R = G = B = S
-但这不会正确(显然)恢复您的颜色.
简而言之,转换为灰度是不可逆的功能,就像对数字进行平方(是正数还是负数)一样-信息丢失并且无法检索.
R = G = B = S
- but this isn't going to restore your colors correctly (obviously).
So in short, conversion to grayscale is an irreversible function, like squaring a number (was it positive or negative?) - information is lost and can't be retrieved.
这篇关于将RGB图像转换为灰度图像,并将灰度图像转换为RGB图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!