简体   繁体   English

将灰度uint8图像转换为RGB uint8图像

[英]Convert grayscale uint8 image to RGB uint8 image

I have a code to run, which uses an original image and a mask image . 我要运行的代码使用原始图像蒙版图像 The code assumes that the original image is RGB, but my original image is gray scale. 该代码假定原始图像是RGB,但是我的原始图像是灰度。 This must be the result of the MATLAB whos command when I run the code: 这一定是我运行代码时MATLAB whos命令的结果:

Name           Size                Bytes  Class      Attributes

mask         308x206               63448  logical              
origImg      308x206x3            190344  uint8                

The mask is produced by making part of the image white and the rest is black (in a simple software like windows paint). 遮罩是通过将图像的一部分变成白色而其余部分是黑色(在像Windows paint这样的简单软件中)来制作的。

I want to use a gray-scale image as the origImg and produce the mask from the origImg in windows paint, but the result of the MATLAB whos command is as follows when I want to use custom photos with attributes as I said: 我想用一个灰度图像作为origImg并产生从面具origImg在Windows画图,但MATLAB的结果whos命令如下时,我想,我说要使用自定义的照片属性:

Name           Size                Bytes  Class    Attributes

mask         490x640x3            940800  uint8              
origImg      490x640              313600  uint8              

I have to convert the origImage dimension to x3 and remove the x3 from the mask, and also convert its class from unit8 to logical . 我必须将origImage维度转换为x3并从蒙版中删除x3 ,并将其类从unit8转换为logical In that case, I think that the code should work properly. 在这种情况下,我认为代码应该可以正常工作。

What should I do here in order to prepare the origImg and mask for that goal? 为了达到这个origImg ,我应该怎么做才能准备好origImgmask

origImg=imread('G:\the_path\to\my_custom\image.png');
mask=imread('G:\the_path\to\my_custom\image_mask.png');
% I have to do something here to make it work.
whos;
% Rest of the code...

I am not sure if I understand you correctly. 我不确定我是否正确理解您。

To make a RGB image out of a gray-scale image, which still shows up as a gray-scale image, you can use 要从仍显示为灰度图像的灰度图像制作RGB图像,可以使用

origImg = repmat(origImg,1,1,3);

which just repeats your gray-scale image for every channel of the RGB image. 只需为RGB图像的每个通道重复灰度图像。

For the mask, you have to do the opposite. 对于口罩,您必须做相反的事情。 since I don't know your image_mask.png file, I assume that it is a RGB image that uses only black and white. 由于我不知道您的image_mask.png文件,因此我假设它是仅使用黑白的RGB图像。 In this case, all three channels are the same and you could simply use one of them for the mask, doesn't matter which one: 在这种情况下,所有三个通道都相同,您可以简单地将其中之一用作遮罩,而与哪一个无关紧要:

mask = mask(:,:,1);

To convert it to logical, use 要将其转换为逻辑,请使用

mask=logical(mask);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM