| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Image Processing Toolbox |
| Contents | Index |
| Learn more about Image Processing Toolbox |
H = imrect
h = imrect(hparent)
h = imrect(hparent, position)
H = imrect(..., param1, val1,...)
H = imrect begins interactive placement of a rectangle on the current axes. The function returns h, a handle to an imrect object. The rectangle has a context menu associated with it that controls aspects of its appearance and behavior—see Interactive Behavior. Right-click on the rectangle to access this context menu.
h = imrect(hparent) begins interactive placement of a rectangle on the object specified by hparent. hparent specifies the HG parent of the rectangle graphics, which is typically an axes but can also be any other object that can be the parent of an hggroup.
h = imrect(hparent, position) creates a draggable rectangle on the object specified by hparent. position is a four-element vector that specifies the initial size and location of the rectangle. position has the form [xmin ymin width height].
H = imrect(..., param1, val1,...) creates a draggable rectangle, specifying parameters and corresponding values that control the behavior of the rectangle. The following table lists the parameter available. Parameter names can be abbreviated, and case does not matter.
| Parameter | Description |
|---|---|
'PositionConstraintFcn' | Function handle fcn that is called whenever the mouse is dragged. You can use this function to control where the rectangle can be dragged. See the help for the setPositionConstraintFcn method for information about valid function handles. |
When you call imrect with an interactive
syntax, the pointer changes to a cross hairs
when over the image. You
can create the rectangle and adjust its size and position using the
mouse. The rectangle also supports a context menu that you can use
to control aspects of its appearance and behavior. The following figure
shows the rectangle.

The following table lists the interactive behaviors supported by imrect.
| Interactive Behavior | Description |
|---|---|
| Moving the rectangle. | Move the pointer inside the rectangle. The pointer changes
to a fleur shape
|
| Resizing the rectangle. | Move the pointer over any of the edges or corners of the rectangle,
the shape changes to a double-ended arrow,
|
| Changing the color of the rectangle. | Move the pointer inside the rectangle. Right-click and select Set Color from the context menu. |
| Retrieving the coordinates of the current position | Move the pointer inside the polygon. Right-click and select Copy Position from the context menu. imrect copies a four-element position vector to the clipboard. |
| Preserve the current aspect ratio of the rectangle during interactive resizing. | Move the pointer inside the rectangle. Right-click and select Fix Aspect Ratio from the context menu. |
Each imrect object supports a number of methods, listed below. Type methods imrect to see a list of the methods.
See imroi for information.
See imroi for information.
See imroi for information.
See imroi for information.
pos = getPosition(h) returns the current position of the rectangle h. The returned position, pos, is a 1-by-4 array [xmin ymin width height].
See imroi for information.
See imroi for information.
See imroi for information.
See imroi for information.
See imroi for information.
setFixedAspectRatioMode(h,TF) sets the interactive resize behavior of the rectangle h. TF is a logical scalar. True means that the current aspect ratio is preserved during interactive resizing. False means that interactive resizing is not constrained.
setPosition(h,pos) sets the rectangle h to a new position. The new position, pos, has the form [xmin ymin width height].
See imroi for information.
setResizable(h,TF) sets whether the rectangle h may be resized interactively. TF is a logical scalar.
See imroi for information.
If you use imrect with an axes that contains an image object, and do not specify a position constraint function, users can drag the rectangle outside the extent of the image. When used with an axes created by the plot function, the axes limits automatically expand to accommodate the movement of the rectangle.
When the API function setResizable is used to make the rectangle non-resizable, the Fix Aspect Ratio context menu item is not provided.
Display updated position in the title. Specify a position constraint function using makeConstrainToRectFcn to keep the rectangle inside the original Xlim and Ylim ranges.
figure, imshow('cameraman.tif');
h = imrect(gca, [10 10 100 100]);
addNewPositionCallback(h,@(p) title(mat2str(p,3)));
fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'));
setPositionConstraintFcn(h,fcn); Now drag the rectangle using the mouse.
Interactively place a rectangle by clicking and dragging. Use wait to block the MATLAB command line. Double-click on the rectangle to resume execution of the MATLAB command line.
figure, imshow('pout.tif');
h = imrect;
position = wait(h);imellipse, imfreehand, imline, impoint,impoly,imroi,makeConstrainToRectFcn
![]() | imreconstruct | imregionalmax | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |