function [epsilon,eta,C] = zxcor( f,D,m,n )
% 自相关函数zxcor(),f为读入的图像数据,D为偏移距离,[m,n]是图像的尺寸数据,返回图像相关函数C的值
% epsilon和eta是自相关函数C的偏移变量
for epsilon=1:D
for eta=1:D
temp = 0;
fp = 0;
for x=1:m
for y=1:n
if(x+epsilon-1)>m | (y+eta-1)>n
f1=0;
else
f1 = f(x,y)*f(x+epsilon-1,y+eta-1);
end
temp = f1+temp;
fp = f(x,y)*f(x,y)+fp;
end
end
f2(epsilon,eta)=temp;
f3(epsilon,eta)=fp;
C(epsilon,eta) = f2(epsilon,eta)/f3(epsilon,eta);
end
end
epsilon = 0:(D-1);
eta = 0:(D-1);
end
调用函数的测试代码如下:
close all;clear all;clc;
f11 = imread('zhuanqiang.jpg');
f1 = rgb2gray(f11);
f1 = double(f1);
[m,n] = size(f1);
D = 20;
[epsilon1,eta1,C1]=zxcor(f1,D,m,n);
f22 = imread('dalishi.jpg');
f2 = rgb2gray(f22);
f2 = double(f2);
[m,n] = size(f2);
[epsilon2,eta2,C2]=zxcor(f2,20,m,n);
figure;
subplot(121);imshow(f11);title('砖墙');
subplot(122);imshow(f22);title('大理石');
figure;
subplot(121);mesh(epsilon1,eta1,C1);
xlabel('epsilon');ylabel('eta');title('砖墙');
subplot(122);mesh(epsilon2,eta2,C2);
xlabel('epsilon');ylabel('eta');title('大理石');