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

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

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

bestSSDG = realmax;
bestHeightG = 0;
bestWidthG = 0;

bestSSDR = realmax;
bestHeightR = 0;
bestWidthR = 0;

for height = -30:1:30
for width = -30:1:30

ssdG = sum(sum(B - circshift(G,[height width])).^2);
ssdR = sum(sum(B - circshift(R,[height width])).^2);

if(ssdG < bestSSDG) 
bestSSDG = ssdG;
bestHeightG = height;
bestWidthG = width;
end

if(ssdR < bestSSDR) 
bestSSDR = ssdR;
bestHeightR = height;
bestWidthR = width;
end

end
end

fprintf('Image: %d, Green X-Align: %d   Y-Align: %d \n', i, bestWidthG, bestHeightG)
fprintf('Image: %d, Red X-Align: %d   Y-Align: %d \n', i, bestWidthR, bestHeightR)

BG = circshift(G,[bestHeightG bestWidthG]);
BR = circshift(R,[bestHeightR bestWidthR]);

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

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





