Journal of Computer and Communications
Vol.03 No.11(2015), Article ID:61271,6 pages

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

Libo Cai1, Yu Ma1, Tangming Yuan1*, Haifeng Wang2*, Tianhua Xu3

1Department of Computer Science, University of York, Heslington, York, UK

2National Engineering Research Center of Rail Transportation Operation and Control Systems, Beijing Jiaotong University, Beijing, China

3Laboratory of Rail Traffic Control and Safety, Beijing Jiaotong University, Beijing, China

Received August 2015


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.


Fault Detection, Rail Thermal Image, Canny Edge Detection, Linear Least Squares

1. Introduction

Rail track failures may directly lead to train derailment which may cause destruction and threaten 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 fast moving railway industry, i.e. high speed rail. An application for automatic and effective fault detection is needed and the aim of this paper is to contribute to addressing this.

Currently, the most popular non-destructive method to detect rail defects is ultrasonic techniques [1]. Ultrasonic testing needs the direct contact between the probe and the railhead in order to detect the rail squat, but the false and missed detections are unavoidable. Infrared thermal imaging is a relatively new but widely used non- destructive inspection technology, and it can be used to detect the fault of the track surface rather than rail squats [2].

In order to detect the faults of the track surface, a vision based inspection technique [3] is adopted in this paper. Since an edge consists of a great deal of information for an image, edge detection is treated as the key process for image processing and analysis. The information provides the 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. Although Canny edge detection is a good tool for edge detection, it may not be directly applicable to every situation. 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 of incorporating histograms with Canny edge detection algorithm in order to process rail thermal image. Section 3 provides the method of applying the improved Canny algorithm to the rail track fault detection. Section 4 discusses the software application that has been specially developed for this purpose. An evaluation of the algorithm and the software tool has been conducted and documented in Section 5.

2. Incorporate Histograms with Canny Edge Detection Algorithm

Canny algorithm utilizes a mask generated by Gaussian function as Equation (1) to reduce the noise of the 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 of the computation cost, one-dimensional instead of two-dimensional mask is adapted to the solution as the convolution computation [5].



In some original thermal images, the rail tracks are difficult to be distinguished from the background due to the low contrast. Therefore, before reducing the noise by using Gaussian kernel, it is necessary to enhance the contrast of the raw thermal image, and the reason for this is that 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 image pre-processing as discussed below.

For an image with width N and length M, the total number of pixels is M * N, and let ni be the occurrence times in the total pixels with grey level xi, then pi, the probability of occurrence times of grey level xi, 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.


If let s(xi) is a transformation of p(xi) that is defined as Equation (4) below, the histogram generated by s(xi) is the cumulative histogram. For example, there is a histogram of one original thermal image demonstrated as Figure 1. The percentage of normal histogram is displayed with a bold line, while the cumulative histogram is drawn thinner than normal histogram line and presented as a monotonically increasing line.


Then, xmin can be defined as the minimum level of grey when the value of transformation is first time greater than 0, xmax 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 ri, the definition of ri is shown in Equation (5).


Figure 1. Histogram of gray distribution.

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 disappeared. Therefore, in this case, utilizing HE to enhance the contrast with starting and ending percentage instead of adopting xmin and xmax is a possible means to suppress the influence.

3. 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 for each edge can be collected by an iterative algorithm from eight directions for each edge point. The algorithm is described below:

Define an integer array with the size of the total number of pixels

Initialize the current edge index as 0

Scan from point P0 (0, 0) (top left of the image) until P1 (width-1, height-1) (bottom right of the image):

for each point, check whether it is an edge point or not,

if it is not then check the next point.

if the point is an edge point and the point is not marked with any index of edge,

then set current edge index as the value of the element that can represent the point from integer array defined earlier, and check all eight directions around the point (northwest, north, northeast, west, east, southwest and southeast) respectively. This checking should be iterative until meet the ending condition and should be marked with the same edge index. The current index number should be transferred as parameter. The current edge index increases by 1 after each iteration.

The output from the algorithm above represents the set of edge points. By analysing a large number of detected edge point results, it can be found that rail edges have three features in rail thermal images. Firstly, since the thermal cameras are positioned at the bottom of a 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.

4. 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.

Thermal Analyzer 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 grey scale, 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 Region Calculator 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 Region Calculator implements the algorithm described in Section 3.

Figure 2. Architecture of railway track thermal analyzer.

The Threshold Calculator 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 Image Analyzer 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 Data Recorder 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.

5. 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 the weak edges can be obtained more effectively, some part of data are also lost which probably leads to image distortion.

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

(a) (b)(c) (d)

Figure 3. Histogram equalization processing.

(a) (b)(c) (d)

Figure 4. Rail track detection.

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).

6. 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.


This work was supported by the National Natural Science Foundation of China (Grant No. 61473029), and the Science and Technology R&D program of China Railway Corporation (Grant No. 2014X008-A).

Cite this paper

Libo Cai,Yu Ma,Tangming Yuan,Haifeng Wang,Tianhua Xu, (2015) An Application of Canny Edge Detection Algorithm to Rail Thermal Image Fault Detection. Journal of Computer and Communications,03,19-24. doi: 10.4236/jcc.2015.311004


  1. 1. Clark, R. (2004) Rail Flaw Detection: Overview and Needs for Future Developments. NDTE International Independent Nondestructive Testing and Evaluation, 37, 111-118.

  2. 2. Peng, D. and Jones, R. (2013) NDI of Rail Squats and Estimating Defect Size and Location Using Lock-In Thermography. Engineering, 5, 29-38.

  3. 3. Deutschl, E., et al. (2004) Defect Detection on Rail Surfaces by a Vision Based System. IEEE Intelligent Vehicles Symposium, 507-511.

  4. 4. Canny, J. (1986) A Computational Approach to Edge Detection. IEEE Transactions on Pattern Analysis and Machine Intelligence, 8, 679-698.

  5. 5. Parker, J.R. (2010) Algorithms for Image Processing and Computer Vision. 2nd Edition, John Wiley & Sons.

  6. 6. Gonzalez, R. and Woods, R. (2008) Digital Image Processing. 3rd Edition, Prentice Hall.

  7. 7. Freedman, D. (2009) Statistical Models: Theory and Practice. Revised Edition, Cambridge University Press.

  8. 8. Cai, L., et al. (2005) Railway Track Thermal Analyzer System Maintenance Manual. MSc Software Engineering Team Project Group Report, University of York.


*Corresponding authors.