⛄一、获取代码方式
获取代码方式1:
完整代码已上传我的资源:【矩阵检测】基于matlab Hough霍夫变换矩阵检测【含Matlab源码 3563期】
点击上面蓝色字体,直接付费下载,即可。
获取代码方式2:
付费专栏Matlab图像处理(初级版)
备注:
点击上面蓝色字体付费专栏Matlab图像处理(初级版),扫描上面二维码,付费29.9元订阅海神之光博客付费专栏Matlab图像处理(初级版),凭支付凭证,私信博主,可免费获得1份本博客上传CSDN资源代码(有效期为订阅日起,三天内有效);
点击CSDN资源下载链接:1份本博客上传CSDN资源代码
⛄二、部分源代码
clc;clear;
tic
%%%step1, image input
F=imread(‘test3.bmp’);
figure, imshow(F);
title(‘输入图像.’);
%%%step2, image in the early proceesing
BW=im2bw(F,0.8);
figure,imshow(BW);
title(‘二值化图像.’);
se=strel(‘square’,2);
BW1=imopen(BW,se);
figure;imshow(BW1);
title(‘The image by open operation.’);
g=edge(BW1,‘canny’,0.2);
figure, imshow(g);
title(‘二值化图像边缘.’);
[K,T]=size(g);
I=zeros(K,T);
%%%step3,generate the edge image,finding dmax and dmin
[l,num]=bwlabel(g,8);%标定最大
[numax,numin]=maxmin(g,l,num);
center{numax}=[zeros(1,2)];
[dmax,gmax,center{numax}]=choose(g,l,numax);
[dmin,gmin,center{numin}]=choose(g,l,numin);
figure,imshow(g);
title(‘找到所有目标中心.’);
for k=1:num
geve{k}=[zeros(K,T)];center{k}=[zeros(1,2)];
[deve(k),geve{k},center{k}]=choose(g,l,k);
hold on
plot(center{k}(1),center{k}(2),‘+r’) %plot on original image
hold off
end
deve=zeros(num,1);
z=1;
Z=zeros(1,num);
for k=1:num %detect of each target whether is rectangle.
disp(‘The’);disp(k);disp(‘target is determined!’)
geve{k}=[zeros(K,T)];center{k}=[zeros(1,2)];
[deve(k),geve{k},center{k}]=choose(g,l,k);
%%%step4,hough transform
dtheta=3pi/(4dmax);
drho=3/4;
[h{k},theta{k},rho{k}]=hough(geve{k},dtheta,drho);
figure,imshow(h{k},‘XData’,theta{k},‘YData’,rho{k},‘InitialMagnification’,‘fit’);
axis on,axis normal;
xlabel(‘\theta’),ylabel(‘\rho’);
%%%step5,finding peaks in hough transform matrics
numpeaks=8;
threshold=round(0.5*max(h{k}(😃));%there are some problems about the parameter of nhood.
Q=houghpeaks(h{k},numpeaks,‘Threshold’,threshold);%r,c are coordinate of identified peaks
c{k}=Q(:,2);
r{k}=Q(:,1);
hold on
plot(theta{k}(c{k}),rho{k}(r{k}),‘linestyle’,‘none’,‘marker’,‘s’,‘color’,‘r’)
%%%step6,plot lines on original axis, edged image
fillgap=5;
minlength=5;
lines{k}=houghlines(geve{k},theta{k},rho{k},r{k},c{k},fillgap,minlength);
figure,imshow(geve{k}),hold on
for t=1:length(lines{k})
xy{k}=[lines{k}(t).point1;lines{k}(t).point2];
plot(xy{k}(:,2),xy{k}(:,1),‘linewidth’,2,‘color’,[1 0 0]);
end
hold on
plot(center{k}(1),center{k}(2),‘+r’)
hold off
%%%step7,determine the target whether is a rectangle
t=1;
Tc=3;
Tl=0.4;
Talpha=3;
for i=1:length(lines{k})
for j=i+1:length(lines{k})
C{k}(i)=lines{k}(i).length;
dtheta(i,j)=abs(lines{k}(i).theta-lines{k}(j).theta);
dC(i,j)=abs(lines{k}(i).length-lines{k}(j).length);
aC(i,j)=Tl*(lines{k}(i).length+lines{k}(j).length)/2;
end
end
for i=1:length(lines{k})
for j=i+1:length(lines{k})
if dtheta(i,j)<Tc && dC(i,j)<aC(i,j)
H{k}(i,:)={lines{k}(i).rho;lines{k}(i).theta};
H{k}(j,:)={lines{k}(j).rho;lines{k}(j).theta};
w{k}(t)=abs((lines{k}(i).rho-lines{k}(j).rho))/2;
alpha{k}(t)=(lines{k}(i).theta+lines{k}(j).theta)/2;
p{k}(t,:)={w{k}(t);alpha{k}(t)};
t=t+1;
disp(‘it might be a rectangle!’);
else
D{k}(i,:)={lines{k}(i).rho;lines{k}(i).theta};
D{k}(j,:)={lines{k}(j).rho;lines{k}(j).theta};
disp(‘it might not be a rectangle!’);
end
end
end
%%to decide wether these lines are composing rectangle,if true,output H,P,M and C
%%if not output D and C;
disp(‘H=’);H{k}=cell2mat(H{k});disp(H{k});
disp(‘P=’);P{k}=cell2mat(p{k});disp(P{k});
t1=max(size(P{k}(:😅));
t2=min(size(P{k}(:😅));
t4=max(P{k}(:,2));
t5=min(P{k}(:,2));
if t2<2
N{k}=P{k};
disp(‘The target is not a rectangle!’)
else
if t1>=2
[r2,c1]=find(P{k}t4);
[r3,c2]=find(P{k}t5);
t6=max(size(r2));
t7=max(size(r3));
if t61 && t71
N{k}(1,1)=P{k}(r2,1);
N{k}(1,2)=P{k}(r2,2);
N{k}(2,1)=P{k}(r3,1);
N{k}(2,2)=P{k}(r3,2);
else
if t6>1 && t71
N{k}(1,1)=P{k}(r2(1,1),1);
N{k}(1,2)=P{k}(r2(1,1),2);
N{k}(2,1)=P{k}(r3,1);
N{k}(2,2)=P{k}(r3,2);
else if t61 && t7>1
N{k}(1,1)=P{k}(r2,1);
N{k}(1,2)=P{k}(r2,2);
N{k}(2,1)=P{k}(r3(1,1),1);
N{k}(2,2)=P{k}(r3(1,1),2);
else if t6>1 && t7>1
N{k}(1,1)=P{k}(r2(1,1),1);
N{k}(1,2)=P{k}(r2(1,1),2);
N{k}(2,1)=P{k}(r3(1,1),1);
N{k}(2,2)=P{k}(r3(1,1),2);
end
end
end
end
end
end
disp(‘N=’);disp(N{k});
t3=max(size(N{k}(:😅));
t8=min(size(N{k}(:😅));
if t32 && t82
dalpha=abs(abs(N{k}(1,2)-N{k}(2,2))-90);
if dalpha<Talpha
Z(1,z)=k;
z=z+1;
disp(‘It is a rectangle.’);
else
disp(‘D=’); D{k}=cell2mat(D{k});disp(D{k});
disp(‘C=’);disp(C{k});
disp(‘It is not a rectangle.’);
end
else
disp(‘It is not a rectangle.’);
end
end
%%%step8, output all determined rectangle and label it in BW image.
disp(‘There are’);disp(z-1);disp(‘rectangles in this image!’);
figure;imshow(BW);hold on;
for z1=1:num
if Z(1,z1)~=0
for t=1:length(lines{Z(1,z1)})
xy{Z(1,z1)}=[lines{Z(1,z1)}(t).point1;lines{Z(1,z1)}(t).point2];
plot(xy{Z(1,z1)}(:,2),xy{Z(1,z1)}(:,1),‘linewidth’,2,‘color’,[1 0 0]);
end
hold on
plot(center{Z(1,z1)}(1),center{Z(1,z1)}(2),‘+r’);
end
end
title(['Threre are ‘,num2str(z-1), ’ rectangles in this image.’]);
hold off
ts=toc;
disp(‘The total time for determination:’);disp(ts);
⛄三、运行结果
⛄四、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1]文海琼,李建成.基于直方图均衡化的自适应阈值图像增强算法[J].中国集成电路. 2022,31(03)文章来源:https://www.toymoban.com/news/detail-756804.html
3 备注
简介此部分摘自互联网,仅供参考,若侵权,联系删除文章来源地址https://www.toymoban.com/news/detail-756804.html
到了这里,关于【矩阵检测】Hough霍夫变换矩阵检测【含Matlab源码 3563期】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!