MATLAB Answers


Placing multiple rectangles using imrect in GUI

Asked by Christoph on 28 Jan 2013

Hi everyone!

I am new to gui creation in Matlab and have just worked through the simple Gui tutorial.

What I want to do is the following:

load an image -> drag multiple (e.g. 3) rectangles over the roi using imrect with a specific size (e.g. 32x100 px) and then get the coordinates to process the image only in the specified region by an edge detecting algorithm I programmed.

I am loading and displaying the image using a pushbutton


and I know I can draw a rectangle and get the position via another pushbutton


can someone help me with the next step that is fixing the size of the rectangle and how to store the data for every instance I click the button and place a rectangle properly?

Every help is appreciated!

Best Wishes, Chris


Log in to comment.

1 Answer

Answer by Image Analyst
on 28 Jan 2013
 Accepted Answer

I don't think you can constrain imrect(), rbbox(), or getrect() to work with fixed size rectangles and not allow the user to change the size of the rectangle. The workaround is to have two scroll bars, one for vertical movement and one for horizontal movement. The slider callbacks will calculate the new position of the box sides and draw it using plot(). Then the user clicks an OK button. In fact I do this for some of my apps.

The alternative is to use dragrect(). I haven't used this before. Here is the description from the help:

Drag a rectangle that is 50 pixels wide and 100 pixels in height.
point1 = get(gcf,'CurrentPoint') % button down detected
rect = [point1(1,1) point1(1,2) 50 100]
[r2] = dragrect(rect)
dragrect returns immediately if a mousebutton is not currently pressed. Use dragrect ina ButtonDownFcn, or from the command line in conjunctionwith waitforbuttonpress, to ensure that the mousebutton is down when dragrect is called. dragrect returnswhen you release the mouse button.
If the drag ends over a figure window, the positions of therectangles are returned in that figure's coordinate system. If thedrag ends over a part of the screen not contained within a figurewindow, the rectangles are returned in the coordinate system of thefigure over which the drag began.
Note:  You cannot use normalized figure units with dragrect.


do you know if the dragrect uses some coordinates different than the image?

If I drag a rectangle and then give the coordinates to


it always plots somewhere else than the drawing area!

I haven't used it yet. But a common problem is confusing x,y with row,column. The order is reversed: x,y = column,row, not row,column. Could that be the problem?

actually it was my mistake because I segmented the image in a wrong way previously!

Thank you for helping it works quite okay now! :)

Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today