简体   繁体   中英

MATLAB: changing montage orientation of 2 images

I'm trying to use MATLAB to open a montage of just two images placed top and bottom. MATLAB defaults to a left/right orientation. I've tried montage(images, 'Size', [2 1]) but this still gives me the left/right orientation but just adds a blank row underneath.

montage defaults to giving you a left/right montage and you can't change that. If you want to stack images on top of each other, assuming the same sized images, use cat . Assuming your images are called A and B , simply do this:

C = cat(1, A, B);
imshow(C);

If however your images aren't the same size, then what we can do is make sure that the columns are the same size, create new images that zeropad the columns and then stack them on top of each other. Assuming that both A and B have the same number of channels:

rows1 = size(A, 1);
cols1 = size(A, 2);
rows2 = size(B, 1);
cols2 = size(B, 2);
C = zeros(rows1 + rows2, max(cols1, cols2), size(A, 3), class(A));

C(1:rows1, 1:cols1, :) = A;
C(rows1+1:end, 1:cols2, :) = B;
imshow(C);

The first four lines determine the rows and columns of each image. Next we create a blank image where the number of rows is just the sum of the two images but the number of columns is the larger of the two images. This is to accommodate for the biggest sized image along the columns. We also make sure we cast the output to the same class as A (or B assuming the same type). Once we're done, you simply place the first image at the top of C , then the second image at the bottom of C offsetting by rows1 (the number of rows for A ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM