Why I can draw lines on the Axes but can't draw lines anymore after imshow a picture on Axes?

1 次查看(过去 30 天)
Hi, I want to build a gui to draw lines on an image. I can draw lines before load a image on Axes. But after I imshow the image, which I want to draw lines on, on Axes, I can't draw lines anymore. And I also tracked the data. The data can be stored before imshow an image. But nothing happened after load the image. Here is part of my code for open a file and draw a line. Moreover, I have already defined all the values at very beginning. Hope it can be solved. Thanks in advance.
function open_ClickedCallback(hObject, eventdata, handles)
% hObject handle to open (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename,pathname]=uigetfile('*.*');
handles.img=imread([pathname filename]);
handles.imggray=rgb2gray(handles.img);
imshow(handles.imggray);
guidata(hObject,handles);
function drawlines_Callback( hObject, eventdata,handles)
% hObject handle to drawlines (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.imgwindow,'ButtonDownFcn',{@start_pencil,handles})
guidata(hObject,handles);
function start_pencil(hObject,eventdata,handles)
coords=get(gca,'CurrentPoint'); %since this is the axes callback, src=gca
x=coords(1,1,1);
y=coords(1,2,1);
r=line(x, y, 'color', 'g', 'LineWidth', 2, 'hittest', 'off'); %turning hittset off allows you to draw new lines that start on top of an existing line.
handles.r = [handles.r r];
set(gcf,'windowbuttonmotionfcn',{@continue_pencil,r,handles})
set(gcf,'windowbuttonupfcn',{@done_pencil,handles})
guidata(hObject,handles);
function continue_pencil(hObject,eventdata,r,handles)
%Note: src is now the figure handle, not the axes, so we need to use gca.
coords=get(gca,'currentpoint'); %this updates every time i move the mouse
x=coords(1,1,1);
y=coords(1,2,1);
%get the line's existing coordinates and append the new ones.
lastx=get(r,'xdata');
lasty=get(r,'ydata');
newx=[lastx x];
newy=[lasty y];
global C;
C=[newx;newy];
guidata(hObject, handles);
set(r,'xdata',newx,'ydata',newy);
function done_pencil(hObject,eventdata,handles)
%all this function does is turn the motion function off
set(gcf,'windowbuttonmotionfcn','')
set(handles.imgwindow,'ButtonDownFcn','')
set(gcf,'windowbuttonupfcn','')
guidata(hObject, handles);

采纳的回答

Image Analyst
Image Analyst 2014-1-28
Why do functions like continue_pencil() not have _Callback after them, like OutputToComputer_Callback() does? Did you have GUIDE create them automatically or did you try to manually create your own callback functions with your own names? Does the function continue_pencil() ever get executed? That's the only function where handles.coord field is attached to handles, so if that function never executes, then handles won't have that field when you get inside OutputToComputer_Callback().

更多回答(1 个)

AJ von Alt
AJ von Alt 2014-1-28
OutputToComputer_Callback may be called before continue_pencil. You should initialize handles.coord in your opening function.

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by