plot line in image via GUI?

3 views (last 30 days)
Kyle
Kyle on 10 Jul 2011
HI,
If i use this code, i'm able to draw line on my image. However when i transfer the code into GUI the line do not appears in the same axes as the image.
imshow(A);
hold on;
c=rand(1,3);
for n=1:10
plot([1 20+n*100],[30 10+n*100],'-','Color',c);
end
Code in GUI:
% --- Executes on button press in advance.
function advance_Callback(hObject, eventdata, handles)
% hObject handle to advance (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global a;
imshow(a,'parent',handles.im2);
hold on;
c=rand(1,3);
for n=1:10
plot([1 20+n*100],[30 10+n*100],'-','Color',c,'parent',handles.im2);
end
if i were to use
plot([1 20+n*100],[30 10+n*100],'-','Color',c);
i could see the line appear on the picture of other axes.
Edit:
My code for the 2 axes and 1 button: (axes im2 is empty)
%>>>>>im1 axes
--- Executes on mouse press over axes background.
function im1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to im1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global a;
[FileName,PathName]= uigetfile(...
{'*.bmp;*.jpg;*.jpeg;*.tif;*.tiff;*.png','All Image Files(*.bmp,*.jpg,*.jpeg,*.tif,*.tiff,*.png)';...
'*bmp','bitmap Files (*.bmp)';...
'*.jpg;*.jpeg','JPEG Files(*.jpg,*.jpeg)';...
'*.tif;*.tiff','Tiff Files(*.tif,*.tiff)';...
'*png','PNG Files (*.png)';...
'*.*','All Files (*.*)'}, ...
'Pick an image file');
cb1=get(gca,'ButtonDownFcn');
fullpath = sprintf('%s%s',PathName, FileName);
a=imread(fullpath);
imshow(a,'parent',handles.im1);
set(gca,'ButtonDownFcn',cb1);
set(get(gca,'Children'),'ButtonDownFcn',cb1);
%>>>>>Button
function advance_Callback(hObject, eventdata, handles)
% hObject handle to advance (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global a;
imshow(a,'parent',handles.im2);
hold on;
c=rand(1,3);
for n=1:10
plot ( handles.im2, [1 20+n*100],[30 10+n*100], '-', 'color', c )
end

Answers (3)

Robert Cumming
Robert Cumming on 10 Jul 2011
in the gui you should force the axes that the plot command should be acting on by giving the plot command the axes handle, i.e.
plot ( handles.im2, [1 20+n*100],[30 10+n*100], '-', 'color', c )
  1 Comment
Kyle
Kyle on 10 Jul 2011
function advance_Callback(hObject, eventdata, handles)
% hObject handle to advance (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global a;
imshow(a,'parent',handles.im2);
hold on;
c=rand(1,3);
for n=1:10
plot ( handles.im2, [1 20+n*100],[30 10+n*100], '-', 'color', c )
end
ya it did plot on handles.im2, but i cant make it to plot on the image that i loaded earlier.

Sign in to comment.


Paulo Silva
Paulo Silva on 10 Jul 2011
Just for fun I created one blank GUI with GUIDE and inserted this code in the OpeningFcn, all works fine, next I put the code on the callback of a button, now each time I press the button the lines changes color.
imshow('trees.tif');
c=rand(1,3);
arrayfun(@(x)line([1 20+x*100],[30 10+x*100],'Color',c),1:10);
Notice also that if you just want to draw lines you should use the line function instead of plot function and you can do it all without the loop using the arrayfun.
  9 Comments
Image Analyst
Image Analyst on 10 Jul 2011
How about just adapting your own code:
set(get(gca,'Children'),'HitTest','off');
Kyle
Kyle on 10 Jul 2011
i tried that but it wouldnt let me input the image second time. as once image is shown on the axes, the ButtonDownFcn is deactivated.
The original code let user to open an image and display it on the axes when user click on the axes itself.(can load image repeatably)

Sign in to comment.


Kyle
Kyle on 10 Jul 2011
I dont really know wat when wrong in previous code. However i did get wat i want after changing the code on the button slightly
%>>>>>Button
function advance_Callback(hObject, eventdata, handles)
% hObject handle to advance (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global a;
imshow(a,'parent',handles.im2);
hold on;
axes(handles.im2)
for n=1:10
c=rand(1,3);
line([1 20+n*100],[30 10+n*100],'Color',c)
end
Thanks for all ur help

Categories

Find more on Migrate GUIDE Apps 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!