|
%% 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);
%% 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)];
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2025-1-9 10:23
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社