Demand
Consider an image, whose lines have been shifted by a few pixels. The image has been presented below :
Download to your working directory the file containing the original image: fichier2.bmp.
You can read it with “imread” MATLAB function and display with “image” function:
clear B;
B=imread('fichier2.bmp','bmp');
B=255*B;
image(B);
colormap(GRAY);
You must correct the offsets of the image lines (think xcorr function).
The current row of pixels and the next row are cross-correlated. The point with the largest correlation coefficient corresponds to the translation distance of this row relative to the previous row. If there is no translation, the difference between one line of the image and the next line should not be large.
B=imread('fichier2.bmp','bmp');
B=255*B;
image(B);
colormap(gray);
B_new=zeros(size(B));
B_new(1,:)=B(1,:);
j_sum=0;
for i=1:2054
B1=B(i,:);
B2=B(i+1,:);
[a,b]=xcorr(B1,B2,'coeff');
[m,j]=max(a);
j=j-3072;
j_sum=j_sum+j;
if(j_sum>0)
B2_2=[ones(1,j_sum)*255,B2(1:3072-j_sum)];
elseif(j_sum<0)
B2_2=[B2(-j_sum+1:3072),ones(1,-j_sum)*255];
elseif(j_sum==0)
B2_2=B2;
end
B_new(i+1,:)=B2_2;
end
image(B_new);
colormap(gray);
Repair results
Because some pixels are moved out of the image frame, the restored image is incomplete.