필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Lewis
Lewis 2016년 10월 22일
댓글: Image Analyst 2016년 10월 23일
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
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
  댓글 수: 2
Lewis
Lewis 2016년 10월 22일
Perfect! Thanks a lot for this, and for replying so quickly.
John BG
John BG 2016년 10월 23일
happy to help
any time :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2016년 10월 22일
You can use the padarray() function.
  댓글 수: 2
John BG
John BG 2016년 10월 23일
there are so many things one can do be do be do
Image Analyst
Image Analyst 2016년 10월 23일
To be explicit:
% Add a 50 pixel wide white boundary around the image.
paddedImage = padarray(rgbImage, [50, 50], 255);

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by