필터 지우기
필터 지우기

How to convert grayscale to rgb ?

조회 수: 120 (최근 30일)
Ynne
Ynne 2018년 1월 16일
편집: The Anh Vuong 2021년 10월 2일
I used the following script to convert grayscale image to rgb
[im, map] = imread('frame1.jpg');
if(isempty(map)) % image is RGB or grayscale
if(size(im, 3) == 1) % image is grayscale
im = cat(3, im, im, im);
end
else % image is indexed
im = ind2rgb(im, map);
end
with frame1 is grayscale. The problem is that when i execute imshow(im) it still without colors but size(im) is 144 176 3 I'm confused, how can i obtain image with colors ?
  댓글 수: 16
Umer Sajid
Umer Sajid 2021년 7월 7일
Hellow , Hope you are fine. I am looking to convert grayscale image dataset folder to RGB image and then store it into another folder.
My Question is How can i perform this operation Please
Image Analyst
Image Analyst 2021년 7월 8일
@Umer Sajid, use the FAQ:
Inside the loop, create input and output filenames, then use cat() and imwrite():
rgbImage = cat(3, grayImage, grayImage, grayImage);
imwrite(rgbImage, outputFileName);
See full demo attached.

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

답변 (2개)

Image Analyst
Image Analyst 2018년 1월 16일
You can apply various colormaps with ind2rgb():
map = hsv(256); % Or whatever colormap you want.
rgbImage = ind2rgb(im, map); % im is a grayscale or indexed image.
  댓글 수: 9
Walter Roberson
Walter Roberson 2020년 10월 8일
you accidentally created a variable named ind2rgb
Image Analyst
Image Analyst 2020년 10월 8일
Set a breakpoint on that line then when it stops there, type this into the command window and tell us what it says
>> which -all ind2rgb
You should see this:
>> which -all ind2rgb
C:\Program Files\MATLAB\R2020b\toolbox\matlab\images\ind2rgb.m
Or for whatever version of MATLAB you're using. You should not see any other lines that indicate that ind2rgb is a variable. If there are, you did something wrong - something to overwrite the built in ind2rgb() function with your own function or variable.

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


The Anh Vuong
The Anh Vuong 2021년 10월 1일
편집: The Anh Vuong 2021년 10월 1일
Hi,
by using of imges to trained of Network , I have this problem. So I would like to share you a converter of converting automaic gray image to color and working later with im resize function.
Have Fun!
GRAY SCALE TO RGB IMAGE Structure
%filename = "Testimage_gray.PNG" or "Testimage_gray.jpg"
%filename = "Testimage_color.PNG" or "Testimage_color.jpg"
im = imread(filename);
colorscale ="-- color picture"
if(size(im, 3) == 1) % image is grayscale
colorscale ="-- gray picture"
[rgbgray,cmap] = gray2ind(im,256)
im = cat (3,rgbgray,rgbgray, rgbgray)
end
imshow(im)
title(string(filename) + colorscale );
Resize the test image to match the ggogleNET network input size for example [224 224].
I = imresize(im, [224 224]);
imshow(I)
  댓글 수: 4
Walter Roberson
Walter Roberson 2021년 10월 2일
[im, cmap] = imread(filename);
if ~isempty(cmap)
im = ind2rgb(im, cmap);
end
if ndims(im) < 3
im = im(:,:,[1 1 1]);
end
The Anh Vuong
The Anh Vuong 2021년 10월 2일
편집: The Anh Vuong 2021년 10월 2일
I have tested your code. It is working too.
We have now 3 methods to convert gray image to "color gray imager" for processing
Have a fun by programming!

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

카테고리

Help CenterFile Exchange에서 Blue에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by