MATLAB: I can draw lines on the Axes but can’t draw lines anymore after imshow a picture on Axes

guiimage analysis

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);

Best Answer

  • The problem solved. I just add a new handle to the imshow and set its buttondownfcn. Thanks, anyway, to everyone who concerned my problem.