A list,
Railway catenary system bar number identification based on MATLAB
Ii. Source code
clc; clear; close all; Img_rgb =imread('.png'); I=img_rgb; %% gray and binarization 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); %% Remove small object I1=~I1; I1 = bwareaopen (I1, 400); I1=~I1; %% ROI I1=~I1; I1 = ROI (I1, 1, 1); % % y = Ytrait cutting (I1); Py0=1; figure(); Title (' after split '); 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; 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(' strcat ',output)); Toc function y=Xajust(x,m,n) %%Xajust function corrects the horizontal direction of binary image X by identifying straight lines according to Hough transform %m represents the number of straight lines to be extracted %n represents the n bw of figure(n) =double(x); BW=edge(bw,'canny'); [H,T,R]=hough(BW); ,3,1 figure (n), subplot (1), imshow (H, [], 'XData' T, 'YData, R,' InitialMagnification ', 'fit'); %'InitialMagnification' sets the InitialMagnification of the image. 100 means 100%. 'fit' means that the image is displayed according to the window size. axis on, axis normal,hold on; %axis on displays marks, units, and grids on the axes; Axis normal means to restore the size of the coordinate system and remove the limit on cells 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); % merge lines less than 50, discard all lines less than 7 subplot(1,3,2),imshow(BW),title(' line identifies image ') max_len = 0; hold on; for k=1:length(lines) xy=[lines(k).point1;lines(k).point2]; % mark line plot (x, y (:, 1), y (:, 2), 'our LineWidth, 2,' Color ', 'green'); Mark line segment of initiation and termination point % plot (x, y (1, 1), y (1, 2), 'x', 'our LineWidth, 2,' Color ', 'yellow'); Plot (x, y (2, 1), y (2, 2), 'x', 'our 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 % emphasizes the longest part of the plot (xy_long (:, 1), xy_long (:, 2), 'our LineWidth, 2,' Color ', 'blue'); [~,Index1]=max(Len(:)); [lines(Index1).point1(1) lines(Index1).point2(1)]; y1=[lines(Index1).point1(2) lines(Index1).point2(2)]; The slope of line segment 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; endCopy the code
3. Operation results
Fourth, note
The code download www.cnblogs.com/ttmatlab/p/…