How to add box to an image?

Hi
I have two images ('yellow.png' and 'bird.png'). See below.
1-4.png bird1_before.jpg
How can I make a box AROUND the image borders with self-defined box colours, say all four lines with grey colour [0.5 0.5 0.5]?
See below.
1-1.png 1-2.png
Please can you help?

7 Comments

After you plot the yellow patch presumable you'll have it's position, width, and height. Then you can use rectangle() to plot a frame around it and control the color, thickness, etc.
They are not plots. They are pre-saved images.
Adam Danz
Adam Danz on 28 Aug 2019
Edited: Adam Danz on 28 Aug 2019
If your question is how to add a frame around an image, you could solve this problem in powerpoint, microsoft paint, coreldraw, photoshop, or any image software. Why do you need Matlab?
If I code:
fn = 'bird1.jpg';
RGB = imread(fn);
s = size(RGB);
figure
imshow(RGB)
rectangle('position',[0 0 s(2) s(1)],'edgecolor',[1 0 0])
I got this. Instead of a box, I only got two lines, and missing the other two lines.
Can anyone help please??
untitled.png
Because I have 273 images. Each one of different size.
The images were first generated in Matlab. I then noticed that some images have similar colour to the background. So that I need to draw a box around the image to separate it from the background. Otherwise it would look like the background eats the image.
Adam Danz
Adam Danz on 28 Aug 2019
Edited: Adam Danz on 30 Aug 2019
"The images were first generated in Matlab"
Maybe it would be easier if you altered the code that generated the image. Adding the frame is just 1 line of code. But if you have read the images back into matlab, that's going to be more work.
Based on the code and image in your other comment, it appears the image's bottom, left corner is not at (0,0) where your rectangle starts. See answer.
A grayscale border can be added using IPT padarray(). A colored border requires extra steps, though alternative methods exist. See the following reference answer, which shows various possibilities.

Sign in to comment.

 Accepted Answer

Image Analyst
Image Analyst on 30 Aug 2019
Edited: Image Analyst on 30 Aug 2019
If the bad lines are always the rightmost one and the bottom one, you can simply crop the image to not include those:
rgbImage = rgbImage(1:end-1, 1:end-1, :);
To pad the image with certain color, you can use padarray() on each color channel. There are other ways. Let me know if it's still a problem for you.

More Answers (4)

Adam Danz
Adam Danz on 28 Aug 2019
Edited: Adam Danz on 2 Sep 2019
Use the handle output from imshow to get the image's coordinates. The XData and YData properties will provide you with the coordinates of the image. Unless the image coordinates are specified in imshow() inputs, the first pixel is centered at coordinate (1,1) so the rectangle should begin at (0.5, 0.5). To expand the rectangle to the outer edges, you must add 1 to the width and height indicated by ranges of XData and YData.
% When the image is in pixel units
corn_gray = imread('corn.tif',3);
h = imshow(corn_gray);
rh = rectangle('position',[ ...
h.XData(1)-.5,...
h.YData(1)-.5,...
range(h.XData)+1,...
range(h.YData)+1],...
'EdgeColor', 'r','linewidth',2);
When the image is not in pixel units, XData and YData properties of the image object may no longer indicate the pixel centers so you must compute the location of the pixel edges.
% When the image is not in pixel units
corn_gray = imread('corn.tif',3);
h = imshow(corn_gray,'XData',200:400,'YData',110:4000); %(arbitrary xdata,ydata)
axis square
axis on % optional, to see axis scales
pixelRatio = [range(h.XData)/size(corn_gray,2), range(h.YData)/size(corn_gray,1)];
rh = rectangle('position',[ ...
h.XData(1)-pixelRatio(1)/2,...
h.YData(1)-pixelRatio(2)/2,...
range(h.XData)+pixelRatio(1),...
range(h.YData)+pixelRatio(2)],...
'EdgeColor', 'r','linewidth',2);
% Expand the axis limits to show full frame
xlim(xlim()+[-.01,.01]*range(xlim())) % add 1% to the x axis limits
ylim(ylim()+[-.01,.01]*range(ylim())) % add 1% to the y axis limits
Images rows and columns start with 1, not zero. so do this:
rectangle('position', [1, 1, s(2), s(1)], 'edgecolor', [1 0 0])
If some part of the rectangle is still missing (outside the image), try adding or subtracting 0.5 or 1 to the position values until you can see it. Like perhaps
rectangle('position', [1, 1, s(2) - 1, s(1) - 1], 'edgecolor', [1 0 0])

5 Comments

"Images rows and columns start with 1, not zero."
The origin of the image could potentially be offset by a spatial reference object which is why I suggest using the XData and YData properties in my answer.
True. But if you want units in something other than pixels you'd set the units with the 'XData' and 'YData' options to imshow(). Maybe you could add them to your demo.
Hi Image Analyst,
Thanks for your answer. I used your way of thinking and modified it.
'position' [0 0 s(2) s(1)] would give me
'position' [1 1 s(2) s(1)] would give me
I guess I just have to do it separately.
clear
close all
clc
RGB = imread('bird1.jpg');
s = size(RGB);
figure
imshow(RGB);
rectangle('position',[1 1 s(2) s(1)], 'edgecolor', [1 0 0])
rectangle('position',[0 0 s(2) s(1)], 'edgecolor', [1 0 0])
3.png
Adam Danz
Adam Danz on 3 Sep 2019
Edited: Adam Danz on 3 Sep 2019
Then you should have accepted Image Analyst's answer rather than your modification of another person's work.
I have accepted Image Analyst's anwser.

Sign in to comment.

clear
close all
clc
RGB = imread('bird1.jpg');
s = size(RGB);
figure
imshow(RGB);
rectangle('position',[1 1 s(2) s(1)], 'edgecolor', [1 0 0])
rectangle('position',[0 0 s(2) s(1)], 'edgecolor', [1 0 0])
3.png

1 Comment

Adam Danz
Adam Danz on 3 Sep 2019
Edited: Adam Danz on 6 Sep 2019
If you zoom into the image at the rectangle corners you'll see that it doesn't frame the pixel edges because you're not offsetting it by the pixel width & height.

Sign in to comment.

Serge
Serge on 23 Mar 2025
Edited: Serge on 23 Mar 2025
Here is another option that uses xlim/ylim and xline/yline.
This works even if one of the the axis is datetime or a duration.
im = imread('rice.png');
figure
imshow(im)
xline(xlim, 'color', 'r')
yline(ylim, 'color', 'r')

Categories

Find more on Display Image in Help Center and File Exchange

Asked:

on 28 Aug 2019

Edited:

on 23 Mar 2025

Community Treasure Hunt

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

Start Hunting!