clear; clc;
% result:
% vertical: 52
% horizon: 48
%% read img data
img1 = imread('one.jpg');
img2 = imread('two.jpg');
img1 = rgb2gray(img1);
img2 = rgb2gray(img2);
[row, column] = size(img1);
%% project the src(source) img in horizon and vertical
horizonImg1 = zeros(1,row);
horizonImg2 = zeros(1,row);
verticalImg1 = zeros(1, column);
verticalImg2 = zeros(1, column);
for i=1:row
horizonImg1(1,i) = sum(img1(i,:));
horizonImg2(1,i) = sum(img2(i,:));
end
for i=1:column
verticalImg1(1,i) = sum(img1(:,i));
verticalImg2(1,i) = sum(img2(:,i));
end
%% plot the project of src img
figure,
% subplot(211),
verticalImg1_projection_src = verticalImg1;
plot(verticalImg1_projection_src,'-or','markersize',3)
hold on
% title('vertical-1')
% subplot(212),
verticalImg2_projection_src = verticalImg2;
plot(verticalImg2_projection_src,'-ob','markersize',3)
% title('vertical-2')
title('vertical - src')
xlabel('方向')
ylabel('the sum of pixel value')
figure,
% subplot(211),
horizonImg1_projection_src = horizonImg1;
plot(horizonImg1_projection_src,'-or','markersize',3)
hold on;
% title('horizon-1')
% subplot(212),
horizonImg2_projection_src = horizonImg2;
plot(horizonImg2_projection_src,'-ob','markersize',3)
% title('horizon-2')
title('horizon - src')
xlabel('方向')
ylabel('the sum of pixel value')
%% compute the error in horizon, the image 1 is referenced
region = 200; % 匹配总范围
stepHorizon = 2; % 步长
start = 2; % 匹配起点
errorHorizon = inf * ones(1, floor(region-start)/stepHorizon+1);
for position=start:stepHorizon:region
horizonImg1_ = horizonImg1(1,1:row - position + 1);
horizonImg2_ = horizonImg2(1,position:row);
errorHorizon(1,(position-start)/stepHorizon+1) = sum(abs(horizonImg1_ - horizonImg2_));
end
minErrorHorizon = min(errorHorizon);
minErrorIndexHorizon = stepHorizon * (find(errorHorizon == minErrorHorizon) - start) + start; % may be not only one
%% compute the error in vertical, the image 1 is referenced too
regionVertical = 200;
stepVertical = 2;
start = 2;
errorVertical = inf * ones(1, floor(regionVertical - start)/stepVertical + 1);
for position=start:stepVertical:regionVertical
verticalImg1_ = verticalImg1(1,1:column-position+1);
verticalImg2_ = verticalImg2(1,position:column);
errorVertical(1, (position - start) / stepVertical + 1) = sum(abs(verticalImg2_ - verticalImg1_));
end
minErrorVertical = min(errorVertical);
minErrorIndexVertical = stepVertical * (find(errorVertical == minErrorVertical) - start) + start; % may be not only one
%% change the no-reference image
img2 = imread('two.jpg');
img2 = img2(minErrorIndexHorizon(1):end, minErrorIndexVertical(1):end,:);
figure,imshow(img2)
title('align - img2')
img1 = imread('one.jpg');
img1 = img1(1:row-minErrorIndexHorizon, 1:column-minErrorIndexVertical, :);
figure, imshow(img1);
title('align - img1')
[row, column] = size(img1);
horizonImg1 = zeros(1,row);
horizonImg2 = zeros(1,row);
verticalImg1 = zeros(1, column);
verticalImg2 = zeros(1, column);
%% compute the align images and gray projection curve
for i=1:row
horizonImg1(1,i) = sum(img1(i,:));
horizonImg2(1,i) = sum(img2(i,:));
end
for i=1:column
verticalImg1(1,i) = sum(img1(:,i));
verticalImg2(1,i) = sum(img2(:,i));
end
figure,
% subplot(211),
plot(verticalImg1,'-or','markersize',3)
hold on
plot(verticalImg1,'-or','markersize',3)
% title('vertical-1')
% subplot(212),
plot(verticalImg2,'-ob','markersize',3)
% title('vertical-2')
title('vertical - after align')
figure,
% subplot(211),
plot(horizonImg1,'-or','markersize',3)
hold on;
% title('horizon-1')
% subplot(212),
plot(horizonImg2,'-ob','markersize',3)
% title('horizon-2')
title('horizon - after align')