Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

how to convert gray scale image into rgb image?

Asked by ug on 1 Apr 2013
Latest activity Commented on by poongothai rajan on 17 Dec 2014 at 6:57

my project is on iridiology...and it requires to convert the gray scale image to an rgb image to find the basic color of the iris. am in need of the code to convert the gray scale image to rgb image.

2 Comments

poongothai rajan on 17 Dec 2014 at 6:56
This comment was flagged by Stephen Cobeldick

clc; close all; clear; %Read Background Image Background=imread('untitled.jpg'); %Read Current Frame CurrentFrame=imread('003.jpg'); %Display Background and Foreground subplot(2,2,1);imshow(Background);title('BackGround'); subplot(2,2,2);imshow(CurrentFrame);title('Current Frame'); %Convert RGB 2 HSV Color conversion [Background_hsv]=round(rgb2hsv(Background)); [CurrentFrame_hsv]=round(rgb2hsv(CurrentFrame)); Out = bitxor(Background_hsv,CurrentFrame_hsv); %Convert RGB 2 GRAY Out=rgb2gray(Out); %Read Rows and Columns of the Image [rows columns]=size(Out); %Convert to Binary Image for i=1:rows for j=1:columns if Out(i,j) >0 BinaryImage(i,j)=1; else BinaryImage(i,j)=0; end end end %Apply Median filter to remove Noise FilteredImage=medfilt2(BinaryImage,[5 5]); %Boundary Label the Filtered Image [L num]=bwlabel(FilteredImage); STATS=regionprops(L,'all'); cc=[]; removed=0; %Remove the noisy regions for i=1:num dd=STATS(i).Area; if (dd < 500) L(L==i)=0; removed = removed + 1; num=num-1; else end end [L2 num2]=bwlabel(L); % Trace region boundaries in a binary image. [B,L,N,A] = bwboundaries(L2); %Display results subplot(2,2,3), imshow(L2);title('BackGround Detected'); subplot(2,2,4), imshow(L2);title('Blob Detected'); hold on; for k=1:length(B), if(~sum(A(k,:))) boundary = B{k}; % plot(boundary(:,2), boundary(:,1), 'r','LineWidth',2); for l=find(A(:,k))' boundary = B{l}; plot(boundary(:,2), boundary(:,1), 'g','LineWidth',2); end end end

i am taking this is for my reference.from the result came from this i want to get only the region of (or)boundary of object.how to do it

poongothai rajan on 17 Dec 2014 at 6:57
This comment was flagged by Stephen Cobeldick

i am taking this is for my reference.from the result came from this i want to get only the region of (or)boundary of object.how to do it

clc; close all; clear; %Read Background Image Background=imread('untitled.jpg'); %Read Current Frame CurrentFrame=imread('003.jpg'); %Display Background and Foreground subplot(2,2,1);imshow(Background);title('BackGround'); subplot(2,2,2);imshow(CurrentFrame);title('Current Frame'); %Convert RGB 2 HSV Color conversion [Background_hsv]=round(rgb2hsv(Background)); [CurrentFrame_hsv]=round(rgb2hsv(CurrentFrame)); Out = bitxor(Background_hsv,CurrentFrame_hsv); %Convert RGB 2 GRAY Out=rgb2gray(Out); %Read Rows and Columns of the Image [rows columns]=size(Out); %Convert to Binary Image for i=1:rows for j=1:columns if Out(i,j) >0 BinaryImage(i,j)=1; else BinaryImage(i,j)=0; end end end %Apply Median filter to remove Noise FilteredImage=medfilt2(BinaryImage,[5 5]); %Boundary Label the Filtered Image [L num]=bwlabel(FilteredImage); STATS=regionprops(L,'all'); cc=[]; removed=0; %Remove the noisy regions for i=1:num dd=STATS(i).Area; if (dd < 500) L(L==i)=0; removed = removed + 1; num=num-1; else end end [L2 num2]=bwlabel(L); % Trace region boundaries in a binary image. [B,L,N,A] = bwboundaries(L2); %Display results subplot(2,2,3), imshow(L2);title('BackGround Detected'); subplot(2,2,4), imshow(L2);title('Blob Detected'); hold on; for k=1:length(B), if(~sum(A(k,:))) boundary = B{k}; % plot(boundary(:,2), boundary(:,1), 'r','LineWidth',2); for l=find(A(:,k))' boundary = B{l}; plot(boundary(:,2), boundary(:,1), 'g','LineWidth',2); end end end

ug

6 Answers

Answer by Image Analyst on 2 Apr 2013

You ask: "i hav an image of the iris with all extra features like eye lashes n all..i hav to extract only the iris frm tis image. first i select the two circles of the iris..the resultant image is that the coloured part of the iris is converted to gray scale n the rest is blacked out... but i want to get the original colour....is there ne other method to get the colour of the iris?"

