# 歸一化交叉相關Normalization cross correlation (NCC)

NCC正如其名字，是用來描述兩個目標的相關程度的，也就是說可以用來刻畫目標間的相似性。一般NCC也會被用來進行圖像匹配，即在一個圖像中搜索與一小塊已知區域的NCC最高的區域作為對應匹配，然后對准整幅圖像。

part1.jpg

part2.jpg

templete.jpg

region.jpg

result.jpg

1. function NCC
2. close all
5.
6. imshow(part2);title('part2'
7. figure, imshow(part1);title('part1');
8. % 選取template和搜索區域region. Note:template的大小必須小於region
9. figure,
10. [sub_part2,rect_part2]=imcrop(part2);% template
11. [sub_part1,rect_part1]=imcrop(part1);% region
12.
13. figure, imshow(sub_part2)
14. figure, imshow(sub_part1);
15.
16. [x,y,z] = normcorr2(double(sub_part2(:,:,1)),double(sub_part1(:,:,1)));
18. %% 尋找偏置
19. [~,imax]=max(z(:));%
20. [yoffset,xoffset]=ind2sub(size(z),imax(1));
21. %% 圖像對齊
22. % 配對點在第一幅圖像中到邊界的距離
23.
24. rect_part1=floor(rect_part1);
25. left1=rect_part1(1)+xoffset;
26. right1=size(part1,2)-left1;
27. up1=rect_part1(2)+yoffset;
28. down1 = size(part1,1)-left1;
29. % 配對點在第二幅圖像中到邊界的距離
30. rect_part2=floor(rect_part2);
31. left2=rect_part2(1);
32. right2=size(part2,2)-left2;
33. up2=rect_part2(2);
34. down2 = size(part2,1)-up2;
35.
36. img=zeros(max(up1,up2)+max(down1,down2)+1,max(left1,left2)+max(right1,right2)+1,size(part1,3));
37. sx=max(left1,left2)+1
38. sy=max(up1,up2)+1
39. img(sy-up1+1:sy-up1+size(part1,1),sx-left1+1:sx-left1+size(part1,2),:)=part1;
40. img(sy-up2+1:sy-up2+size(part2,1),sx-left2+1:sx-left2+size(part2,2),:)=part2;
41. imshow(uint8(img));
42. end
43.
44.
45.
46. function [x,y,z]=normcorr2(I1,I2)
47. % I1,I2是單通道圖像，即矩陣
48. % I1 is template
49. % I2 is the region where match pairs are searched
50. [m1,n1]=size(I1);
51. [m2,n2]=size(I2);
52. z=conv2(I2,rot90(I1,2),'valid');
53. im2=cumsum(I2.^2,1);
54. im2=cumsum(im2,2);
55. sum1=sum(sum(I1.^2,1),2);
56. % nz=zeros(m2-m1+1,n2-n1+1);
57. wrapIm2=zeros(1+m2,1+n2);
58. wrapIm2(2:end,2:end)=im2;
59. nz=(wrapIm2(m1+1:m2+1,n1+1:n2+1)+wrapIm2(1:m2-m1+1,1:n2-n1+1)-wrapIm2(1:m2-m1+1,n1+1:n2+1)-wrapIm2(m1+1:m2+1,1:n2-n1+1))*sum1;
60. nz=sqrt(nz);
61. z=z./nz;
62. [x,y]=meshgrid(1:n2-n1+1,1:m2-m1+1);
63. end

1477398865849.jpg

1477398880386.jpg

1477398892560.jpg

1477398906100.jpg

1477398924878.jpg

http://cn.mathworks.com/help/images/examples/registering-an-image-using-normalized-cross-correlation.html.