Grayscaled Images to RGB

조회 수: 12 (최근 30일)
Amjed
Amjed 2011년 1월 25일
댓글: Walter Roberson 2021년 3월 12일
I am working over gray scaled images that would want to specify a single color for there, is there a way to specify which color to convert it to... for example i have a gray scale image that would like to have it go between 0 to 255 RED... is there a way to do that???

채택된 답변

Walter Roberson
Walter Roberson 2011년 1월 25일
If your image array is IM and it is already the datatype that you want, then
red_IM = cast(cat(3, IM, zeros(size(IM)), zeros(size(IM))), class(IM));
green_IM = cast(cat(3, zeros(size(IM)), IM, zeros(size(IM))), class(IM));
blue_IM = cast(cat(3, zeros(size(IM)), zeros(size(IM)), IM), class(IM));
If your grayscale is 0 to 1 and you want to convert it to 0 to 255, then before doing the above, do
IM = uint8(IM * 256);
  댓글 수: 1
Amjed
Amjed 2011년 1월 25일
Thanks Walter, the code works perfectly ;)

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

추가 답변 (5개)

Kenneth Eaton
Kenneth Eaton 2011년 1월 25일

If img is your grayscale image, stored as a 2-D uint8 array, you can create a red version by making img be the red color plane of an RGB image and setting the green and blue color planes to 0. The following uses the functions CAT and ZEROS to do this:

filler = zeros(size(img),'uint8');  % For the green and blue color planes
rgbImage = cat(3,img,filler,filler);  % Make the RGB image

And here's an example:

img = imread('cameraman.tif');
filler = zeros(size(img),'uint8');
rgbImage = cat(3,img,filler,filler);
imshow(rgbImage);
  댓글 수: 3
Paul Wansch
Paul Wansch 2021년 3월 12일
and how i got this pic blue or green?
Walter Roberson
Walter Roberson 2021년 3월 12일
img = imread('cameraman.tif');
filler = zeros(size(img),'uint8');
rgbImageR = cat(3,img,filler,filler);
rgbImageG = cat(3,filler,img,filler);
rgbImageB = cat(3,filler,filler,img);
imshow(rgbImageR);
imshow(rgbImageG);
imshow(rgbImageB);

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


Egon Geerardyn
Egon Geerardyn 2011년 1월 25일
If you just need red, green or blue, you can use the following code:
I = im2double(imread('cameraman.tif')); %image in double format
Icol = repmat(I,[1,1,3]); % just create a grayscale RGB image from it
Icol(:,:,1) = 1; % put red column = 1 everywhere
imshow(Icol);
You can do the same thing for green or blue (just change the 1 in the third line to 2 or 3).
If you want to have generic colors, that should be possible too, but I think the simplest solution is using hsv2rgb() and rgb2hsv(), e.g. like so:
I = im2double(imread('cameraman.tif'))
color = [1 0 1]; %rgb color
colorHSV = rgb2hsv(color);
IHSV = zeros(size(I));
IHSV(:,:,1) = colorHSV(1); %hue (what color)
IHSV(:,:,2) = 1-I; %saturation (how much color)
IHSV(:,:,3) = colorHSV(3); %value (brightness) of your reference
Icol = hsv2rgb(IHSV);
If your image is too dark, you can always put the brightness to 1 in the code: IHSV(:,:,3) = 1;
  댓글 수: 1
Amjed
Amjed 2011년 1월 25일
For the first part of your code... is it a generic red color? ... and does it matter if the grayscale is normalized from 0-255 to 0-1 ?

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


Amjed
Amjed 2011년 1월 25일
Thanks every one ... all of the programs works perfectly, now the thing is i have 3 images, and they are in RED GREEN AND BLUE, how can i combine them into one picture?
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 1월 25일
RGB_IM = cat(3, red_IM(:,:,1), green_IM(:,:,2), blue_IM(:,:,3));
Amjed
Amjed 2011년 1월 25일
Cheers Walter ;) works perfectly ;)

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


Ahmad
Ahmad 2011년 4월 10일
I have a picture in Gray scale ones and zeros
and for some multiplications characteristics to apply functions of convolution i will need to convert it to rgb with a matrix that consist of 3
how can i do gray2rgb? I've tried some defining new function for this that i took from the shared files in this website but didn't work.
mentioning that my version is (MATLAB 7.6.0 r2008a)
I get this message: Function definitions are not permitted at the prompt or in scripts.
Whenever I define this function:
MATLAB code
Image=imread('input.bmp')
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
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 4월 10일
You posted this as a new question (which was the right thing to do); I have answered there.

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


Femi Joy
Femi Joy 2013년 1월 25일
편집: Walter Roberson 2013년 1월 25일
I_origin1 = repmat(double(I_origin)./255,[1 1 3]);
I_origin_yiq(i,:,:,:) = rgb2ntsc(I_origin1);
This is my code, m getting n error wn i run.
??? Error using ==> rgb2ntsc>parse_inputs at 89
RGB image must be an M-by-N-by-3 array.
Error in ==> rgb2ntsc at 29
A = parse_inputs(varargin{:});
Error in ==> Wavelet at 18
I_origin_yiq(i,:,:,:) = rgb2ntsc(I_origin1);
i m nt able to rectify it. Pls help
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 1월 25일
This should have been posted as a new question.
You are passing I_origin1 to rgb2ntsc(), but your I_origin1 is not RGB.

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by