Grayscaled Images to RGB
조회 수: 12 (최근 30일)
이전 댓글 표시
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
채택된 답변
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);
추가 답변 (5개)
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
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
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;
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
2011년 4월 10일
You posted this as a new question (which was the right thing to do); I have answered there.
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
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!