MATLAB: Error using rgb2gray

image processingImage Processing Toolboxrgb2gray

I've made GUI with GUIDE which makes RGB image into binary image. Before I change the RGB image into binary image, I read the image from another axes not from folder.
%read multiple image at once and showed it in multiple axes
[files,folder,FilterIndex] = uigetfile('*.jpg*','MultiSelect','on');
handles.img=cell(1,numel(files));
for ifile = 1:numel(files)
filename=fullfile(folder,files{ifile});
img=imread(filename);
ax = findall(gcf,'type','axes','tag',sprintf('axes%d',ifile));
imshow(img,'Parent',ax);
handles.img{ifile} = img;
end
guidata(hObject,handles);
%change RGB image in axes1 into binary image
proses=guidata(gcbo);
D=get(proses.citraasli,'Userdata');
abu=rgb2gray(D);
cb=imclearborder(abu);
thresh=graythresh(cb);
b=im2bw(cb,thresh);
bw=bwareaopen(b,60);
bwfill=imfill(bw,'holes');
[c,num]=bwlabel(bwfill,8);
for i=1:1:num
axesName=sprintf('axes%d',i);
ax=handles.(axesName);
imshow((c==i),'Parent',ax);
end
guidata(hObject,handles);
This command works properly in another GUI program, but it gives 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 ==> guikedelaizulfa>ekstrakfitur_Callback at 1127
abu=rgb2gray(D);
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> guikedelaizulfa at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)guikedelaizulfa('ekstrakfitur_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
>>
I've checked the image is RGB image, and i can't find the problem to fix it. If somebody knows how to fix it, please help me! Thank you!

Best Answer

You are not getting an image from an axes with that code. You read it from a file, then find axes on your figure and display the image from the file into that axes. If you want to extract an image from an axes, use
theImage = getimage(handles.axesImage);
Don't use ax like you did - no reason to. Just use the actual "tag" name of the axes that you gave it in GUIDE.
What does this say in the command window:
[rows, columns, numberOfColorChannels] = size(D);
fprintf('Rows = %d, columns = %d, number of color channels = %d\n', rows, columns, numberOfColorChannels);
whos D
abu=rgb2gray(D);