Undefined function 'rgb2gray' : Error using image function
10 views (last 30 days)
Show older comments
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
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
...
Answers (2)
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
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.
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));
0 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!