Matlab : getrect alternative? -
i'm looking select area of image in matlab using mouse, returning x/y of corners user.
looking online, getrect function in image processing toolbox don't have image processing toolbox.
is open source alternative i.e. on matlab file exchange?
mark
if okay using global variables , callbacks, following do. create function passes (for example) image wish display
function extractblock(i) % clear global variables (or specific app) clear global; % display image figure; image(i); % set callbacks handling of mouse button down, motion, , % events against figure set(gcf,'windowbuttondownfcn', @mybuttondowncallback, ... 'windowbuttonupfcn', @mybuttonupcallback, ... 'windowbuttonmotionfcn', @mybuttonmotioncallback); end
now define callbacks. start mouse button down event record mouse button has been pressed , position (this callback , others can placed in same file above function):
function mybuttondowncallback(~,~) global is_button_down; global rect_start_coord; is_button_down = true; % top left corner of rectangle rect_start_coord = get(gca,'currentpoint'); end
now handle mouse motion callback draw (or re-draw) rectangle user moves mouse pointer across image:
function mybuttonmotioncallback(~,~) global is_button_down; global rect_start_coord; global rect_end_coord; global rectangle_handle; if ~isempty(is_button_down) && is_button_down % bottom right corner of rectangle rect_end_coord = get(gca,'currentpoint'); % top left corner , width , height of % rectangle (note absolute value forces "open" % left right - need smarter logic other direction) x = rect_start_coord(1,1); y = rect_start_coord(1,2); w = abs(x-rect_end_coord(1,1)); h = abs(y-rect_end_coord(1,2)); % draw rectangle if width , height positive if w>0 && h>0 % rectangle drawn in white (better colour needed different % images?) if isempty(rectangle_handle) % empty rectangle not yet drawn rectangle_handle = rectangle('position',[x,y,w,h],'edgecolor','w'); else % need redraw set(rectangle_handle,'position',[x,y,w,h],'edgecolor','w'); end end end end
now handle mouse event remove rectangle figure , write out corners of rectangle command window (means return matrix of kind have added if return value desired):
function mybuttonupcallback(~,~) global is_button_down; global rectangle_handle; global rect_start_coord; global rect_end_coord; % reset button down flag is_button_down = false; % delete rectangle figure delete(rectangle_handle); % clear handle rectangle_handle = []; % compute top left (tl) , bottom right (br) coordinates tl = [rect_start_coord(1,1) rect_start_coord(1,2)]; br = [rect_end_coord(1,1) rect_end_coord(1,2)]; % compute top right (tr) , bottom left (bl) coordinates tr = [br(1) tl(2)]; bl = [tl(1) br(2)]; % write coordinates command window fprintf('(%f,%f)\t',tl(1),tl(2)); fprintf('(%f,%f)\n',tr(1),tr(2)); fprintf('(%f,%f)\t',bl(1),bl(2)); fprintf('(%f,%f)\n',br(1),br(2)); fprintf('\n'); % optionally display block image end
the above quick way extract user-defined block image, logic missing handle rectangle being drawn right left. hope helps!
Comments
Post a Comment