How do I overlay an rgb image onto a background image?
3 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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
A=imread('image1.jpg');
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
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
More Answers (1)
Image Analyst
on 22 Oct 2016
You can use the padarray() function.
2 Comments
Image Analyst
on 23 Oct 2016
To be explicit:
% Add a 50 pixel wide white boundary around the image.
paddedImage = padarray(rgbImage, [50, 50], 255);
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!