How to insert picture 1 into picture 2?
21 views (last 30 days)
Show older comments
I want to use matrix insertion method to insert an image taken by a webcam (640x480) into another image with a larger resolution, how should I do that?
0 Comments
Accepted Answer
Vilém Frynta
on 25 Nov 2022
Edited: Vilém Frynta
on 25 Nov 2022
Example:
% Using random pictures that are part of Matlab
img1 = imread("peacock.jpg"); % Smaller picture (792 x 1056)
img2 = imread("flamingos.jpg"); % Larger picture (972 x 1296)
img1_Resolution = size(img1)
We know picture sizes. Now you need to choose where you want to insert the picture and use indexing to overwrite part of the img2 by img1.
I have chosen to insert to position 51–842 on X axis and 123–1279 on Y axis.
imgNew = img2; % Saving larger picture into new variable
% X_position defines where you insert new picture on X axis
X_position = 51:50+img1_Resolution(1);
% Y_position defines where you insert new picture on Y axis
Y_position = 123:122+img1_Resolution(2);
% Inserting smaller picture into the larger one on defined positions
imgNew(X_position, Y_position,:) = img1;
subplot(2,2,1)
imshow(img1)
subplot(2,2,2)
imshow(img2)
subplot(2,2,[3 4])
imshow(imgNew)
Hope this helps. I will edit this to be more understandable, just wanted to post this to save my answer progress.
Edit: aesthethics
6 Comments
Vilém Frynta
on 26 Nov 2022
X position on your frame corresponds to horizontal length of the green area on your picture Y position corresponds to vertical length of the green area
with these 2 things, you are telling Matlab where you want to insert the picture.
I recommend you to educate yourself on “Image Processing Toolbox”, prefferably in your best language, to understand better.
More Answers (1)
See Also
Categories
Find more on Subplots 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!