我有3个不同尺寸的图像。如何基于具有最大尺寸的图像调整其中的两个并将它们居中到新尺寸的中间?
谢谢
答案 0 :(得分:1)
这是我想你想要的。我只使用2张图片(随Matlab一起提供),但这个想法类似于3张图片。由于我不完全确定您想要的输出,因此我为叠加图像制作了2个场景。代码已注释,因此应该很容易理解。
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);
这导致以下结果:
希望有所帮助!