chee
chee

Reputation: 979

joining images of different dimensions in matlab

if i have two images of different dimensions....than can i do it like i have a window with size equal to the sum of sizes of two images, means window( size)=size of image1+size of image 2 and than paste these mages on this window to show them jointly .....is it possible?if yes than how

Upvotes: 0

Views: 2784

Answers (2)

user2603796
user2603796

Reputation:

I have written this code to join two images horizontally. img1 and img2 should be grayscale.

function [ output_args ] = sideBySideImage( img1, img2 )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here, expects a black and white images


    [r1, c1] = size(img1);
    [r2, c2] = size(img2);

    if r1<r2    %rows in first img are less, so we add some rows in it
        temp_row = zeros(1,c1);
        while r1~=r2
            img1 = [img1; temp_row];
            [r1, c1] = size(img1);
            [r2, c2] = size(img2);
        end
    elseif r1>r2    %rows in second img are less, so we add some rows in it
        temp_row = zeros(1,c2);
        while r1~=r2
            img2 = [img2; temp_row];
            [r1, c1] = size(img1);
            [r2, c2] = size(img2);
        end
    end



    output_args = [img1, img2];

end

Upvotes: 2

Itamar Katz
Itamar Katz

Reputation: 9655

From your description it sounds you want to construct a block-diagonal matrix from the two images, since then the size of the result will be the sum of sizes (along each dimension). The way to do it is to use the blkdiag function:

img1 = randn(70,100);
img2 = randn(50,110);
img = blkdiag(img1,img2);
imshow(img)

Of course the off-diagonal blocks are padded with zeros.

EDIT:

Answering your refined question, you have to pad the shorter image with zeros so it becomes at the same height as the longer image. Then you can concatenate them side by side. Assuming img1 is longer, it would look something like:

h1 = size(img1, 1);
[h2, w2] = size(img2);
img2a = [img2; zeros(h1-h2, w2)];
img = [img1, img2a];

Upvotes: 2

Related Questions