An Alternative Vehicle Counting Tool Using the Kalman Filter within MATLAB

This study proposes an alternative and economical tool to estimate traffic densities, via video-image processing adapting the Kalman filter included in the Matlab code. Traffic information involves acquiring data for long periods of time at stationary points. Vehicle counting is vital in modern transport studies, and can be achieved by using different techniques, such as manual counts, use of pneumatic tubes, magnetic sensors, etc. In this research however, automatic vehicle detection was achieved using image processing, because it is an economical and sometimes even faster option. Commercial automatic vehicle detection and tracking programs/applications already exist, but their use is typically prohibitive due to their high cost. Large cities can obtain traffic recordings from surveillance cameras and process the information, but it is difficult for smaller towns without such infrastructure or even assigned budget. The proposed tool was developed taking into consideration these difficult situations, and it only requires users to have access to a fixed video camera placed at an elevated point (e.g. a pedestrian bridge or a light pole) and a computer with a powerful processor; the images are processed automatically through the Kalman filter code within Matlab. The Kalman filter predicts random signals, separates signals from random noise or detects signals with the presence of noise, minimizing the estimated error. It needs nevertheless some adjustments to focus it for vehicle counting. The proposed algorithm can thus be adapted to fit the users’ necessities and even the camera’s position. The use of this algorithm allows to obtain traffic data and may help small cities ́ decision makers dealing with present and future urban planning and the design or installment of transportation systems.


Introduction
An accurate estimation of vehicle densities is considered as a significant factor for managing traffic operations in urban zones.Traffic information includes vehicle counting, speed measurements, determination of peak hours, number of pedestrians, etc. [1].Large cities usually do not have problems acquiring such information, because they have an adequate infrastructure to obtain it such as surveillance cameras throughout the city, speed check points and continuous vehicle counting.However, in smaller cities, without such infrastructure or even an assigned budget, traffic data should be attained in a manual manner or by adapting available technology.
Typically, vehicle counting is achieved by using different techniques, such as manual counts, pneumatic tubes, magnetic sensors, inductive loops, piezoelectric sensors, acoustic detectors, passive infrared, doppler and radar microwave sensors [2,3].However, sensor measurements exhibit estimation errors, and sometimes, present difficulties to install and/or remain fixed.For optimizing vehicle counting, automatic video-based tracking systems are recommended [4].The system detects and tracks vehicles while they pass through the camera, providing information such as vehicle count, average speed, or traffic congestions [5][6][7].To achieve it, an algorithm must be created that segments and tracks the vehicle movements within the video [6,8,9].
Although automatic vehicle counting software already exist, and some of them use the Kalman filter [7,[10][11][12][13][14], their price is elevated, and in some cases, may be prohibitive.An alternative vehicle counting tool for low budget conditions is proposed, which involves video-image processing with the Kalman filter in Matlab.Users will only need a fixed camera on a high point (e.g.pedestrian bridge, light pole) and a computer with a powerful processor.The video is processed automatically through a Matlab code.

Background
The developed vehicle counting tool was tested in an express way, called Periferico de la Juventud (PDJ), considered the most important traffic road of Chihuahua City, which is the capital of the state of Chihuahua, located in northern Mexico (Fig. 1).In 2010, the city had a population of 809,232 inhabitants, with a ratio of one vehicle for every two inhabitants [15], and nowadays, approximately 500,000 vehicles are registered in the city [16].Chihuahua City is considered a metropolitan zone, and its road infrastructure occupies about the 42% of urban area, and contains a dozen main roads, with traffic jam issues during peak hours [17].PDJ is located west of Chihuahua City; its length is ca.20 km and has a N-S direction (Fig. 1).According to Chihuahua city's planning department, PDJ was not properly designed, resulting in a series of bottlenecks appearing due to incorrect location of in/out ramps [17].PDJ does not have traffic lights, and communicates both residential and business zones along its way.
Currently, Chihuahua City faces public transportation problems [18]; the number of automobiles has augmented, requiring a larger circulation space within the city [17].Therefore, it is vital to obtain vehicle counting data to be able to simulate traffic scenarios, and thus to propose answers integrating displacements of pedestrians, drivers, cyclists, and public transportation users.

The First Test Case: Ideal Dam Break Over Movable Bed
Currently, image processing is considered as a suitable tool to improve image quality or to look for information amongst them.Analyzing images in real time involves several processes, such as the study of iconographies' flux captured by a camera, and the system control based on the results [20].One of the major problems to be considered, is working with image sequences is degradation due to presence of noise, which appears while acquiring the images [11,[21][22][23].It is imperative then to remove the noise and process the images in real time [24] by applying a filter, such as Kalman, which can separate random signals from noise [25].
The Kalman filter is an algorithm designed to function as an estimator, that minimizes the estimated error [26][27][28].The filter predicts random signals, separates signals from random noise or detects signals with the presence of noise.The filter uses a sequence of measurements detected over time, and produces more accurate variable estimates [25].Nowadays, the Kalman filter is widely used in navigation and guidance technology [29], vehicle control (air-and spacecraft) [30], signal processing and econometrics [29].Traffic engineering has embraced this method in image processing.
For traffic data, the Kalman filter was first used to predict vehicle volumes that circulated at a certain point and in short term [31,32].Subsequently, vehicle tracking algorithms have been created using the Kalman filter to obtain improved estimates of traffic densities and to facilitate the data acquirement [4,7,10,[33][34][35].There are also new algorithms for vehicle detection that combine the Kalman filter with other methods, such as the hybrid median filter [13], but require a more complex programming language.

Data Acquisition
The processed images were recorded by a fixed camera positioned in two pedestrian bridges (points C1 and C2, Figure 1) located in the middle (C1) and in the northern part (C2) of PDJ, facing north.Along with the video images procurement, manual vehicle counting was completed to validate the automated counting tool.
The camera used was a 9.2 megapixels video HD handycam, installed in a tripod over two different pedestrian bridges.The algorithm was performed in Matlab 2013b, on a laptop with 1 TB Rom, 12 GB Ram and icore 7 processor under Windows 10 OS.The speed of processing images was 4 fps.

Video Analysis for Vehicle Counting
Matlab was the selected software to process the images for its accessibility and for containing the Kalman filter function.The Kalman filter code for Matlab already exists, but it needs modifications to focus it for vehicle counting.To generate the video analysis, several adjustments were applied, which consisted in: (a) configuring the vehicle detection and tracking by adding a corner box with the total count, (b) delimiting geometrically and manually the lanes to analyze, and (c) eliminating the noise that increases the total sum, such as shades and other objects (e.g.pedestrians, sidewalk edges).
For circumstances where cities lack surveillance videos or have budget issues, videos can be obtained placing a fixed camera on a high point, such as a pedestrian bridge or any light or electricity pole.For analysis purpose and a better performance in Matlab, images should be in black and white, and the videos must be converted to .AVI or .MP4 format.

Vehicle Detection in MATLAB
Detection and vehicle tracking within Matlab, using the Kalman filter, is a practical method to evaluate the traffic density at a given point (Figure 2).To make the code work, a flow chart (Figure 3) and the following steps should be followed: 1) To accelerate image processing and to obtain the vehicle total count quicker, it is recommended to cut the video every four images.
2) Matlab's "Built-in" function "ginput()" allows the user to move and click the mouse to get coordinates in 2D, in this case, to delimitate the lane width that defines the counting area.
3) The large shades or the headlights are usually counted as a vehicle and cause error.The shades' RGB range tends to be low (the closest to [0,0,0], the darker the image).Depending of the time the video was taken, the shades' colors are matched with the asphalt color using a cycle, skipping the code block tolerance that added vehicles to the counting.It is necessary to analyze all the frame and to know its size, by assigning a size "X" to a variable "nz" to create the cycle.4) Once the area is delimited and the shades eliminated, the Kalman estimation is performed.The program displays a rectangle that identifies the vehicle movement and size, reporting it as one count.A tolerance range must be established for the result.With a variable flag, the program decides if the movement detected is a vehicle and if it has been already counted.Pixels in the background are not altered by the vehicle detection.5) A corner box can be added to be displaying the vehicle counting.
The variables employed in the Kalman filter algorithm are declared as the following matrixes: R -Noise measurement matrix H -Transform from measure to state matrix Q -System noise matrix P -The status covariance matrix A -State transform matrix X -Current frame.
From this point the Kalman algorithm is applied to reduce the possible noise present in the coordinates enclosing the rectangular space where the detected vehicle is located.This is basically the prediction and correction stages, inherent to the Kalman filter.During the prediction phase, an a priori estimate of a given state at a time k is given, and an error covariance associated with the a priori estimate.In the correction phase, a measurement update needs to be calculated, as well as the Kalman gain, a posteriori estimation, and its error covariance associated with a posteriori estimation.
The initial variable values were first, as an input the actual frame with the background already subtracted, so it could be used in the correction phase and to be able obtain the updated value of the matrix Xk, as well as with the value of the Kalman gain (the closer it is to 0, the reliability of the prediction is greater) an updated value is obtained.And each time the cycle progresses, the pixels containing the input change, and therefore, the prediction variable.

