Ellen - I'm guessing the error has something to do with the image_selected variable not being defined when the popup menu callback fires. This is (most likely) due to the fact that this callback is missing a global image_selected statement in the first line of the callback. So you can add in this mising line in or you can use the handles structure to manage this variable and any other that you might want to share between callbacks. Something like
function pushbutton2_Callback(hObject, eventdata, handles)
handles.img_selected = 2
axes(handles.axes1)
imshow('policeimage2.jpg');
guidata(hObject,handles);
We call guidata so that the updated handles object is "saved" and so it's updated fields will be available to the other callbacks. In the popup menu callback, you would then do
function popupmenu1_Callback(hObject, eventdata, handles)
str = get(hObject,'String')
val = get(hObject,'Value')
switch str{val}
case 'Sobel'
if handles.img_selected == 1
Note how we access this field via handles.
Or you can do something else. It looks like your code only uses img_selected to know which of the three images you have read and shown on the axes. Rather than reading the file again, you could grab the image data by saving the handle to the image object that is shown on the axes. If you make the following change to each of your three pushbutton callbacks
function pushbutton2_Callback(hObject, eventdata, handles)
axes(handles.axes1)
handles.hImage = imshow('policeimage2.jpg');
guidata(hObject,handles);
So now your handles structure has a handle to the image graphics object that is shown in axes1. Now in your popup menu callback, you can use this handle to get the image data:
function popupmenu1_Callback(hObject, eventdata, handles)
str = get(hObject,'String')
val = get(hObject,'Value')
img = get(handles.hImage, 'CData');
switch str{val}
case 'Sobel'
img1 = rgb2gray(img);
img2 = edge(img1,'Sobel')
axes(handles.axes2);
imshow(img2);
case 'Canny'
img1 = rgb2gray(img);
img2 = edge(img1,'Canny');
axes(handles.axes2);
imshow(img2);
case 'Log'
img1 = rgb2gray(img);
img2 = edge(img1,'log');
axes(handles.axes2);
imshow(img2);
case 'Prewitt'
img1 = rgb2gray(img);
img2 = edge(img1,'prewitt');
axes(handles.axes2);
imshow(img2);
case 'Own Method'
img = imread('own_edge_det_output.png');
axes(handles.axes2);
imshow(img);
end
You could also remove alot of the duplicated code in the above as the only difference (with the exception of the last case) is the method used in the edge call.
Best Answer