方向模板匹配的精度往往受限模板的方向,并且由于大量的匹配运算消耗当量的时间,纹理复杂的物面会使条纹向更多方向发生偏移,优点就是能够克服白噪声干扰,具有在一定程度上修补断线的能力,通常方向模板和灰度重心法相结合较为常见。
clear;
clc
I = imread('6.png');
img = imread('6.png');
if ndims(I) == 3
I = rgb2gray(I);
end
thresh =20;
[r,c] = size(I);
for i = 1:r
for j = 1:c
if I(i,j) < thresh
I(i,j) = 0;
end
end
end
%创建预定义滤波算子
gaussFilter = fspecial('gaussian',[7 7],5);
%对图像进行滤波,‘replicate’表示图像大小通过复制外边界的值来扩展
I = imfilter(I,gaussFilter,'replicate');
%灰度拉伸,增强图像对比
I = im2uint8( mat2gray( log(1 + double(I)) ) );
imshow(I);
I = double(I)/255;
%二值化图像
thresh = graythresh(I);
I1=im2bw(I,thresh);
imshow(I1);
%膨胀处理
I1=bwmorph(I1,'dilate',10);
imshow(I1);
%腐蚀
se = strel('disk',6);
I1=imclose(I1,se);
I1 = bwmorph(I1,'thin',Inf);
% bwl=bwmorph(I,'skel',Inf);
% figure;
imshow(I1,'Border','tight');
% [r,c]=size(I1);
% T1=[0 0 0 0 1 0 0 0 0;
% 0 0 0 0 1 0 0 0 0;
% 0 0 0 0 1 0 0 0 0;
% 0 0 0 0 1 0 0 0 0;
% 0 0 0 0 1 0 0 0 0;
% 0 0 0 0 1 0 0 0 0;
% 0 0 0 0 1 0 0 0 0;
% 0 0 0 0 1 0 0 0 0;
% 0 0 0 0 1 0 0 0 0
% ];
%
% T2=[0 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0 0;
% 1 1 1 1 1 1 1 1 1;
% 0 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0 0;
% 0 0 0 0 0 0 0 0 0
% ];
%
% T3=[1 0 0 0 0 0 0 0 0;
% 0 1 0 0 0 0 0 0 0;
% 0 0 1 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 0 0 0 0 0 0 0 0 0 0 0
];
T3=[1 0 0 0 0 0 0 0 0 0 0 0 0;
0 1 0 0 0 0 0 0 0 0 0 0 0;
0 0 1 0 0 0 0 0 0 0 0 0 0;
0 0 0 1 0 0 0 0 0 0 0 0 0;
0 0 0 0 1 0 0 0 0 0 0 0 0;
0 0 0 0 0 1 0 0 0 0 0 0 0;
0 0 0 0 0 0 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 1 0 0 0 0 0;
0 0 0 0 0 0 0 0 1 0 0 0 0;
0 0 0 0 0 0 0 0 0 1 0 0 0;
0 0 0 0 0 0 0 0 0 0 1 0 0;
0 0 0 0 0 0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 0 0 0 0 0 1
];
T4=[0 0 0 0 0 0 0 0 0 0 0 0 1;
0 0 0 0 0 0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 0 0 0 1 0 0;
0 0 0 0 0 0 0 0 0 1 0 0 0;
0 0 0 0 0 0 0 0 1 0 0 0 0;
0 0 0 0 0 0 0 1 0 0 0 0 0;
0 0 0 0 0 0 1 0 0 0 0 0 0;
0 0 0 0 0 1 0 0 0 0 0 0 0;
0 0 0 0 1 0 0 0 0 0 0 0 0;
0 0 0 1 0 0 0 0 0 0 0 0 0;
0 0 1 0 0 0 0 0 0 0 0 0 0;
0 1 0 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 0 0 0 0
];
% [H,W] = size(I);
% I = [zeros(7,W);I;zeros(7,W)];
% I = [zeros(H+14,7),I,zeros(H+14,7)];
% [H,W] = size(I1);
% I1 = [zeros(7,W);I1;zeros(7,W)];
% I1 = [zeros(H+14,7),I1,zeros(H+14,7)];
[r,c] = size(I1);
H1=zeros(r,c);
H2=zeros(r,c);
H3=zeros(r,c);
H4=zeros(r,c);
num1=zeros(11,1);
num2=zeros(11,1);
centerU = [];
centerV = [];
center = [];
ccc=0;
%模板匹配,遍历模板中的点与骨架上的每一个点,如果匹配度最高,必定乘积最大
for u=7:r-7
for v=7:c-7
if I1(u,v)
for i=1:13
for j=1:13
H1(u,v)=I1(u-7+i,v-7+j).*T1(i,j)+H1(u,v);
H2(u,v)=I1(u-7+i,v-7+j).*T2(i,j)+H2(u,v);
H3(u,v)=I1(u-7+i,v-7+j).*T3(i,j)+H3(u,v);
H4(u,v)=I1(u-7+i,v-7+j).*T4(i,j)+H4(u,v);
end
end
data=[H1(u,v),H2(u,v),H3(u,v),H4(u,v)];
h=find(data==max( data));
cc=length(h);
if cc>1
h=h(2);
end
if h==1
for k = 1:9
num1(k) = I(u-5+k,v)*(u-5+k);
num2(k) = I(u-5+k,v);
end
centerU = sum(num1)/sum(num2);
centerV = v;
center = [center;[centerV,centerU]];
end
if h==2
for k = 1:9
num1(k) = I(u,v-5+k)*(v-5+k);
num2(k) = I(u,v-5+k);
end
centerU = sum(num1)/sum(num2);
centerV = u;
center = [center;[centerU,centerV]];
end
if h==3
for k = 1:9
num1(k) = I(u+5-k,v-5+k)*(v-5+k);
num2(k) = I(u+5-k,v-5+k);
end
centerU = sum(num1)/sum(num2);
for k = 1:9
num1(k) = I(u+5-k,v-5+k)*(u+5-k);
num2(k) = I(u+5-k,v-5+k);
end
centerV = sum(num1)/sum(num2);
center = [center;[centerU,centerV]];
end
if h==4
for k = 1:9
num1(k) = I(u-5+k,v-5+k)*(v-5+k);
num2(k) = I(u-5+k,v-5+k);
end
centerU = sum(num1)/sum(num2);
for k = 1:9
num1(k) = I(u-5+k,v-5+k)*(u-5+k);
num2(k) = I(u-5+k,v-5+k);
end
centerV = sum(num1)/sum(num2);
center = [center;[centerU,centerV]];
end
end
end
end
% center=sortrows(center,1);
figure;
imshow(img,'Border','tight')
hold on
plot(center(:,1),center(:,2),'r.')
f = getframe(gcf)
imwrite(f.cdata, 'qu6-1.png')
% hold on
% plot(b,a,'b.')
我建立了一个激光条纹提取的群,欢迎小伙伴加入,学习交流:点击链接加入群聊【激光条纹中心提取】 先申明本人是学生,不涉及营销、宣传、推广、盈利,单纯为了学习交流,所以心怀不轨的请绕道!!!