An algorithm for extracting groove rail area based on improved Hough transform

In order to improve the accuracy and real-time performance of the automatic cleaning of groove rails in modern trams, this paper proposes a groove rail region extraction algorithm based on improved Hough transform. First, in order to speed up the detection and remove noise, the algorithm performs a series of pre-processing on the images collected by the camera, and then use the Canny edge detection method to extract the edge feature information of the groove rail. Finally, the algorithm is improved on the basis of the traditional Hough transform method according to the actual environment. The algorithm proposes three constraints from the straight line length, the slope of the straight line and the distance between the left and right edges, making the algorithm more feasible and accurate in extracting groove rail area. The extraction accuracy reached 97.9%, and the average extraction speed was 0.1903s, laying the foundation for the automatic cleaning of trough rails of modern trams.


Introduction
With the increasingly prominent problems of urban traffic congestion, environmental pollution, and energy shortages, modern trams are gradually gaining popularity. As of December 31, 2018, 18 cities in China have opened modern tram lines, with a total of 38 operating lines, 541 stations, and a total mileage of 406.2km [1].
In order to ensure the sustainable and healthy development of modern trams, our primary task is to ensure the safety of its operation. The cleanliness of the groove rail of modern trams has also attracted attention. As there are many small-radius curves on the main line of modern trams, and conditions for mixing with social vehicles must be reserved, some sections need to be designed as green tracks. Generally, the main line uses grooved rails of similar models such as 59R2 and 60R2 [2]. Various kinds of garbage such as leaves, cigarette butts, bottle caps, etc., are easily accumulated in the groove rail, which affects the safety of modern trams. If it is not cleaned in time, it is very likely to cause a traffic accident [3]. In order to promote the automatic cleaning of trough rails for modern trams, this paper proposes a trough rail region extraction algorithm based on improved Hough transform, which extracts the groove rail part of the collected image, reduces unnecessary interference and improves track garbage detection accuracy.
2 Image pre-processing

Image graying
The image collected by the high-speed camera is an RGB three-channel color image, with R, G, and B corresponding to three colors of red, green, and blue respectively. In order to reduce the amount of calculation, the original image needs to be grayed out and the three color channels are merged into one channel. In addition, the color is easily affected by light in nature, and graying the image is beneficial to reduce the influence of light on the accuracy of extracting the groove rail area. The gray processing formula used this time is: = 0.299 × + 0.587 × + 0.144 × (1)

Remove noise
Visible noise interference in digital images is mainly affected by temperature and ISO sensitivity. During the actual acquisition of groove track images, due to the complex and diverse actual environment, it is easy to produce image noise interference and affect the accuracy of groove track area extraction. Both Gaussian filtering and bilateral filtering can effectively remove noise. The Gaussian filter is a linear smoothing filter. The Gaussian filter scans each pixel, performs a Gaussian weighted average of its neighboring pixels, and replaces the pixel value with this value to achieve the purpose of removing noise. However, Gaussian filtering only considers the spatial relationship between pixels, and does not consider the similarity between pixels. Even pixels located at the edge of the track will be processed by Gaussian filtering, which leads to blurred rail edges and is not conducive to edge detection.
Bilateral filtering is a kind of non-linear filtering that uses spatial Gaussian function to calculate pixel value and adds gray value similarity Gaussian function. The similarity of gray value Gaussian function does not process the pixels with large gray value changes at the boundary, and can well preserve the edge of the groove track. In contrast, bilateral filtering has the advantages of removing noise and preserving the boundary of the groove rail, so this experiment uses bilateral filtering to remove image noise interference. After a lot of experiments and comparisons, the best bilateral filter template is 9*9.

Edge detection
After grayscale and bilateral filtering, the track image will still have redundant noise. In order to remove these image noises, edge detection of the rail image is also required. The Canny edge detection algorithm can detect the edges in the image well while filtering out other interference information. Therefore, the Canny algorithm has better results in detecting edges. The main steps are as follows: Step1: Calculate image gradient and angle. Use the Sobel operator to calculate the image gradient in two directions on the smoothed image, using the formula: Step2: Non-maximum suppression. Use non-maximum suppression to the gradient magnitude of the image to refine the edges. Check each pixel on the image. For the surrounding points with the same gradient direction, take the one with the largest gradient amplitude and keep it, and filter out the points on the non-boundary to achieve the effect of thinning the edge of the groove rail.
Step3: Hysteresis threshold. In order to determine which are the real groove rail edges, two thresholds Vmin and Vmax are set. When the image gray gradient is lower than Vmin, the point is deleted; when the image gray gradient is higher than Vmax, the point is retained. For a point between Vmin and Vmax, you need to add an additional condition: if the point is connected to a point higher than Vmax, that is, it is connected to a point that has been identified as an edge, this point is also retained, otherwise the point delete.
Generally, the gray value of the actual groove rail is quite different from that of the ground. In order to detect the edge of the groove track more accurately, a number of simulation experiments are carried out to check, and finally the selected parameters are as follows: = 80 (4) = 120 (5)

