Problem with rgb2gray function ?

조회 수: 20 (최근 30일)
Jenny
Jenny 2013년 1월 23일
댓글: Image Analyst 2017년 6월 1일
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
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
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Sabarinathan Vadivelu
Sabarinathan Vadivelu 2013년 1월 23일
Check with your input image. It was gray image.
  댓글 수: 2
Jenny
Jenny 2013년 1월 23일
You were right! Thank you very much!
VIKAS  PATEL
VIKAS PATEL 2015년 5월 29일
yes, I was having the same problem and it helped me. Thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
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
linsong zhan
linsong zhan 2017년 3월 9일
Thanks for your short codes. It helps me for my project.
Asma Shahid
Asma Shahid 2017년 4월 13일
Thank you so much for your help.This code is very helping.

댓글을 달려면 로그인하십시오.


Anis Abboud
Anis Abboud 2014년 11월 28일
Shorter version:
if size(I, 3) > 1
I = rgb2gray(I);
end

카테고리

Help CenterFile Exchange에서 Red에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by