一、简介
基于matlab铁路接触网系统杆号识别
二、源代码
clc;clear;close all;
img_rgb=imread('支柱杆号.png');
I=img_rgb;
%% 灰度化及二值化
I1=rgb2gray(I);
I1=im2bw(I1);
I1=ROI(I1,3,10);
I1=bwareaopen(I1,100);
%% 垂向矫正
tic
BW=edge(I1,'canny');
[H,T,R]=hough(BW);
P=houghpeaks(H,4,'threshold',ceil(0.3*max(H(:))));
lines=houghlines(BW,T,R,P,'FillGap',50,'MinLength',7);
max_len = 0;
hold on;
for k=1:length(lines)
xy=[lines(k).point1;lines(k).point2];
len=norm(lines(k).point1-lines(k).point2);
Len(k)=len;
if (len>max_len)
max_len=len;
xy_long=xy;
end
end
[~,Index1]=max(Len(:));
K1=-(lines(Index1).point1(2)-lines(Index1).point2(2))/...
(lines(Index1).point1(1)-lines(Index1).point2(1));
angle=atan(K1)*180/pi;
I1 = imrotate(I1,-angle-90,'loose');
I1=ROI(I1,5,5);
%% 几何矫正
I1=Ajust(I1);
x=Xtrait(I1);
Lx=length(x);
for i=1:Lx-1
if x(i)>=x(i+1)
break
end
end
nX1=i;
for i=1:Lx-1-nX1
if x(Lx-i)>=x(Lx-i-1)
break
end
end
nX2=Lx-i;
I1=I1(:,nX1:nX2);
%% 移除小对象
I1=~I1;
I1=bwareaopen(I1,400);
I1=~I1;
%% ROI
I1=~I1;
I1=ROI(I1,1,1);
%% 切割
y=Ytrait(I1);
Py0=1;
figure();title('分割后');
for i=1:4
while ((y(Py0)<1)&&(Py0<length(y)))
Py0=Py0+1;
end
Py1=Py0;
while (((y(Py1)>=2)&&(Py1<length(y)))||((Py1-Py0)<50))
Py1=Py1+1;
end
Z=I1(Py0:Py1,:,:);
Py0=Py1;
subplot(1,4,i),imshow(Z);
switch strcat('Z',num2str(i))
case 'Z1'
PIN1=Z;
case 'Z2'
PIN2=Z;
case 'Z3'
PIN3=Z;
otherwise
PIN4=Z;
end
end
%% 分割后预处理
n1=CCmatching(PIN1);
n2=CCmatching(PIN2);
n3=CCmatching(PIN3);
n4=CCmatching(PIN4);
marknumber=n1*1000+n2*100+n3*10+n4;
output=num2str(marknumber);
disp(strcat('支柱杆号是:',output));
toc
function y=Xajust(x,m,n)
%%Xajust函数根据hough变换通过识别直线的方法矫正二值化图像x的水平方向
%m表示要提取的直线个数
%n表示figure(n)的n
bw=double(x);
BW=edge(bw,'canny');
[H,T,R]=hough(BW);
figure(n),subplot(1,3,1),imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');%'InitialMagnification'是设置图片显示初始放大率,100表示100%显示,这里的'fit'表示图片根据窗口的大小来显示
xlabel('\theta'),ylabel('\rho');
axis on, axis normal,hold on;%axis on表示显示坐标轴上的标记、单位和格栅;axis normal表示恢复坐标系的大小,取消对单元格的限制
P=houghpeaks(H,m,'threshold',ceil(0.3*max(H(:))));
x=T(P(:,2)); y = R(P(:,1));
plot(x,y,'s','color','white');
lines=houghlines(BW,T,R,P,'FillGap',50,'MinLength',7);%合并距离小于50的线段,丢弃所有长度小于7的直线段
subplot(1,3,2),imshow(BW),title('直线标识图像');
max_len = 0;
hold on;
for k=1:length(lines)
xy=[lines(k).point1;lines(k).point2];
% 标出线段
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% 标出线段的起始和终端点
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
len=norm(lines(k).point1-lines(k).point2);
Len(k)=len;
if (len>max_len)
max_len=len;
xy_long=xy;
end
end
% 强调最长的部分
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','blue');
[~,Index1]=max(Len(:));
% 最长线段的起始和终止点
x1=[lines(Index1).point1(1) lines(Index1).point2(1)];
y1=[lines(Index1).point1(2) lines(Index1).point2(2)];
%% 求得线段的斜率
s_s=zeros(X,1);
s_e=zeros(X,1);
for i=1:X
for j=1:Y
if x(j,i)==1
break;
end
end
s_s(i,1)=j;
end
for i=1:X
for j=1:Y
if x(Y-j+1,i)==1
break;
end
end
s_e(i,1)=Y-j+1;
end
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.