plot line in image via GUI?

4 次查看(过去 30 天)
Kyle
Kyle 2011-7-10
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

回答(3 个)

Robert Cumming
Robert Cumming 2011-7-10
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 个评论
Kyle
Kyle 2011-7-10
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.

请先登录,再进行评论。


Paulo Silva
Paulo Silva 2011-7-10
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 个评论
Image Analyst
Image Analyst 2011-7-10
How about just adapting your own code:
set(get(gca,'Children'),'HitTest','off');
Kyle
Kyle 2011-7-10
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)

请先登录,再进行评论。


Kyle
Kyle 2011-7-10
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

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by