how to convert grey image to RGB

조회 수: 408 (최근 30일)
mohd akmal masud
mohd akmal masud 2021년 1월 9일
댓글: Image Analyst 2023년 2월 28일
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?

채택된 답변

Jan
Jan 2021년 1월 9일
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
mohd akmal masud
mohd akmal masud 2021년 1월 10일
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 2021년 1월 10일
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?

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

추가 답변 (2개)

Image Analyst
Image Analyst 2021년 1월 10일
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.
  댓글 수: 8
john
john 2022년 1월 10일
편집: john 2022년 1월 10일
how do i convert grayscale image to same color image but it should be an rbg image?
I tried the above function which u gave. My input was a grayimage with just 0 and 255 values when i used above function and runned the program it got converted to blue and brown. But i want the same original image but it should be an rgb so how do i get it?
Image Analyst
Image Analyst 2022년 1월 10일
@john if you have an image with just 0 and 255, and use ind2rgb() then you will end up with an RGB color image with only two colors - those colors being cmap(1,:) and cmap(255, :), which I guess are ble and brown for the colormap you used. There is no way to get back the original true color RGB image from only a gray scale version of it. as you can imagine, there are nearly an infinite number of color images that could be made from it. You'll just have to save your original RGB image if you want it later in your program.

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


Jamal Nasir
Jamal Nasir 2023년 2월 28일
이동: Image Analyst 2023년 2월 28일
there is no traditional method to convert gray image to color image
this need using machine learning for prediction the pixel value depend on the region in
similar sense
  댓글 수: 1
Image Analyst
Image Analyst 2023년 2월 28일
He has a 60 channel hyperspectral image, as you can see from his code. One can use one of the strategies in the attached paper to convert multiple spectral channels into an RGB image.

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

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by