Position based image stitching

18 views (last 30 days)
I have 4 same size tile images and they are partially overlapped (assume 15% each).
I don't want to use stitching algorithms like feature based stitching but just array images in rectangle mold like above image.
Thanks!

Accepted Answer

Walter Roberson
Walter Roberson on 17 Jul 2017
The below uses a facility introduced in R2016a
overlap_fraction = 15/100;
[r, c, p] = size(FirstImage); %we assume the others are the same size
overlap_c = round(overlap_fraction * c);
new_c = c + c - overlap_c;
overlap_r = round(overlap_fraction * r);
new_r = r + r - overlap_r;
temp_image1 = nan(new_r, new_c, p ); %double
temp_image2 = temp_image1;
temp_image3 = temp_image1;
temp_image4 = temp_image1;
temp_image1(1:r, 1:c, :) = FirstImage;
temp_image2(1:r, end-c+1:end, :) = SecondImage;
temp_image3(end-r+1:end, 1:c, :) = ThirdImage;
temp_image4(end-r+1:end, end-c+1:end, :) = FourthImage;
temp_image = cat(4, temp_image1, temp_image2, temp_image3, temp_image4);
%this line requires R2016a or later
registered_image = cast( mean(temp_image, 4, 'omitnan'), class(FirstImage) );
If you do not have R2016a or later but you do have the Statistics Toolbox you can use
registered_image = cast( nanmean(temp_image, 4), class(FirstImage) );

More Answers (2)

Adam Rauff
Adam Rauff on 23 Mar 2018
Edited: Adam Rauff on 23 Mar 2018
This code generalizes this procedure to more than 2x2 (grayscale images only)
% define Y by X number of images
IMinfo.YXgrid = [10 8];
% chan4.IM is structure where images are stored in order
% i.e chan4(1).IM is image at the (1,1) position
% chan4(2).IM is image at the (1,2) position
% chan4(5).IM is image at the (2,1) position
% obtain final size of image
overlap = (Insert your overlap percentage);
[r, c] = size(firstImage);
overlap_c = round(overlap * c); % pixels
new_c = c*IMinfo.XYGrid(1) - (IMinfo.XYGrid(1)-1)*overlap_c; % total columns in final image
overlap_r = round(overlap * r); % pixels
new_r = r*IMinfo.XYGrid(2) - (IMinfo.XYGrid(2)-1)*overlap_r; % total rows in final image
% intialize mosaic template
Mosaic = zeros(new_r, new_c);
% "stitch" images by overlaying them
for i = 1:IMinfo.XYGrid(2)
roBegin = (i-1)*r + 1 - overlap_r*(i-1);
roEnd = (i-1)*r + r - overlap_r*(i-1);
for j = 1:IMinfo.XYGrid(1)
colBegin = (j-1)*c + 1 - overlap_c*(j-1); % in matlab index begins at 1
colEnd = (j-1)*c + c - overlap_c*(j-1);
Mosaic(roBegin:roEnd, colBegin:colEnd) = chan4(j+((i-1)*IMinfo.XYGrid(1))).IM;
end
end
  1 Comment
Timothy Sawe
Timothy Sawe on 5 Dec 2019
Hi Adam so I tried inserting my images using chan4 as below:
chan4(1).IM = imread('img1.jpg');
chan4(2).IM = imread('img2.jpg');
but it gives me an error:
Unable to perform assignment because the size of the left side is 1-by-6 and the size of the
right side is 2873-by-2825.
Error in pos_based (line 44)
Mosaic(roBegin:roEnd, colBegin:colEnd) = chan4(j+((i-1)*IMinfo.YXGrid(1))).IM;
I suspect I haven't inserted the images properly is why. How did you do it? P.S. They are in grayscale.

Sign in to comment.


Don Zheng
Don Zheng on 17 Jul 2017
Declare an image with the final size and register each of the four images according to your layout to the final image.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!