why do i get error when i try to use rgb2gray function in gui?

i tried to make a GUI which converts a rgb image to grayscale image.
this is my code:
clc
clear all
ext='*.jpeg';
folder='C:\Users\SAGARIKA\Desktop\butter\butterfly2';
image = uigetfile([folder '\' ext])
old_image = fullfile(folder,image)
copyfile(old_image,['C:\Users\SAGARIKA\Desktop\butter\' image]);
imshow(old_image);
I = rgb2gray(old_image);
i got the following error:
Error using rgb2gray>parse_inputs (line 81)
MAP must be a m x 3 array.
Error in rgb2gray (line 35)
X = parse_inputs(varargin{:});
Error in que (line 9)
I = rgb2gray(old_image);
>>

 Accepted Answer

sagarika - first, do not name local variables as image as this conflicts with the built-in MATLAB function with the same name (see image for details).
Now, look at the line of code that is generating the error
I = rgb2gray(old_image);
where old_image is the path (to) and the filename of the image that you wish to convert. According to rgb2gray, the input to this function is either a true-colour (RGB) image specified as a 3D array, or it is a mx3 map. In either case, it isn't a string.
I think that what you want to do instead is something like
myImage = imread(old_image);
imshow(myImage);
myImageAsGray = rgb2gray(myImage);
Note that the confusion may be because imshow does allow you to pass in the path and filename of the image you wish to load. Unfortunately, rgb2gray behaves differently.

1 Comment

rgb2gray() will throw an error if you pass it a grayscale image, so to avoid potential problems, I'd make it more robust like this:
[rows, columns, numberOfColorChannels] = size(myImage);
if numberOfColorChannels > 1
myImageAsGray = rgb2gray(myImage);
end
Or you could popup a warning message with uiwait(warndlg()) if you want to warn the user and get instructions on what to do.

Sign in to comment.

More Answers (1)

I have this code :
I1 = gpuArray(reshape(linspace(0,1,20),[5 4])); %performs the conversion on a GPU.
W = gather (I1);
[I1,map] = imread(fullname);
imshow(I1,map)
myImageAsGray = rgb2gray(map)
%graycomatrix ignores pixel pairs if either of the pixels contains a NaN,
%replaces positive Infs with the value NumLevels,
%replaces negative Infs with the value 1.
%ignores border pixels, if the corresponding neighbor pixel falls outside the image boundaries
graycomatrix(I1, 'offset', [0 1], 'Symmetric', true)
graycomatrix(I1, 'offset', [0 1], 'Symmetric', false)
graycomatrix(I1, 'offset', [0 -1], 'Symmetric', false)
imshow(myImageAsGray)
imagesc(myImageAsGray)
and the same critical issue!
Error using rgb2gray>parse_inputs (line 77)
MAP must be a m x 3 array.
Error in rgb2gray (line 52)
[X, threeD] = parse_inputs(X);
Error in CoC>pushbutton10_Callback (line 634)
myImageAsGray = rgb2gray(map)
I'm able to see the image in grayscale without problem,but with this error message! I tried to add the code that you suggested but the error message is the same.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!