MATLAB Answers

Grayscaled Images to RGB

242 ビュー (過去 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 件のコメント
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 件のコメント
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.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by