how to convert grey image to RGB

조회 수: 7 (최근 30일)
mohd akmal masud
mohd akmal masud 2021년 1월 6일
편집: Image Analyst 2021년 1월 9일
hi All,
i have 60 slice image SPECT in grey scale. How to convert all to RGB?
Below is my coding.
P = zeros(103, 103, 60);
for K = 1 : 60
K_file=30+10*K;
petname = sprintf('I%d.dcm', K_file);
P(:,:,K) = dicomread(petname);
scale = 130/103 ;
Pi(:,:,K) = imresize(P(:,:,K),scale) ; % where P is your 103*103 3D matrix
end
rgbImage = gray2rgb(Pi);
imshow3D(rgbImage)
this is my function
function [Image]=gray2rgb(Image)
%Gives a grayscale image an extra dimension
%in order to use color within it
[m n]=size(Image);
rgb=zeros(m,n,3);
rgb(:,:,1)=Image;
rgb(:,:,2)=rgb(:,:,1);
rgb(:,:,3)=rgb(:,:,1);
Image=rgb/255;
end
i got error
Unable to perform assignment because the size of the left side is 130-by-7800 and the size of
the right side is 130-by-130-by-60.
Error in gray2rgb (line 6)
rgb(:,:,1)=Image;
Error in sliderspect1 (line 12)
rgbImage = gray2rgb(Pi);
I DONT KNOW HOW COME LEFT SIDE-BY-7800...

채택된 답변

Image Analyst
Image Analyst 2021년 1월 7일
This is wrong:
[m n]=size(Image);
Why? See Steve's blog:
It should be
[rows, columns, numberOfSlices] = size(Image);
but don't use Image as the name of your variable since that's a built-in function (almost). Also, the pet/spect image is already grayscale. You shouldn't need to convert to gray scale.
Also, you did not make a color image, you made a volumetric image with 60 gray scale slices in it.
I'm not sure what you're trying to do, but what you did doesn't make sense to me.
If you're trying to make a volumetric image with 60 RGB slices in it instead of 60 gray scale images, you'll need a 4-D image where the 4th dimension is the slice number and the third dimension is the color channel (1, 2, or 3).
  댓글 수: 2
mohd akmal masud
mohd akmal masud 2021년 1월 9일
actually i want the grey image convert to RGB image.
as I attached, grey image and RGB. RGB come from grey image.
Image Analyst
Image Analyst 2021년 1월 9일
편집: Image Analyst 2021년 1월 9일
You can do
grayImage = rgb2gray(rgbImage);
but it might look weird unless you have the colormap they applied to the gray image to get that rgb image. If you have that colormap, you can do
grayImage = rgb2ind(rgbImage, colorMap);
and this will of course be a lot better and actually accurate, unlike the first way.
In the future, please start a new question for something like this.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by