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 个)
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 )
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 个评论
Paulo Silva
2011-7-10
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
Kyle
2011-7-10
Image Analyst
2011-7-10
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.
Kyle
2011-7-10
Image Analyst
2011-7-10
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
Kyle
2011-7-10
Kyle
2011-7-10
Image Analyst
2011-7-10
How about just adapting your own code:
set(get(gca,'Children'),'HitTest','off');
Kyle
2011-7-10
类别
在 帮助中心 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!