How can I solve this error in converting RGB to grayscale?

1 view (last 30 days)
I am trying to implement an algorithm in computer vision and I want to try it on a set of pictures. The pictures are all in color, but I don't want to deal with that. I want to convert them to grayscale which is enough for testing the algorithm. I found the algorithm here https://sourceforge.net/p/octave/image/ci/default/tree/inst/rgb2gray.m
I am calling the function with:
[name,path]=uigetfile('*.*');
img=imread([path,name]);
[img_x,img_y,nargin]=size(img);
if nargin > 1
im=rgb2gray(img);
else
im=img; % It's already gray.
end
But I got these errors: Error: File: rgb2gray.m
if (iscolormap (rgb))
gray = rgb2gray (rgb)(:, 1) * ones (1, 3);
()-indexing must appear last in an index expression.
and
Error in above code (line 5)
im=rgb2gray(img);
Why am I getting those errors?

Accepted Answer

Walter Roberson
Walter Roberson on 25 Dec 2016
That code is for Octave, not for MATLAB.
rgb2gray is built into the Image Processing toolbox, which is required by the Computer Vision toolbox.
If you do not have the Image Processing toolbox then you can implement using the equation that is documented on the rgb2gray documentation page.
  1 Comment
David Goodmanson
David Goodmanson on 25 Dec 2016
Hello Dihara, somewhere between versions 2014a (no) and 2015a (yes), the rgb2gray function got included into basic Matlab, although that probably will not be of direct help.
You can find the equation Walter is citing quickly by googling up "rgb2gray". For Matlab help I use google quite a bit since a lot of the time it is at least as efficient as going to the Mathworks site.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!