An Application of Canny Edge Detection Algorithm to Rail Thermal Image Fault Detection

The paper discusses an application for rail track thermal image fault detection. In order to get better results from the Canny edge detection algorithm, the image needs to be processed in advance. The histogram equalization method is proposed to enhance the contrast of the image. Since a thermal image contains multiple parallel rail tracks, an algorithm has been developed to locate and separate the tracks that we are interested in. This is accomplished by applying the least squares linear fitting technique to represent the surface of a track. The performance of the application is evaluated by using a number of images provided by a specialised company and the results are essentially favourable.


Introduction
Rail track failure may directly leads to derailment of the train which may threat people's lives. Detecting rail track defects in advance can improve the reliability of the railway transport systems. In addition, the low efficiency and high error rate of manual detection is no longer appropriate for the current development. An application for automatic and effective fault detection is needed and the aim of this project is to cater for this.
Currently, the most popular non-destructive method to detect rail defects is ultrasonic techniques [1]. Ultrasonic testing needs the probe and the railhead contacts directly to detect the rail squat, but the false detections and missed detection are unavoidable. Infrared thermal imaging is a relatively new but widely used non-destructive inspection technology, it is not used in the detection of rail squats, but it can detect the surface of the tracks [2].
In order to detect the faults of the track surface, a vision based inspection technique [3] is adopted in this paper. The edge consists of a vast amount of information of an image, so the edge detection is treated as a key process in image processing and analysis. This information provides a foundation for image segmentation, character determination, image categorization and pattern recognition. Canny edge detection algorithm [4] is one of the most classical methods for edge detection and it specifies three principles, namely, low error rate, good localization and low spurious to a single edge. Even though the Canny Edge Detection is a good tool for edge detection, but it is not appropriate for any situations. This paper represents an improved version of the Canny algorithm to the processing of rail track thermal image.
The rest of the paper is organised as follows. Section 2 discusses the method to incorporate histograms with Canny Edge Detection Algorithm in order to process rail thermal image. Section 3 provides the method of applying the modified Canny algorithm to the rail track fault detection. Section 4 discusses the software application that has been developed for this purpose. An evaluation of the algorithm and the software tool has been conducted and documented in Section 5.

Incorporate Histograms with Canny Edge Detection Algorithm
Canny algorithm utilizes a mask generated by Gaussian function as Equation (1) to reduce the noise from original image and the first derivative Gaussian is shown in Equation (2) which is used for computing gradient. According to the features of Gaussian function and the consideration in terms of computation cost, one-dimensional mask is adapted to the solution instead of two-dimensional mask and also as the convolution computation [5].
In some original thermal images, the rail tracks are difficult to be distinguished from the background due to low contrast. Therefore, before reducing noise by Gaussian kernel, it is necessary to enhance the contrast of the raw thermal image because the value of gradient will be enhanced when calculating the potential edge by using the algorithm. As a result, the edge will be recognized easier. As an efficient approach to the contrast enhancement, histogram equalization (HE) [6] is incorporated to the Canny algorithm for the pre-processing as discussed below.
For an image with width N and length M, the total number of pixels is M*N, and let n i be the occurrence times in the total pixels with grey level x i , then p i, the probability of occurrence times of grey level x i , can be defined as Equation (3). L is the number of grey levels and in the current application its value is always 256 and this means the range of grey level is from 0 to 255. (4) Then, x min can be defined as the minimum level of grey when the value of transformation is first time greater than 0, x max can be defined as the maximum level of grey when the value of transformation is first time reach up to M*N respectively. Then let the equalized level of grey be r i , the definition of r i is shown in Equation (5).
After histogram equalization, the contrast of original image is strengthened and the new histogram should be distributed from 0 to 255 (the max level of grey). However, sometimes if the distribution of pixels which belong to noise or other objects is far from that of the rail, the effect of HE will be greatly reduced, or even completely

Rail Track Detection
The most significant step of rail detection is the determination of edges of rail from all edges detected by the Canny algorithm discussed in Section 2 above. This is one of the key requirements for rail thermal image detection. Before the recognition of the edges, the set of points on each edge can be collected by an iterative algorithm from 8 directions for each edge point. The algorithm is described below: Define an integer array with the size of the total number of pixels The current edge index increases by 1 after each iteration. The output from the algorithm above represents the set of edge points. Via analysing a large number of detected edge point results, it can be found that there are 3 features of rail edges in rail thermal images. Firstly, since the thermal cameras are positioned at the bottom of the train, the current rail track will always be in the lower half of the image. Secondly, in most of images with crossing over junction, the edges of rails are always displayed as a group of single lines, which are similar to horizontal lines but with a little lean. Thirdly, the edges of rail should be longer than other edges, or it should be combined with some edges which have the similar angle of tilt. Combining the above three features, the linear function with slope and intercept is selected as the characterization function.
Since the collection of points for each detected edge is not difficult to be obtained, the horizontal and vertical coordinates for each point that belongs to one edge can be used for calculating the fitted line by Linear Least Squares Fitting Algorithm [7] and all the edges can be expressed by a linear function with slope and intercept as shown in Equation (6), = ( ) = + where k is slope and b is intercept.

