資源簡介
基于Hough變換的車牌子圖像提取,得到清晰的車牌區域,輸出直線方程與坐標點
代碼片段和文件信息
N?=?44/14;%車牌長寬比
%%%%%灰度圖轉換
I?=?imread(‘chepai2.jpg‘);
subplot(131);
imshow(I);
I1?=?rgb2gray(I);
subplot(132);
imshow(I1);
I11?=?medfilt2(I1);%中值濾波
subplot(133);
imshow(I11);
I2?=?edge(I11‘canny‘);%邊緣檢測
figure
imshow(I2);
se?=?[1;1;1];
I3?=?imerode(I2se);%圖像腐蝕
figure
imshow(I3);
%%%%%%灰度跳變閾值初步確定車牌區域
[x0y0]?=?size(I3);
s?=?zeros(1y0);
for??cnt1?=?1:x0
????for?cnt2?=?2:y0
????????if?abs(I3(cnt1cnt2)-I3(cnt1(cnt2?-?1)))==1
????????????s(cnt1)?=?s(cnt1)?+?1;
????????end
????end
????
end
r1?=?zeros(1x0);
for?cnt1?=?1:x0
???if?s(cnt1)?>=?20?%設置閾值為20
???????r1(cnt1)?=?cnt1;
???end
end
cnt3?=?0;%尋找車牌區域第一行,存在cnt3中
for?cnt1?=?1:x0
???if?r1(cnt1)?==?0
???????cnt3?=?cnt3?+?1;
???else
???????break;
???end
end
figure
A?=?10;
B?=?25;
I4?=?I1((cnt3-A):(max(r1)+B)1:480);%初步取出車牌區域
imshow(I4);
rotI?=?imrotate(I40‘crop‘);%圖像旋轉(若有必要)
I5?=?edge(rotI‘canny‘);%canny算子邊緣檢測
figure
imshow(I5);
Theta_test?=?1;%參數度量選擇
Rho_test?=?1;
[HTR]?=?hough(I5‘ThetaResolution‘Theta_test‘RhoResolution‘Rho_test);%Hough變換
%%%%%選取theta?=?0?時的直線
[x00x0]?=?size(T);
[ab]?=?size(H);
H0?=?zeros(ab);
for?cnt1?=?1:x0
???if?T(cnt1)?==?0
???????H0(:cnt1)?=?H(:cnt1);?
???end
end
T0?=?zeros(1x0);
for?cnt1?=?1:x0
???if?T(cnt1)?==?0
???????T0(1cnt1)?=?T(1cnt1);?
???end
end
P0?=?houghpeaks(H02‘threshold‘ceil(0.4*max(H0(:))));
H_mid?=?(H(P0(11)P0(12))?+?H(P0(21)P0(22)))/2;
lines0?=?houghlines(I5T0RP0‘FillGap‘50
評論
共有 條評論