jingweimo的个人博客分享 http://blog.sciencenet.cn/u/jingweimo

博文

Specify image rectangle by mouse

已有 2172 次阅读 2015-9-15 03:53 |系统分类:科研笔记

%% Use the built-in function: getrect

figure; imshow(A); %

rect = getrect; % current figure by default; not applicable for subplot image

% rect is a four-element vector [xmin, ymin, width, height];

% actual region selected by mouse is [xmin:xmin+width-1, ymin:ymini+height-1];

rect = round(rect); % conversion to integer

rs = rect(2); %starting row

re = rect(2) + rect(4) -1; %ending row

cs = rect(1); %starting column

ce = rect(1) + rect(3) -1;

figure; imshow(A(rs:re,cs:ce));


%% use figure's WindowButtonDownFcn property

%define a call-back function
% main
function test_mouse_track()
figure;
axis([-10,10,0,5]);
set(gcf,'WindowButtonDownFcn',@ButttonDownFcn);

% call back
function ButttonDownFcn(src,event)
pt = get(gca,'CurrentPoint');
x = pt(1,1);
y = pt(1,2);
line(x,y);
fprintf('x=%f,y=%fn',x,y);


%% Otherwise, use get function

k = waitforbuttonpress;             
point1 = get(gca,'CurrentPoint');   
finalRect = rbbox;                
point2 = get(gca,'CurrentPoint');  
point1 = point1(1,1:2);           
point2 = point2(1,1:2);

p1 = min(floor(point1),floor(point2));             % calculate position
p2 = max(floor(point1),floor(point2));
offset = abs(floor(point1)-floor(point2));         % offset(1): width,offset(2): height

x = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
y = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];





https://blog.sciencenet.cn/blog-578676-920868.html

上一篇:GUI: Pop-up Menu and Listbox, Button group and Panel
下一篇:余光
收藏 IP: 35.10.57.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2025-1-9 10:23

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部