-2

I want to do this pseudocode in a system without imagemagick but I have Matlab 2016a and Java in the system

convert 1.png 2.png +append result.png

Assume you want to concatenate in a loop like the following such that dimensions may not match. However, all objects 1,2,...,N have exactly the same dimensions. The following loop, however, concatenates two pictures at the same time, making the dimensions different with subsequent images: bigImage vs new image.

iterationCounter=1;

result=0;
while(iterationCounter < 3)
    imgRGB=imread(filenamePng); % Images 1,2, ..., N have same dimensions.

    if (result==0)
        result=imgRGB;
    end

    % http://stackoverflow.com/a/35915990/54964
    if (ismatrix(result(:,:,1)))
        heightRatio=size(result,1)/size(imgRGB,1);
        wantedSize=int16([size(result,1), size(imgRGB,2)*heightRatio]);
        imgResized=imresize(imgRGB, wantedSize);
        result=[result, imgResized];
    end

    iterationCounter=iterationCounter+1;

end

where output is an empty picture.


How can you do the horizontal appending of the images in Matlab/Java?

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • If you can change the resolution and colourspace of images to be compatible, then you can use simple `BothImages=[Image1,Image2];` command. – Crowley Mar 10 '16 at 12:02
  • @Crowley Actually, I have done individually `parula` colormap messes up the decoration. So I am now storing raw gray-pictures instead such that I could apply the colormap to one big picture at once. – Léo Léopold Hertz 준영 Mar 10 '16 at 12:05
  • I meant thas some images are represented by `m x n` matrix with `colormap` doing the colours and other are represented by `m x n x 3` matix while 3rd dimension desribes the colour in, say RGB, colourspace. Dimensions must fit so concatenation won't throw an error. – Crowley Mar 10 '16 at 12:13
  • Yes, no problem with that. I am mostly in interested in fitting the colorspaces correctly. I think grayscale is the only way to do it bijectively. – Léo Léopold Hertz 준영 Mar 10 '16 at 12:14

2 Answers2

1

in general, when you concat two images horizontally, and they both have the same number of rows, the following code does the trick:

I1 = imread('coins.png');
I2 = imread('coins.png');
appendedIm = [I1,I2];

however, if you need to change I2 dimensions to fit to I1 height, you can perform imresize as follows:

%input - two images with different dimensions
I1 = imread('coins.png'); 
I2 = imread('cameraman.tif');

heightRatio = size(I1,1)/size(I2,1); %the ratio which well need to make the two images to have the same height
wantedSize = int16([size(I1,1), size(I2,2)*heightRatio]);
I2Resized = imresize(I2,wantedSize );
outIm = [I1,I2Resized];

if you need vertical concatination, you can use:

appendedIm = [I1;I2];
ibezito
  • 5,782
  • 2
  • 22
  • 46
  • 3
    This will work only if, as I wrote in comment, when colorspaces and dimensions match. – Crowley Mar 10 '16 at 12:08
  • you are correct. a possible way to overcome the size issue, will be to change the second line as following: I2 = imresize(imread('coins.png'),size(I1)) – ibezito Mar 10 '16 at 12:36
  • @Masi if you cannot resize, how you want to deal with, possibly, different widths? – Crowley Mar 10 '16 at 12:42
  • @Masi if you expand bitmap figure, you loose dpi, if you shrink you loose details. Or you can crop the image sacrificing the viewfield. – Crowley Mar 10 '16 at 12:47
  • 1
    @Masi - another possibility would be padding of smaller images with zeros - depends on what is the desired output – ibezito Mar 10 '16 at 12:49
  • @masi, I'll modify my answer accordingly – ibezito Mar 10 '16 at 13:15
  • 1
    @Masi - I modified my answer. let me know if you have any more questions about it :) by the way - notice that in your case you perform vertical concatanation. if you want it to be horizontal, change result=[result; imgRGB]; into result=[result, imgRGB]; – ibezito Mar 10 '16 at 13:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105916/discussion-between-drorco-and-masi). – ibezito Mar 10 '16 at 14:52
1

This should work:

imwrite([imread('1.png') imread('2.png')], '12.png');

Note: They should have the same width/height depending on how you are appending them.

Lincoln Cheng
  • 2,263
  • 1
  • 11
  • 17