我正在尝试在matlab中合并多个图像。合并图像的数量最多为四个。我希望它们能够基于层的概念(如在photoshop中)组合在一起,而不是连接在一起。这意味着所得图像的大小将与每个组合图像的大小相同。是否有正确执行该任务的matlab函数?
答案 0 :(得分:0)
试试这个:
img1 = imread...
img2 = imread...
img3 = imread...
img4 = imread...
combined_img(:,1) = img1;
combined_img(:,2) = img2;
combined_img(:,3) = img3;
combined_img(:,4) = img4;
现在你有一个4层的图像,你可以通过combined_img的第三个索引访问它。 以下命令将显示第一个图像:
imshow(combined_img(:,1));
答案 1 :(得分:0)
这就像你描述的那样:
function imgLayers
% initialize figure
figure(1), clf, hold on
% just some random image (included with Matlab) to server
% as the background
img{1,1} = imresize(imread('westconcordaerial.png'), 4);
img{1,2} = [0 0];
% rotate the image and discolor it to create another image
img{2,1} = uint8(imresize(imrotate(img{1}, +12), 0.3)/2.5);
img{2,2} = [150 20];
% and another image
img{3,1} = uint8(imresize(imrotate(img{1}, -15), 0.5)*2.5);
img{3,2} = [450 80];
% show the stacked image
imshow(stack_image(img));
%% create new image, based on several layers
function newImg = stack_image(imgs)
% every image contained at a cell index (ii) is placed
% on top of all the previous ones (0:ii-1)
rows = cellfun(@(x)size(x,1),imgs(:,1));
cols = cellfun(@(x)size(x,2),imgs(:,1));
% initialize new image
newImg = zeros(max(rows(:)), max(cols(:)), 3, 'uint8');
% traverse the stack
for ii = 1:size(imgs,1)
layer = imgs{ii,1};
offset = imgs{ii,2};
newImg( offset(1)+(1:rows(ii)), offset(2)+(1:cols(ii)), :) = layer;
end
end
end
请注意 - 没有足够的限制检查等。所以你必须做一些自己的发展。但它应该足以让你开始:)