Reputation: 633
I have 3 images with different dimensions. How can I resize 2 of them based on the image with biggest dimension and center them to the middle of the new dimensions?
Thanks
Upvotes: 0
Views: 377
Reputation: 13945
Here is what I think you want to get. I only use 2 images (that ship with Matlab) but the idea is similar with 3 images of course). Since I'm not entirely sure of the output you want, I made 2 scenarios for overlaying images. The code is commented so it should be easy to follow.
clear
clc
%// Read large image...peppers
Im1 = rgb2gray(imread('peppers.png'));
%// Get the size of the larger image
[row1,col1,~] = size(Im1);
%// Read small image
Im2 = imread('cameraman.tif');
%// Get the size of the small image
[row2,col2,~] = size(Im2);
%// Resize the small image to fit the size of the large one.
Im2R = imresize(Im2,[row1 col1]);
%// Calculate the offset needed to center the small image with the large
%// image
offset_col = round((col1 - size(Im2,2))/2);
offset_row = round((row1 - size(Im2,1))/2);
%// Create new dummy images. I create 2 because I don't completely
%// understand what you want haha.
NewImage = zeros(row1,col1,3,'like',Im1);
NewImage2 = zeros(row1,col1,3,'like',Im1);
%// Assign a channel, here green, to the original large image
NewImage(:,:,2) = Im2R;
NewImage2(:,:,2) = Im1;
%// Place the small cameraman image at the center of the large image (either cameraman resized or peppers). The
%// channel assigned to "NewImage" is the red one. You can change this of course.
NewImage(offset_row:offset_row+row2 -1,offset_col:offset_col+col2 -1,1) = Im2;
NewImage2(offset_row:offset_row+row2 -1,offset_col:offset_col+col2 -1,1) = Im2;
figure;
subplot(1,2,1)
imshow(NewImage)
title({'NewImage';'Cameraman (small) on cameraman (resized)'},'FontSize',16);
subplot(1,2,2)
imshow(NewImage2)
title({'NewImage2';'Camaraman(small) on peppers (large original)'},'FontSize',16);
This results in the following:
Hope that helps!
Upvotes: 1