plot line in image via GUI?

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

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 个评论

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.

请先登录,再进行评论。

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 个评论

you just need to select the current axes before making lines or whatever
axes(axeshandle) %this selects the current axes
%now you can add lines or other objects to the current axes
hm for my case if my GUI has only 1 axes, the code work fine. However when i add another axes problem and write the code to display and plot line of the second axes problem arise.
What's your new code? Calling axes() before each call to plot() or imshow() will cause drawing to the specified axes. I can't tell why you say it's not doing that if you don't show your code, which has both im1 and im2 in it - all you've shown us so far is im2 code.
I placed the code in my question.
Are you sure the buttondown function for the axes is being called? Does it stop there if you put a breakpoint there? Have you read this solution: http://www.mathworks.com/support/solutions/en/data/1-1B03X/?solution=1-1B03X
Hmm i just tested loading with a button instead of using axes problem solved.
But i dont understand what when wrong. The buttondown function of im1 axes is just to load im image when the user click the axes.
Quoted from the link send by Image Analyst
"
1. Set the "HitTest" property for the axis' child objects to 'off'. The "HitTest" property determines whether the object can become the current object when it is clicked on. If the "HitTest" property is set to 'off', then the object below it will become the current object, which could be another of the axes' child objects or the axes itself. If all of the axes' child objects have their "HitTest" property set to 'off', then the axes will become the current object and its "ButtonDownFcn" callback will be executed.
set(h,'HitTest','off')"
How do i do that?
How about just adapting your own code:
set(get(gca,'Children'),'HitTest','off');
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)

请先登录,再进行评论。

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

类别

帮助中心File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

提问:

2011-7-10

Community Treasure Hunt

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

Start Hunting!

Translated by