将灰度图像转换为RGB [英] Convert gray image to RGB

查看:133
本文介绍了将灰度图像转换为RGB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将灰度图像转换为RGB图像,以便保留其原始颜色?
我尝试了几件事,但是它无法实现完美的恢复.

How do I convert a gray image to RGB image so as it can retain its original colors ?
I tried few things but it does not give the perfect recovery

推荐答案

在不了解其他图像的情况下,不可能实现完美的恢复.考虑一下这个等式:
It is impossible to give the perfect recovery without knowing something else about the image. Think about this equation:
y = r + g + b;


在给定"r","g"和"b"值的情况下,很容易找出"y"……只要将它们插入,就可以得到"y"的值.但是,假设您只有"y",您如何计算"r","g"和"b"的值?您不能因为有多种解决方案.假设y = 10,这是一些可能的解决方案:


Easy enough to figure out "y" given the values of "r", "g", and "b"... you just plug them in and you get the value of "y". However, assuming you only have "y", how would you figure out the values of "r", "g", and "b"? You can''t because there are multiple solutions. Assuming y = 10, here are some possible solutions:

r = 2, g = 6, b = 2
r = 6, g = 2, b = 2
r = 3, g = 3, b = 4
...


在灰度("y")和颜色("r","g"和"b")之间进行转换存在相同的问题.您可以转换为灰度,但不能完全转换回颜色.


Converting between grayscale (the "y") and color (the "r", "g", and "b") has the same problem. You can convert to grayscale, but you can''t convert back to color perfectly.


实际上,现在我已经考虑了,如果您可以控制转换,就可以执行此操作图像变为灰度.如果您的灰度图像具有比彩色图像更高的色深,则可以将颜色信息存储在灰度图像的最低有效位中.

因此,如果您有24位彩色图像要转换为32位灰度图像,则可以将灰度信息存储在最高位字节(8位)中,并存储红色,绿色和蓝色.最低顺序为3个字节(24位).当您转换回颜色时,您仅从3个最低顺序字节中获取颜色信息.尽管这可能会使灰度图像看起来有点粗糙...不确定,您必须对其进行测试.

同样,如果您有16位彩色图像要转换为24位灰度图像,则可以将1个字节用于灰度信息,将其他2个字节用于颜色信息.

某些图像格式允许您将其他信息与图像一起存储.因此,您可以将颜色信息存储在图像的隐藏信息中,而实际图像信息将存储灰度信息.例如,我非常确定JPEG可以存储与实际图像不同的缩略图...您可以制作彩色的全尺寸缩略图.此外,BMP具有标头信息,该信息指示有多少文件将专用于实际图像.如果将信息存储在文件末尾(例如颜色信息),某些图像查看程序可能会忽略这些信息(不幸的是,其他程序可能会认为BMP损坏而无法显示).

但是,如果您无法控制将图像转换为灰度图像,那么您将很不走运.
Actually, now that I think about it, you can sort of do this if you have control over converting the images to grayscale. If you have a grayscale image that is a higher color depth than the color image, then you can store the color information in the least significant bits of the grayscale image.

So, if you have a 24-bit color image you want to convert to a 32-bit grayscale image, you can store the grayscale information in the highest order byte (8-bits) and store the red, green, and blue I the lowest order 3 bytes (24-bits). When you convert back to color, you just get the color information from the 3 lowest order bytes. This might make the grayscale image look a little grainy though... not sure, you''d have to test it.

Likewise, if you have a 16-bit color image you want to convert to a 24-bit grayscale image, you can use 1 byte for the grayscale information and the other 2 bytes for the color information.

Some image formats allow you to store additional information with the image. So, you could store the color information in the hidden information for the image but the real image information would store the grayscale information. For example, I''m pretty sure JPEG''s can store a thumbnail different than the actual image... you could make a full-size thumbnail that is in color. Also, BMP''s have header information that indicate how much of the file is going to be dedicated to the actual image. If you store information at the end of the file (e.g., the color information), that might get ignored by some image viewing programs (others, unfortunately, might think the BMP is corrupt and not display it).

But if you don''t have control over converting the images to grayscale, you''re pretty much out of luck.


这篇关于将灰度图像转换为RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