MATLAB: How to add context menu to imrect

context menuimage processingimage taggingurgent

How to add context menu to imrect ..I have a image i want to select a particular part using imrect nd then i want to add context menu to dat part how can i do the same./?????/ Its urgent.. help me asap….

Best Answer

Here's a basic function I created that calculates statistics for the ROI chosen by an imrect:
function imstatsrect(im)
% This function displays the data in the input 2D array 'im' in a new
% figure
% A draggable, resizeable rectangle allows the user to define a ROI
% The rectangle is limited by the size of the original image. Zoom
% functions are compatible but it’s possible to zoom so the rectangle is
% out of your field of view.
% The title and axes labels of the figure are continually updated with the
% position and statistics (mean and standard deviation) for the ROI
% The structure variable tempimstatscur in the main workspace is
% constantly updated with the stats from the ROI
% Right-clicking the rectangle and choosing the context-menu option 'Copy
% current data to workspace' appends the current stats to the structure
% variable 'tempimstatssave'. Stats from multiple rectangles can be
% appended to this variable. The variable is reset when this function is
% run again
%
% Michael S. D. Smith February 11, 2013
figure
imagesc(im);
hrect = imrect(gca, [1 1 round(size(im,2)/2) round(size(im,1)/2)]);
api = iptgetapi(hrect);
hrectchild = get(hrect, 'Children');
hcmenu = get(hrectchild(1),'UIContextMenu'); % get the handle for the context menu
itemnew = uimenu(hcmenu, 'Label', 'Copy current data to workspace', 'Callback', @copytoworkspace); % add a menu item to copy data to workspace
fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'));
api.setPositionConstraintFcn(fcn);
api.addNewPositionCallback(@newpos);
evalin('base','tempimstatscur.xrange=[0 0];')
evalin('base','tempimstatscur.yrange=[0 0];')
evalin('base','tempimstatscur.mean=0;')
evalin('base','tempimstatscur.std=0;')
evalin('base','clear tempimstatssave')
function newpos(p)
xmin=ceil(p(1)); % make sure values are integers
ymin=ceil(p(2));
xmax=floor(p(1)+p(3));
ymax=floor(p(2)+p(4));
imdata=getimage(gca); % get data from current image
datamean=mean2(imdata(ymin:ymax,xmin:xmax));
datastd=std2(imdata(ymin:ymax,xmin:xmax));
title(['mean=' num2str(datamean) ', std=' num2str(datastd)]); % display current stats in labels
xlabel(['xmin=' num2str(xmin) ', xmax=' num2str(xmax)])
ylabel(['ymin=' num2str(ymin) ', ymax=' num2str(ymax)])
imstats.xrange=[xmin xmax];
imstats.yrange=[ymin ymax];
imstats.mean=datamean;
imstats.std=datastd;
assignin('base','tempimstatscur',imstats)
function copytoworkspace(varargin)
try
evalin('base','tempimstatssave(length(tempimstatssave)+1) = tempimstatscur;')
catch
evalin('base','tempimstatssave = tempimstatscur;')
end