attempt to execute rgb2gray but showing error

1 view (last 30 days)
Here's my code
clc; %rgb2gray
a=imread('2.jpg');
c=rgb2gray(a);
f=double(c);
[row, col, dim]=size(f);
b=255;
g=b-c;
subplot(2,2,1);
imshow(a);
title('original image');
subplot(2,2,2);
imshow(c);
title('rgb2gray');
subplot(2,2,3);
colormap(gray);
imshow(g);
title('negative image');
The error message is :
Error in rg (line 3)
c=rgb2gray(a);
  1 Comment
Steven Lord
Steven Lord on 15 Apr 2019
That's not the full text of the error message. Show us all the text displayed in red.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 15 Apr 2019
Replace this:
a=imread('2.jpg');
c=rgb2gray(a);
f=double(c);
[row, col, dim]=size(f);
with this:
a = imread('2.jpg');
[row, col, numberOfColorChannels] = size(a)
if numberOfColorChannels == 3
% Convert only if color
grayImage = rgb2gray(a);
else
grayImage = a;
end
f=double(grayImage);

Community Treasure Hunt

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

Start Hunting!