Results
The Matlab code effectiveness was measured in distinct video recordings, at different city points.In Table 1, the Matlab analyses (automated) for one lane are presented along with the manual counting, to measure the efficiency of the suggested code.C1 and C2 are recording videos from two different points, facing the same direction.The best performance was attained while using time periods of 90 seconds, avoiding the program cycling.By using a more powerful processor, analyzed time periods can be increased.The difference in the total count is attributed to the variable speed in the vehicles.The slowest detected movement sometimes is counted as two vehicles.Errors can also be accounted for extreme weather conditions (heavy rain, snow, hail) that may change the asphalt color, thus the shade does not match with it.As observed in Table 1, there is a tendency of counting one or two more vehicles in some video segments, presenting an error of 2 -3%, which can be discarded, unless a more accurate study is needed.If this is the case, the manual count is recommended.The presented program can be improved to add speed detection.

Conclusions
The result of adjusting Kalman filter function within Matlab to create a vehicle counting tool, can be considered as an economical alternative for obtaining traffic data.The algorithm can be adapted to fit the users' necessities and/or to the camera's position.At this stage, it is recommended to process videos with a length smaller than 90 seconds to avoid the failing of the processor.Likewise, it is suggested to analyze one, or maximum three lanes at a time to optimize the program performance, which can also be valuable to obtain the traffic occupancy rate.This code is useful at peak hours, when a manual count is programmed, because only one person is needed to complete it.In this case, Matlab was the selected software to achieve the goals in this paper, but the Kalman filter can be used to process images in any other platform of your choice.

Figure 1 .
Figure 1.Location map of the case study, where C1 and C2 are the two points of video and manual data acquisition [19].

Figure 2 .
Figure 2. Vehicle counting in Matlab.Camera was stationary over a pedestrian bridge (C2) at Periferico de la Juventud.

Figure 3 .
Figure 3. Proposed procedure of applying the Kalman filter to images