matlab gui 鼠标选择矩形区域 返回鼠标坐标点

1.鼠标选择矩形区域


--- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
hObject    handle to pushbutton1 (see GCBO)
eventdata  reserved - to be defined in a future version of MATLAB
handles    structure with handles and user data (see GUIDATA)

I=imread('pic_gui_I.bmp','bmp');
axes(handles.axes1);
image(I)
axis off;
mp= handles.activex1; % activex1 为调用的WMP的handle。

%画图后:
h=imrect;%鼠标变成十字,用来选取感兴趣区域

----------------------------------------
%图中就会出现可以拖动以及改变大小的矩形框,选好位置后:
---------------------------------------- 
pos=getPosition(h);

---------------------------------------- 
%pos有四个值,分别是矩形框的左下角点的坐标 x y 和 框的 宽度和高度
---------------------------------------- 

---------------------------------------- 
%拷贝选取图片
---------------------------------------- 
imCp = imcrop( I, pos );
figure(2)
imshow(imCp);

2. 返回鼠标坐标点

function main()
global line;
global h1;
global flag;
flag=0;
line=rand(1,100);
h1=plot(line);
hold on
set(h1,'ButtonDownFcn',@clicky);
h2 = uicontrol('style','text','Position',[30 15 100 30],'string','此处显示选中点的坐标');

function clicky(varargin)
%鼠标点击时触发该事件
global h_point
global line;
global flag;
if flag
    set(h_point,'Visible','off');
end
a=get(gca,'Currentpoint');
for i=1:2
    ai=a(1,i);
if ceil(ai)-ai>ai-floor(ai)
    b(1,i)=floor(ai);
else
    b(1,i)=ceil(ai);
end
end
h_point=plot(gca,b(1,1),line(b(1,1)),'r*');
if ~isempty(h_point)
    flag=1;
end
set(findobj('style','text'),'String',strcat('x:',num2str(b(1,1)),'  y:',num2str(line(b(1,1)))));
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值