fileIn = 'lab3/image%d.jpg';
fileOut = 'lab3/image%d-ncc.jpg';

for i = 1:1:6
I = imread(sprintf(fileIn, i));

B = im2double(I(1:342, 1:end));
G = im2double(I(342:683, 1:end));
R = im2double(I(683:1024, 1:end));


c = normxcorr2(G, B);
[ypeak, xpeak] = find(c==max(c(:)));
yoffset = ypeak - size(G, 1);
xoffset = xpeak - size(G, 2);
BG = circshift(G,[yoffset xoffset]);

fprintf('Image: %d, Green X-Align: %d   Y-Align: %d \n', i, yoffset, xoffset)

c = normxcorr2(R, B);
[ypeak, xpeak] = find(c==max(c(:)));
yoffset = ypeak - size(R, 1);
xoffset = xpeak - size(R, 2);
BR = circshift(R,[yoffset xoffset]);

fprintf('Image: %d, Red X-Align: %d   Y-Align: %d \n', i, yoffset, xoffset)

COLOR = cat(3, BR, BG, B);

imwrite (COLOR, sprintf(fileOut, i));
end
