How do I overlay an rgb image onto a background image?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
John BG
2016-10-22
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
更多回答(1 个)
Image Analyst
2016-10-22
You can use the padarray() function.
2 个评论
Image Analyst
2016-10-23
To be explicit:
% Add a 50 pixel wide white boundary around the image.
paddedImage = padarray(rgbImage, [50, 50], 255);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!