IMRECT: constrain rectangle heght to be a multiple of another value

Hi, Please help me end three days of frustration....I need to be able to:
  1. Overlay a rectangle on an image using imrect (does not have to be imrect but this is what I am using, I am open to change)
  2. Allow the user to modify the upper and lower bounds (height) of the rectangle
  3. When either lower or upper bound is modified, find the the new 'height' (e.g. 25pixels)
  4. Constrain the height to be an integer multiple of a certain pre-determined value (e.g. n*20pixels) - therefore automatically modify the height into a multiple of the pre-determined value (i.e. auto-change to 40)
Thanks, Dominic

Answers (3)

Three days? Don't you just call imrect() and then the user clicks to accept it, and then you just get the coordinates and quantize them to the nearest multiple of 20? It's just a few lines of code, isn't it? Three minutes of work got me this:
% imrect Example 2
% 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.
imshow('pout.tif');
h = imrect;
position = wait(h);
delete(h);
% Now quantize width and height.
quantizedWidth = position(3) - rem(position(3), 20)
quantizedHeight = position(4) - rem(position(4), 20)
x1 = position(1);
y1 = position(2);
x2 = x1 + quantizedWidth - 1;
y2 = y1 +quantizedHeight - 1;
rectangle('Position', [x1, y1, quantizedWidth, quantizedHeight], 'EdgeColor', [1 1 0]);
This sounds like it might be possible with the setFixedAspectRatioMode of the imrect() object. Then the aspect ratio will never change no matter what they do.
Thanks very much for your answers and expertise in helping a schmuck like me answer a problem like that!!
Your answer allowed me to better understand the capabilities of imrect and helped to solve the problem .
I really appreciate it.
Thanks again, Domninic

This question is closed.

Tags

Asked:

on 17 Apr 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!