## 3 Rail Track Detection Based on Approach of Computer Vision  
The rails can go straight or curved and can intersect with each other at switches. Concrete and wooden sleepers, vegetation, and other influencing factors that may affect the accuracy of detection should also be considered. This chapter will address the topic of the application of CV, including image processing, for the purpose of more accurate detection of rails. The entire process of CV applied in this paper can be divided into two subunits: edge detection and Hough transform, using a similar approach to [PERSON] et al. (2018). In order to implement the edge detection algorithms for finding rails in images, videos, and in real time, it was necessary to perform several operations with images. The images (.jpg format) were first loaded in the algorithm and returned as multi-dimensional matrices with filled pixel intensity values belonging to the image. Figure 3a shows the original image, which will undergo the necessary transformations, as described below. From a human point of view, the image can be defined as a 2D projection of the 3D world on the retina. Mathematically speaking, an image is a matrix of light, where the intensity of light at a point represents the value of a pixel in a matrix cell. In order to carry out further processing more efficiently, the conversion of the red, green, blue (RGB) image to grayscale followed. Grayscale images have only one channel, which makes them much simpler and more convenient to process. Thus, the switch from three channels to only one channel was performed, with a range of pixel values from 0 to 255, where a higher pixel value represents a cell with more light. Figure 3b shows the first step in the CV algorithm that was performed to prepare the image for detection. The next step in the CV algorithm was noise reduction by application of Gaussian filter. Figure 4a shows a blurred variant of the grayscale image. Noise can confuse the process, as it can create false edges. This procedure was used to average the pixel intensity values using a Gaussian distribution kernel (dimensions \(5\times 5\)). The filter passes through the whole image, starting from the coordinate origin and smoothing the pixel values. This procedure can also be considered as optional since the next step in the CV algorithm (Canny edge detection) encompass noise reduction by filtering. Figure 3: Railway infrastructure (Novi Sad–Belgrade route). (**a**) Original image; (**b**) grayscale image. The rapid change in brightness between the rails and the rest of the image is the reason why it was decided to apply the multi-step Canny edge detection algorithm presented. The application of the Canny edge method in railways has been considered in an number of papers [19; 20; 21; 22; 23]. The dimensions of the images on which the detection is performed are 1920 \(\times\) 1080 pixels, and the number of channels is one (after processing). Rapid changes in brightness, defined by the Canny edge detection algorithm, are presented in the form of derivative by both directions (x and y):  
\[\text{derivative(f(x,y))}. \tag{1}\]  
Each edge represents a sudden change in pixel intensity. A small derivative is a small change in intensity, while a large derivative is a large change. Comparing Figure 3a,b, it can be concluded that wherever objects and/or surroundings intersect, edges are separated. By calling Canny (conducting derivative in both directions), one actually calculates the gradients in both directions of the image. The white edges drawn in Figure 3b represent the strongest gradient. They are drawn as a series of white pixels. Since the gradient was larger than the upper defined threshold, the edges were accepted. The defined pixel intensity of the upper threshold was 150. All the edges that were below the lower defined threshold (in this case: 50) were not drawn--black space. The ratio between the thresholds was 3:1, finally defined by trial and error.