Here's how to do it.

% Make a mask from your circle using poly2mask
% xCircle, yCircle are the coordinates of your circle perimeter.
mask = poly2mask(xCircle, yCircle, rows, columns);
% Now mask the image so that only the original RGB part
% inside the circle remains, and outside the circle is set to black.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask,class(rgbImage)));

0 Comments

Image Analyst
Answer by Ahmed A. Selman on 1 Apr 2013

Use colormap types, since you are interested with a colored region of interest, I think any colormap format suffices. Example:

   close all
   clear 
   clc % just to clear things up
   I=imread('333.jpg');
   R=rgb2gray(I); % It is gray now
   y=colormap(hot(110));
   imwrite(R,y,'rgb.jpg','jpg'); % Re-change it to colored one 
   imshow('rgb.jpg');

5 Comments

Image Analyst on 2 Apr 2013

Please explain why you think it is even possible to recover "the basic color of the iris". Did you snap the original iris image with a color camera or a monochrome camera?

ug on 2 Apr 2013

here is the procedure, i hav an image of the iris with all extra features like eye lashes n all..i hav to extract only the iris frm tis image. first i select the two circles of the iris..the resultant image is that the coloured part of the iris is converted to gray scale n the rest is blacked out... but i want to get the original colour....is there ne other method to get the colour of the iris?

Image Analyst on 2 Apr 2013

Of course. Wish you'd explained that initially. See my new answer.

Ahmed A. Selman
Answer by Mahdi on 1 Apr 2013

Gabriel made a script for that. You can find it here.

1 Comment

ug on 2 Apr 2013

this did nt work too

Mahdi
Answer by Image Analyst on 1 Apr 2013

ug, if your starting image is monochrome, then you cannot determine the original color. You cannot. Not if all you have is the grayscale image. Period.

You can convert a grayscale image into an RGB image using cat():

rgbImage = cat(3, grayImage, grayImage, grayImage);

of course it will look all grayscale even though it is a "true color" RGB image - it's just that all the "colors" are gray. You can apply a pseudocolor lookup table to the gray values, where each gray value gets mapped into some color, to get a multi-colored image, like this:

rgbImage = ind2rgb(grayImage, jet(256));

however the colors are NOT the original colors of the iris as if you had snapped it with a color camera.

2 Comments

ug on 2 Apr 2013

am getting an error in the ind2rgb function... sayin the dimensions don match

Image Analyst on 2 Apr 2013

Like I said before see my new answer ( http://www.mathworks.com/matlabcentral/answers/69368#answer_80694). ind2rgb() doesn't apply anymore. You will not be using ind2rgb. Again, see my new answer above.

Image Analyst
Answer by ug on 1 Apr 2013
 @Ahmed A. Selman
Am getting the following errors:
??? Error: File: C:\MATLAB7\toolbox\images\images\private\checkinput.m Line: 38 Column: 1
Incomplete or misformed expression or statement.

Error in ==> images\private\checkstrs at 26 checkinput(in, 'char', 'row', function_name, variable_name, argument_position);

Error in ==> imlincomb>ParseInputs at 95 output_class = checkstrs(varargin{end}, valid_strings, mfilename, ...

Error in ==> imlincomb at 83 [images, scalars, output_class] = ParseInputs(varargin{:});

Error in ==> rgb2gray at 58 I = imlincomb(coef(1),X(:,1),coef(2),X(:,2),coef(3),X(:,3), ...

Error in ==> trial at 5 R=rgb2gray(I); % It is gray now

6 Comments

Ahmed A. Selman on 2 Apr 2013

True, Analyst, I do agree with the idea that when color is lost it cannot be retained correctly. Thus any color map can do the job mentioned in the question with the same quality, give or take; they all will NOT be correct, but ANY could specifically isolate iris from the rest, at least better than a grayscale image. In fact I do wonder what does (determine the basic color of the iris from gray images) means, I keep reading amazing stuff here. Correctly colored iris image from gray image is, without doubt, not possible unless a true image was used to specify the colormap .. and if true images do exist then why convert them to gray and ask how to retain the color?..

Anyway, industrially colored images do look like garbage, even when using highly sophisticated image software (truth has only one face..) but does it matter if it was using hot or jet or any other map? If there is a difference please specify what kind is better -- new info are useful and appreciated, always. If not, however, then old or new colormaps will be all as crappy as the same, right? :)

Image Analyst on 2 Apr 2013

We're in agreement here. If all you want to do is to colorize it, you can pick whatever colormap you feel looks best or suits your needs - it's totally subjective.

Ahmed A. Selman on 2 Apr 2013

My exact point. Thank you.

ug
Answer by G.Liz on 31 Jul 2013

Does anyone know how to segment the sclera of the eye ?

1 Comment

Walter Roberson on 31 Jul 2013

Please start a new Question for that.

G.Liz

Contact us