Problem with rgb2gray function ?
古いコメントを表示
I've made a GUI with GUIDE which makes a RGB image into binary image. I have the following code in one part of the program :
fileName = uigetfile('*.jpg');
imshow(fileName);
I = imread(fileName);
Iinitial = I;
Igray = rgb2gray(Iinitial);
I = imcomplement(I);
Igray = rgb2gray(I);
And it gives me that following error:
??? Error using ==> rgb2gray>parse_inputs at 82
MAP must be a m x 3 array.
Error in ==> rgb2gray at 35
X = parse_inputs(varargin{:});
Error in ==> lets_see>pushbutton1_Callback at 97
Igray = rgb2gray(Iinitial);
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> lets_see at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)lets_see('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
I cannot find the problem and fix it. If somebody knows, please help. Thanks!
2 件のコメント
VYSHAKH V NAIR
2017 年 6 月 1 日
may be this is because you are choosing a gray scale image as input.and trying again to convert it to gray scale.
Image Analyst
2017 年 6 月 1 日
Yes, and that's what my solution below checks for. It checks how many color channels there are and only calls rgb2gray() if it's a color image.
採用された回答
その他の回答 (2 件)
Image Analyst
2013 年 1 月 23 日
編集済み: Image Analyst
2017 年 3 月 10 日
You can only call rgb2gray() on a color image. Calling rgb2gray() on an image that is already gray scale will throw an error. Add this code to make sure the conversion is done ONLY on RGB images:
% Get the number of rows and columns,
% and, most importantly, the number of color channels.
[rows, columns, numberOfColorChannels] = size(Iinitial);
if numberOfColorChannels > 1
% It's a true color RGB image. We need to convert to gray scale.
Igray = rgb2gray(Iinitial);
else
% It's already gray scale. No need to convert.
Igray = Iinitial;
end
6 件のコメント
Jenny
2013 年 1 月 23 日
James
2013 年 12 月 12 日
I found this code very useful, thank you!
Manoj Kumar
2014 年 3 月 17 日
Thanks for your code. It helped me
aimi shaharuddin
2016 年 10 月 11 日
thank you! helps me a lot
linsong zhan
2017 年 3 月 9 日
Thanks for your short codes. It helps me for my project.
Asma Shahid
2017 年 4 月 13 日
Thank you so much for your help.This code is very helping.
Anis Abboud
2014 年 11 月 28 日
Shorter version:
if size(I, 3) > 1
I = rgb2gray(I);
end
カテゴリ
ヘルプ センター および File Exchange で Convert Image Type についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!