Grayscaled Images to RGB
15 views (last 30 days)
Show older comments
Amjed
on 25 Jan 2011
Commented: Walter Roberson
on 12 Mar 2021
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???
0 Comments
Accepted Answer
Walter Roberson
on 25 Jan 2011
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);
More Answers (5)
Kenneth Eaton
on 25 Jan 2011
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);
![](http://i37.photobucket.com/albums/e77/kpeaton/example_gray_to_red.png)
3 Comments
Walter Roberson
on 12 Mar 2021
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
on 25 Jan 2011
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;
Ahmad
on 10 Apr 2011
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 Comment
Walter Roberson
on 10 Apr 2011
You posted this as a new question (which was the right thing to do); I have answered there.
Femi Joy
on 25 Jan 2013
Edited: Walter Roberson
on 25 Jan 2013
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 Comment
Walter Roberson
on 25 Jan 2013
This should have been posted as a new question.
You are passing I_origin1 to rgb2ntsc(), but your I_origin1 is not RGB.
See Also
Categories
Find more on Red in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!