?一、獲取代碼方式
獲取代碼方式1:
完整代碼已上傳我的資源:【矩陣檢測】基于matlab Hough霍夫變換矩陣檢測【含Matlab源碼 3563期】
點擊上面藍色字體,直接付費下載,即可。
獲取代碼方式2:
付費專欄Matlab圖像處理(初級版)
備注:
點擊上面藍色字體付費專欄Matlab圖像處理(初級版),掃描上面二維碼,付費29.9元訂閱海神之光博客付費專欄Matlab圖像處理(初級版),憑支付憑證,私信博主,可免費獲得1份本博客上傳CSDN資源代碼(有效期為訂閱日起,三天內(nèi)有效);
點擊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);
?三、運行結(jié)果
?四、matlab版本及參考文獻
1 matlab版本
2014a
2 參考文獻
[1]文海瓊,李建成.基于直方圖均衡化的自適應(yīng)閾值圖像增強算法[J].中國集成電路. 2022,31(03)文章來源:http://www.zghlxwxcb.cn/news/detail-756804.html
3 備注
簡介此部分摘自互聯(lián)網(wǎng),僅供參考,若侵權(quán),聯(lián)系刪除文章來源地址http://www.zghlxwxcb.cn/news/detail-756804.html
到了這里,關(guān)于【矩陣檢測】Hough霍夫變換矩陣檢測【含Matlab源碼 3563期】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!