Improved Hough transform line detection algorithm
Hough transform is based on the dual characteristics of points and lines, and uses the mapping relationship between Cartesian coordinate system and polar coordinate system to map a straight line in the rectangular coordinate system to a point in the ρ-θ parameter space. In the OpenCV coordinate system, the parameter description of a straight line is shown in the figure below: After the above series of image preprocessing, the groove-shaped rail edge feature is basically retained, and the Hough transform can be used to directly detect the straight line of the rail edge. However, considering the structural characteristics of the groove rail, it is easy to accumulate trash such as leaves and cigarette butts. If the traditional Hough transform is directly used to detect straight lines, it is very likely that the stems on the leaves and cigarette butts will be detected by mistake, and the groove rail area cannot be accurately extracted. It can be known from the actual environment of the groove rail that the rail area is in the middle of the image and passes through the entire image vertically. Therefore, based on the traditional Hough transform straight line detection algorithm, the filter conditions are specified according to the actual environment: (1) Straight length The resolution of the Huagu Power WP-UC500 industrial camera used in this experiment is 1440x1080. In the captured image, the rail vertically crosses the upper and lower boundaries, and the rail edge line is the longest line in the image, so the line length threshold is set to 500 pixels initially. However, through experiments, it has been found that image noise interference such as illumination, shadows, garbage, etc., greatly affects the detection of straight lines of the rail edge, leading to false detections and missed detections. After repeated experiments, the shortest length L=100pixel that is the most suitable for straight line detection of rail edges is obtained. This experiment uses the cv2.HoughLines(InputArray image, rho, theta, threshold) function in OpenCV.
Corresponding parameter description: InputArray is the input gray image, rho is the pixel accuracy, theta is the angle accuracy, and threshold is the straight line threshold. rho=1, theta= 180 , threshold=100.
(2) Straight line slope Generally, the camera that collects the rail image is perpendicular to the rail plane, so the edge of the rail to be detected is almost vertical to the screen. Also, considering that the camera may shake or tilt slightly when the vehicle is running, some straight lines have a certain inclination. In order to filter out straight lines that are not at the edge of the rail, combined with the above analysis, set the straight line slope ∈( ) under the premise of retaining a certain slope margin. If the slope of the detected straight line is not within this range, the straight line will be rejected.
(3) Left and right edge distance Multiple straight lines with similar slopes will be detected on the same edge of the track, and the final detection result only requires one straight line on the left and right edges. Therefore, it is necessary to filter out two straight lines that meet the distance between the left and right edges of the rail as the final detection result. Since each straight line runs through the entire image, the abscissa of the midpoint of the straight line is taken as the comparison parameter, combined with the height of the actual camera from the track plane and the image size, it can be concluded that the straight line distance between the left and right edges of the track should satisfy ∈( 2 , 4 5 ) . Where is the difference of the abscissa of the midpoint of the two straight lines, and is the width of the image collected by the camera.
The specific implementation method steps are: take the straight line with the smallest abscissa of the midpoint as the reference straight line that is the straight line of the left edge. Calculate the difference between the abscissas of the reference straight line and the midpoints of other straight lines in turn, eliminate straight lines that do not meet the requirements, and then average the abscissa of the midpoint of the filtered right edge straight line. Finally, the straight line with the smallest difference from the average value is taken as the right edge straight line.   After collecting a large number of groove rail images, the algorithm is used to extract the groove rail area, and calculate the extraction accuracy of the groove rail area and the average extraction speed of each image according to the running results. Compare the results with the method in literature [5], as shown in Table 1. Table 1. Setting Word's margins.

Extraction accuracy(%)
Average extraction speed(s/张) Literature [5] 93.87 0.046 Algorithm 97.9 0.190 This experiment environment is pycharm+python3.8+OpenCV, Intel Core CPU i3. A total of 94 pictures were detected, which took 19.7021s, and the average extraction speed of each image was 0.1903s. After manual inspection, 2 of them were mistakenly detected. The main reason is that the collected images are not clear enough and the rail edges are blurred, resulting in the detection result was wrong, and the correct extraction rate was 97.9%. Literature [5] mentions a fast extraction algorithm for orbit region using HSV color space conversion. The average extraction time of this method is 0.046s, and the extraction accuracy is 93.87%. Although the extraction speed of the algorithm mentioned in this paper is not as fast as the method in literature [5], its accuracy rate is higher than that in document x. It can extract groove-shaped rail area more accurately and has strong robustness, which is more suitable for automatic rail cleaning.