Undefined function 'rgb2gray' : Error using image function

10 views (last 30 days)
Hi all, I am new to Matlab and have student version R2012a. I am trying to convert a color image to black and white. The error and code is listed below.
I appreciate all your help
Regards,
Amit Bothra
Error:
Undefined function 'rgb2gray' for input arguments of type 'uint8'.
Error in Week09_Program1 (line 4) Abw = rgb2gray(A);
My code:
clear all; close all; clc; A = imread('chacha','jpg'); size(A); Abw = rgb2gray(A);
  1 Comment
Jan
Jan on 31 Jan 2013
Edited: Jan on 31 Jan 2013
Btw, omitting the "clear all" is a good idea, because there is no reason to remove all loaded functions from the memory. I assume, anywhere in the universe there is a dwarfish and evil teacher, who instructs the pupils to use clear all, most of all when posting code in a forum, to test when I desperate and give up mentioning this trivial but important detail.
@evil dwarfish teacher: I will be stronger!
@frequent contributors: Sorry that I repeat myself such frequently. It is not my intention to annoy you. I only want to save energy.
I wait for the day when a user finds out, that the code runs faster with this modification:
all = 7.0710678; % <- magic accelerator!
clear all
...

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 30 Jan 2013
Type "ver" on the command line in the command window. Do you see the Image Processing Toolbox listed? Or you can run this code:
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
  2 Comments
Amit bothra
Amit bothra on 31 Jan 2013
Thanks for your help.
when i run the code i dont see any message on the Command window. However in the workspace hasIPT value is 1.
Kindly guide.
ver command output:
------------------------------------------------------------------------------------------------ MATLAB Version: 7.14.0.739 (R2012a) MATLAB License Number: STUDENT Operating System: Microsoft Windows 7 Version 6.1 (Build 7601: Service Pack 1) Java Version: Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot™ Client VM mixed mode ------------------------------------------------------------------------------------------------ MATLAB Version 7.14 (R2012a) Simulink Version 7.9 (R2012a) Control System Toolbox Version 9.3 (R2012a) Curve Fitting Toolbox Version 3.2.1 (R2012a)
Image Analyst
Image Analyst on 31 Jan 2013
You do not have the Image Processing Toolbox so you don't have the rgb2gray() function. You can use the formula Jan gave as a replacement.

Sign in to comment.


Jan
Jan on 30 Jan 2013
Edited: Jan on 30 Jan 2013
If you do not have the Image Processing Toolbox, you find e.g. this in the FileExchange: http://www.mathworks.de/matlabcentral/fileexchange/29392-rgb2gray.
But when you are in the net already, why not asking your favorite internet serach engine? You find even more useful source codes. E.g.:
GrayImg = uint8(0.2989 * Img(:,:,1) + 0.5870 * Img(:,:,2) + ...
0.1140 * Img(:,:,3));

Categories

Find more on Image Processing Toolbox 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!