# How do I overlay an rgb image onto a background image?

8 views (last 30 days)
Lewis on 22 Oct 2016
Commented: Image Analyst on 23 Oct 2016
Hello, I'm quite new to MATLAB and I've been trying to find a solution to what seems like a simple problem.
I have an RGB image and I would like to create a white border around it. In particular, I want the border at the bottom to be quite a bit larger to allow for some text. Then I will loop over all images in a directory and process them in that way.
I'm sure I have to create a background image whose dimensions are simply a bit larger than the rgb image, and then just overlay the rgb image onto the background. I need some help doing the overlaying part. Is there a simple way to do this?
Thanks

John BG on 22 Oct 2016
Hi Lewis
If I have understood your question correctly, you want to do the following:
1. clear all;clc
figure(1);imshow(A); % select image to have frame built around
[sza1 sza2 sza3]=size(A); % note sza2 is X, and sza1 is Y
2. define offsets, in this question 2 2D offset points, for instance
dx1=20
dy1=30
% 2nd offset point
dx2=60
dy2=120
3. generate void image to output, 0 is white, 255 is black
B=255*ones(sza1+dy1+dy2,sza2+dx1+dx2,sza3);
B=uint8(B); % if this step missed MATLAB function imshow flattens it all to whites
4. overlay R G B layers separately
A1=A(:,:,1);A2=A(:,:,2);A3=A(:,:,3);
B1=B([dy1:1:sza1+dy1-1],[dx1:1:sza2+dx1-1],1);
B2=B([dy1:1:sza1+dy1-1],[dx1:1:sza2+dx1-1],2);
B3=B([dy1:1:sza1+dy1-1],[dx1:1:sza2+dx1-1],3);
B1=A1;
B2=A2;
B3=A3;
B([dy1:1:sza1+dy1-1],[dx1:1:sza2+dx1-1],1)=B1;
B([dy1:1:sza1+dy1-1],[dx1:1:sza2+dx1-1],2)=B2;
B([dy1:1:sza1+dy1-1],[dx1:1:sza2+dx1-1],3)=B3;
et voilà
5.
figure(2);imshow(B)
6. post-processing, like for instance saving result to file: imwrite(B,'result_file_name.jpg')
so dear Lewis
John BG
##### 2 CommentsShowHide 1 older comment
John BG on 23 Oct 2016
happy to help
any time :)

Image Analyst on 22 Oct 2016
You can use the padarray() function.
##### 2 CommentsShowHide 1 older comment
Image Analyst on 23 Oct 2016
To be explicit:
% Add a 50 pixel wide white boundary around the image.