MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何在MATLAB中将灰度矩阵转换为RGB矩阵? (https://www.labfans.com/bbs/showthread.php?t=23301)

poster 2019-12-10 20:30

如何在MATLAB中将灰度矩阵转换为RGB矩阵?
 
rgbImage = grayImage / max(max(grayImage)); 要么

rgbImage = grayImage / 255; 以上哪一项是正确的,原因是什么?



[B]回答:[/B]

要将[URL="http://www.mathworks.com/help/images/image-types-in-the-toolbox.html#f14-13941"]灰度图像[/URL]转换为[URL="http://www.mathworks.com/help/images/image-types-in-the-toolbox.html#f14-20224"]RGB图像[/URL] ,必须解决两个问题:
[LIST][*] [URL="http://www.mathworks.com/help/images/image-types-in-the-toolbox.html#f14-13941"]灰度图像[/URL]是2- d,而[URL="http://www.mathworks.com/help/images/image-types-in-the-toolbox.html#f14-20224"]RGB图像[/URL]是3-d,所以你必须复制灰度图像数据三次并连接所有的三个拷贝沿第三维度。[*]图像数据可以存储为许多不同的[URL="https://www.mathworks.com/help/matlab/numeric-types.html"]数据类型[/URL] ,因此您必须进行相应的转换。当存储为[URL="http://www.mathworks.com/help/matlab/ref/double.html"]double[/URL]数据类型时,图像像素值应为0到1范围内的浮点数。当存储为[URL="http://www.mathworks.com/help/matlab/ref/uint8.html"]uint8[/URL]数据类型时,图像像素值应为0到255之间的整数。使用函数[URL="http://www.mathworks.com/help/matlab/ref/class.html"]class[/URL]检查图像矩阵的数据类型。[/LIST]您可能会遇到以下3种典型情况:
[LIST][*]要将[URL="http://www.mathworks.com/help/matlab/ref/uint8.html"]uint8[/URL]或[URL="http://www.mathworks.com/help/matlab/ref/double.html"]double[/URL]灰度图像转换为[I]相同数据类型[/I]的RGB图像,可以使用[URL="http://www.mathworks.com/help/matlab/ref/repmat.html"]repmat[/URL]或[URL="http://www.mathworks.com/help/matlab/ref/cat.html"]cat[/URL]函数:

rgbImage = repmat(grayImage,[1 1 3]); rgbImage = cat(3,grayImage,grayImage,grayImage);[*]要将[URL="http://www.mathworks.com/help/matlab/ref/uint8.html"]uint8[/URL]灰度图像转换为[URL="http://www.mathworks.com/help/matlab/ref/double.html"]double[/URL] RGB图像,应先转换为[URL="http://www.mathworks.com/help/matlab/ref/double.html"]double[/URL] ,然后按255缩放:

rgbImage = repmat(double(grayImage)./255,[1 1 3]);[*]要将[URL="http://www.mathworks.com/help/matlab/ref/double.html"]double[/URL]灰度图像转换为[URL="http://www.mathworks.com/help/matlab/ref/uint8.html"]uint8[/URL] RGB图像,应先缩放255,然后再转换为[URL="http://www.mathworks.com/help/matlab/ref/uint8.html"]uint8[/URL] :

rgbImage = repmat(uint8(255.*grayImage),[1 1 3]);[/LIST]
[url=https://stackoverflow.com/questions/2619668]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 03:00

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.