Railway Track Thermal Analyzer
By applying the algorithms and methods discussed in Section 2&3 above, a Railway Track Thermal Analyzer System has been implemented by a team of MSc Software Engineering students at the University of York [8].The tool can be used to analyze the proprietary LSJPI/LSJPG video files to automatically identify thermal hotspots in the rail captured by the camera. The system is divided into eight modules, as shown in Figure 2, which also details the flow of data between the modules.

Figure 2. Architecture of Railway Track Thermal Analyzer
ThermalAnalyzer is the main driver class for the application. It sets up the logging object, then obtains an instance of Controller and passes it the path supplied to the program by the user. The Controller module has the job of coordinating all the other main functional modules. If requested to run a single video file, the Controller obtains an instance of the FileReader module linked to that video file and requests a sample of frames from the video. It also obtains an instance of the EdgeDetector module and, for each sample frame, the Controller requests edge detection results from the EdgeDetector. Once it has all the edge detection results, the Controller requests the RegionCalculator module to analyze them and calculate the region of the video frame images that should be analyzed for hotspots. The aim is to restrict the analysis to the rail of interest to reduce processing overhead and to ignore hotspots in a second railway track often visible in the upper portion of the video image. The FileReader module is mainly responsible for reading and retrieving information from the video files. The video files are a proprietary format, comprising metadata and a series of greyscale, thermal JPG images. The FileReader reads and retrieves metadata from the LSJPI file and uses it to access the image data in the related LSJPG file. The EdgeDetector module is responsible for analyzing a video frame image and identifying the position of the edges that bound the rail of interest for analysis. It uses the Canny Edge Detection algorithm discussed in Section 2 above. The RegionCalculator takes the results of the EdgeDetector module and determines the area of the video image that should be analysed for hotspots, i.e. where in the image the rail of interest appears throughout the video. The RegionCalculator implements the algorithm described in Section 3.
The ThresholdCalculator module is a simple implementation that returns a fixed value, either as specified in the configuration file or the default value of 255. This value is used by the ImageAnalyzer to determine whether a frame image pixel is hot and thus potentially part of a hotspot. A desirable approach might be to calculate the appropriate threshold based on the qualities of each video. However, there was not time to investigate such a method and using a fixed value of the maximum possible brightness was suitable for the data observed so far. This functionality is included in a module (rather than a simple hard coded value) to enable easy modification should there be further work around threshold calculation.
The ImageAnalyzer module scans the specified region of a frame's image to find hotspots. Within the set region, it compares each pixel's brightness to the set threshold to determine whether it is bright enough to be considered hot. It keeps count of the hot pixels found and if there are more than a specific value (this value can be set in the configuration values; the default is 20), then the frame is deemed to have a hotspot. A more complex algorithm that checks locality of the hot pixels found (to prevent a hotspot being declared when the hot pixels are spread throughout the regions, for example) were desired, but this feature is not implemented in the current version for the system. We are catering this in the future. The DataRecorder module records and retrieves data relating to video frames to and from a database. The Logging module provides logging facilities, recording messages passed with a timestamp in text files.

Evaluation
The Algorithms and method discussed above have been evaluated by using the tool developed in Section 4 above. The data files for evaluation are provided by a specialised railway software company at York. Figure 3 illustrates the results for the processing with HE. The original image is shown in Figure 3(a) with its histogram in Figure  3(b). In Figure 3(c), the original image equalized with 5% as starting and 95% as ending point, which greatly enhance the contrast of the image. In addition, the histogram of the equalized image in Figure 3(d) can be clearly seen that the grey level of the image has been equalized. On the other hand, it should be noticed that although through this processing, the weak edges can be obtained more effectively, some part of data are also lost, which probably leads to image distortion. a b c d Figure 3. Histogram Equalization Processing Figure 4 shows the result for railway track detection. The original image is shown in Figure 4(a), the edges with red lines that are generated by Canny edge detection are displayed based on original image, which is shown in Figure 4(b). Note that the histogram equalization processing is performed before Canny edge detection, and the low threshold and high threshold are 2.5 and 7.5 respectively. The edges need to be filtered by a series of rules to get the region that the rail track belongs to. Using the algorithm described in Section 3 to collect all the points of edges. The test image with detected edges is shown in Figure 4(c), each of colours represents an edge. The points in an edge are used to fit a straight line, and the result of Linear Least Squares Fitting Algorithm is shown in Figure 4(d).
a b c d

Conclusion
A railway track thermal analyzer has been presented. The original image is pre-processed by using histogram equalization prior to Canny edge detection. An iterative algorithm is used to obtain appropriate edges of rail tracks. These edges are then filtered by some conditions to get the appropriate region of a track. The pixels in the rail track region are scanned to judge whether hotpots are present. The system is able to generate an output description file with hotspot information. In the future, more complicated situations other than hotspots need to be considered, for example, the recognition of the region of a rail joint. The threshold values in Canny edge detection are currently fixed and they can only be adjusted manually. It would be necessary to tune these thresholds for Canny edge detection and the adaptive thresholds would be useful to get a better result of edges.