How to reset the current axes to display either color or grayscale image on the same axes in MATLAB?

1 view (last 30 days)
Hi,
I have some grayscale images, which after some segmentation I converted a section to color for display purposes. But I cannot display a color image on the axes after I display a grayscale image on the same axes.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global imB
global imF
global finalSegment_LE
i=38;
% imB = img2{i} ;% Background original image
tempSeg = finalSegment_LE{i};
tempSeg(finalSegment_LE{i} ==0) = min(finalSegment_LE{i}(:));
imF = tempSeg;
cla(handles.axes1,'reset');
[~,~] = imoverlay(imB,imF,[],[],'hsv',0.8,handles.axes1); % color image..
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% global imB
temp = imread('cameraman.tif');
cla(handles.axes1,'reset');
axes(handles.axes1);
imshow(temp,[]) % grayscale image...
If I press pushbutton1 first I see a color image, but after I press pushbutton2, the axes become grayscale and even when I press the pushbutton1, It still displays a grayscale image instead of the color image.
Thanks,
Gopi

Answers (1)

Gopichandh Danala
Gopichandh Danala on 7 Jul 2017
I figured out the solution, we can set colormap and then imshow to display respective images..
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[img,cmap] = imread('peppers.png');
cla(handles.axes1,'reset');
colormap(handles.axes1,cmap);
axes(handles.axes1);
imshow(img,[]);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% global imB
temp = imread('cameraman.tif');
cla(handles.axes1,'reset');
colormap(handles.axes1,gray);
axes(handles.axes1);
imshow(temp,[])

Categories

Find more on Interactive Control and Callbacks 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!