Set position constraint function of ROI object
setPositionConstraintFcn is not recommended. With the new ROIs, use
DrawingArea property instead. For more information, see Compatibility Considerations.
Update Title when Rectangle Moves
Display a rectangle ROI over an image. Display the position of the rectangle in the title. The title updates when you move the rectangle. Try dragging one side of the rectangle outside the boundary of the image.
imshow('cameraman.tif') h = imrect(gca,[10 10 100 100]); addNewPositionCallback(h,@(p) title(mat2str(p,3)));
Specify a position constraint function using
makeConstrainToRectFcn to keep the rectangle inside the original
fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim')); setPositionConstraintFcn(h,fcn);
Now drag the rectangle using the mouse. Observe that the rectangle can no longer extend past the image boundary.
fcn — Function handle
Function handle, specified as a handle. You can use the
create this function. The function must accept a numeric array as input, and
it must return a numeric array as output. Both arrays must have the same
form as returned when calling
on the object. For more information, see Create Function Handle.
setPositionConstraintFcn is not recommended
Not recommended starting in R2018b
Starting in R2018b, a new set of ROI objects replaces the existing set of ROI objects. The new objects provide more functional capabilities, such as face color transparency. The new classes also support events that you can use to respond to changes in your ROI such as moving or being clicked. Although there are no plans to remove the old ROI objects at this time, switch to the new ROIs to take advantage of the additional capabilities and flexibility. For more information on creating ROIs using the new ROI functions, see Create ROI Shapes.
In the existing ROIs, you can create a function that controls where you can draw
or move an ROI. You then register the position constraint function with the ROI. To
specify the area when you can draw or move an ROI, use the
Update all instances of
with the freehand or polygonal ROI.
|Discouraged Usage||Recommended Replacement|
This example creates a point ROI and uses the
imshow('cell.tif') h = impoint(gca,20,60); % Make a function that constrains movement of the point x = get(gca,'XLim'); y = get(gca,'YLim'); fcn = makeConstrainToRectFcn('impoint',x,y);; % Apply the constraint function to the ROI. setPositionConstraintFcn(h,fcn);
Create one of the new ROI objects and use the
imshow('cell.tif') h = drawpoint(gca,'Position',[20 60]) [height width] = size(I); %Get image dimensions h.DrawingArea = [10,10,(width-20),(height-20)];