MATLAB: How to solve this error in converting RGB to grayscale

colour to grayscalecomputer visiondigital image processingimageimage acquisitionimage processingImage Processing Toolbox

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?

Best Answer

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.