MATLAB: Pasting multiple images in a line onto another image

aligning imagesImage Processing Toolboxpasting images

Best Answer

Actually, I think I've figured it out! Just use the row sizes of each of the images to determine the placement of the next image. It's a little messy but I think it's a good start :)
if true
% % Lets user drag out a box on an image, then define where they want to paste it.
% Then it pastes the drawn region onto the original image.
% Figure out how to select two points for pasting
clc; % Clear the command window.

close all; % Close all figures (except those of imtool.)

imtool close all; % Close all imtool figures.

clear; % Erase all existing variables.

workspace; % Make sure the workspace panel is showing.

fontSize = 20;
format compact;
grayImage = imread('Blackout.png');
% Get the dimensions of the image.

% numberOfColorBands should be = 1.

[I1R I1C numberOfColorBands] = size(grayImage);
% Display the original gray scale image.

subplot(1, 3, 1);
imshow(grayImage);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.

set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
Display the cropped image.
rotatedImage=imread('rotCoxa.png');
subplot(1, 3, 2);
imshow(rotatedImage);
axis on;
title('Region that you defined', 'FontSize', fontSize);
% Paste it onto the original image

[I2R I2C] = size(rotatedImage);
x = 1;
y = I1R/2;
% Determine the pasting boundaries.

r1 = int32(y);
c1 = int32(x);
r2 = r1 + I2R - 1;
r2 = min([r2 I1R]);
c2 = c1 + I2C - 1;
c2 = min([c2, I1R]);
plot([c1 c2 c2 c1 c1], [r1 r1 r2 r2 r1], 'r-');
% Paste as much of croppedImage as will fit into the original image.

grayImage(r1:r2, c1:c2) = rotatedImage(1:(r2-r1+1), 1:(c2-c1+1));
subplot(1, 3, 3);
imshow(grayImage);
axis on;
title('Region that you defined pasted onto original', 'FontSize', fontSize);
%save rotated image

imwrite(grayImage, 'alignedImage.png');
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
%delete('Blackout.png');
grayImage = imread('alignedImage.png');
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[I1R I1C numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(1, 3, 1);
imshow(grayImage);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
Display the cropped image.
rotatedImage=imread('rotTrochanter.png');
subplot(1, 3, 2);
imshow(rotatedImage);
axis on;
title('Region that you defined', 'FontSize', fontSize);
% Paste it onto the original image
[I3R I3C] = size(rotatedImage);
previouslyrotatedImage=imread('rotCoxa.png');
[I2R I2C] = size(previouslyrotatedImage);
x = I2C;
y = I1R/2 + I2R/2 - I3R; %check math. I3R/2 or I3R?
% Determine the pasting boundaries.
r1 = int32(y);
c1 = int32(x);
r2 = r1 + I3R - 1;
r2 = min([r2 I1R]);
c2 = c1 + I3C - 1;
c2 = min([c2, I1C]);
plot([c1 c2 c2 c1 c1], [r1 r1 r2 r2 r1], 'r-');
% Paste as much of croppedImage as will fit into the original image.
grayImage(r1:r2, c1:c2) = rotatedImage(1:(r2-r1+1), 1:(c2-c1+1));
subplot(1, 3, 3);
imshow(grayImage);
axis on;
title('Region that you defined pasted onto original', 'FontSize', fontSize);
%save rotated image
imwrite(grayImage, 'alignedImage.png');
end