Drawing a rectangle on top of an image

298 views (last 30 days)
med-sweng
med-sweng on 19 Dec 2013
Commented: DGM on 26 Feb 2022
Say I have a rectangle with some dimensions. If I want to draw a rectangle over an image using for instance `plot`, if I start as follows:
hold on
imshow(img)
Then, regarding `plot` and my rectangle. How would they fit in this process?
Thanks.

Answers (1)

Image Analyst
Image Analyst on 19 Dec 2013
imshow(img);
hold on;
% Then, from the help:
rectangle('Position',[0.59,0.35,3.75,1.37],...
'Curvature',[0.8,0.4],...
'LineWidth',2,'LineStyle','--')
  4 Comments
Katia-Renae Purnell
Katia-Renae Purnell on 25 Feb 2022
I see you calculate the size of the image, but I don't see where it is used. How do you calculate the offset so that the rectangle is actually drawn over the image?
DGM
DGM on 26 Feb 2022
As to how you find the correct offset, that certainly might be a function of the image height and width as you suspect. The problem with writing a generalized example is that there are multiple ways to find an offset for an arbitrary location in space. In some case, you might want to draw a rectangle over some text to censor it. In some example, you might want to draw a rectangle around a peak value. In the above example, the rectangle is drawn around a face. Programmatic solutions to these three tasks would be significantly different and some are individually involved enough that it's arguable that they're beyond the scope of a simple rectangle() demo.
I don't know for certain how Image Analyst found the face, but I imagine that if I needed to draw a rectangle over one such image, I would probably just use a datatip or impixelinfo() to fetch an approximate set of coordinates.
If your're implicitly asking how you can programmatically get the offsets to place a rectangle() object in a certain spot on various images, you'll need to start with a good description of what your images look like and what your goals are. Depending on the scope of the problem, it may be best to open a new question so as to get more eyes on it.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!