MATLAB Answers

how to convert grey image to RGB

44 views (last 30 days)
mohd akmal masud
mohd akmal masud on 9 Jan 2021
Commented: Image Analyst on 11 Jan 2021
Hi all,
i have 60 slice grey image. how want to convert to RGB image?
as attached is my grey image (grey.jpg). rgb.jpg is from internet what i want like.
this is my coding, but got error.
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
BUT 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 THINK MY function gray2rgb wrong.
can some one help me?

  0 Comments

Sign in to comment.

Answers (2)

Jan
Jan on 9 Jan 2021
Your Pi is a [130 x 130 x 60] matrix, but your function gray2rgb() expects a 2D matrix as input. It is not clear how you want to convert the 60 layers of the image data to RGB channels.
Maybe you want:
[r,c,m] = size(Pi);
rgbImage = repmat(reshape(Pi / 255, [r, c, 1, m]), [1, 1, 3, 1]);

  2 Comments

mohd akmal masud
mohd akmal masud on 10 Jan 2021
Hi Jan,
i have change my coding accordingly your suggestion. But my image still grey as picture attached.
clc
clear all
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);
below is my function i changed.
function [Pi]=gray2rgb(Pi)
%Gives a grayscale image an extra dimension
%in order to use color within it
[r,c,m] = size(Pi);
rgbImage = repmat(reshape(Pi / 255, [r, c, 1, m]), [1, 1, 3, 1]);
end
CAN HELP ME?
Jan
Jan on 10 Jan 2021
Yes, of course the image is grey, because the original images contain the grey level information only. If the value of each pixel of the input is used for all 3 RGB channels, the output looks grey also.
Which information do you want to use to define the colors?

Sign in to comment.


Image Analyst
Image Analyst on 10 Jan 2021
Try
function rgbImage = gray2rgb(grayImage)
cmap = jet(256); % Or whatever one you want.
rgbImage = ind2rgb(grayImage, cmap); % Convert gray scale image into an RGB image.

  3 Comments

mohd akmal masud
mohd akmal masud on 11 Jan 2021
Hi Image Analyst,
i have try, but error,
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in matlab.images.internal.ind2rgb (line 24)
rgb(1:height, 1:width, 1) = reshape(cm(indexedImage, 1), [height width]);
Error in ind2rgb (line 17)
r = matlab.images.internal.ind2rgb(a, cm);
Error in gray2rgb (line 5)
rgbImage = ind2rgb(grayImage, cmap); % Convert gray scale image into an RGB image.
Error in sliderspect1 (line 13)
rgbImage = gray2rgb(Pi);
mohd akmal masud
mohd akmal masud on 11 Jan 2021
this is my coding
clc
clear all
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 rgbImage = gray2rgb(Pi)
cmap = jet(256); % Or whatever one you want.
rgbImage = ind2rgb(Pi, cmap); % Convert gray scale image into an RGB image.
Image Analyst
Image Analyst on 11 Jan 2021
To convert a gray scale image to an RGB image, it must have one channel. You have 60 channels, like either it's a hyperspectral image with 60 color channels, or a volumetric image, like from CT or MRI, with 60 slices. Neither of those can be converted into an RGB image. You either need three channels, or one channel, not 60.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by