# A Multi-Loop Vehicle-Counting Method under Gray Mode and RGB Mode
## 3 Methods
### Vehicle Detection under Gray and RGB Modes
#### 3.1.1 Preliminary Detection under Gray Mode  
The traditional gray background subtraction was applied for preliminary vehicle detection. Under gray mode, there are two main approaches to obtain a gray-mode background: Gaussian mixture model (GMM) [25] and statistical median model (SMM) [26]. Figure 1: Block diagram of the proposed method. GMM has relatively higher extraction accuracy than SMM, but its algorithm complexity is higher and takes 40 to 50 times longer than SMM. Moreover, GMM contains not only the parameter of total number of images in the image library for extracting the gray-mode background (\(N\)) but also one more parameter: the learning constant (\(\alpha\)), which is an empirical value and is not easily determined [27]. In contrast, SMM contains only one parameter (\(N\)). Taking the above factors into consideration, SMM was selected for extracting the gray-mode background in this study. An image library under gray mode for extracting the gray-mode background should be built first. In this image library, every image is labeled, which could be expressed as \(I_{n_{0}}^{gray}\), where \(n_{0}=1,2,\ldots,N\), and \(N\) is the total number of images in the image library. The establishment of background is calculated at the pixel level. The basic idea of establishing the background is to find the gray value in the median position at each pixel in the image library. Different from the previous methods, we not only need to obtain the gray value in the median position at each pixel but also need to record the label of the image that provides the median-position gray value. At each pixel \((i,j)\), the images in the image library is sorted according to the gray-mode value first:  
\[I_{1_{(i,j)}}^{gray}(i,j)\leq\cdots\leq I_{n_{(i,j)}}^{gray}(i,j)\leq\cdots \leq I_{N_{(i,j)}}^{gray}(i,j) \tag{1}\]  
where \(n_{(i,j)}\) is the sorted label and \(n_{(i,j)}=1,2,\ldots,N\); \(I_{n_{(i,j)}}^{gray}(i,j)\) is the gray-mode value at each pixel \((i,j)\) of image \(n_{(i,j)}\). In this case, the label of the image that provides the median-position gray value is recorded as follows:  
\[n_{(i,j)}^{mid}=[\tfrac{N}{2}]_{(i,j)} \tag{2}\]  
where \([x]\) is the integral function. At each pixel \((i,j)\), the background value equals to the median-position gray value:  
\[B^{gray}(i,j)=I_{n_{(i,j)}^{mid}}^{gray}(i,j) \tag{3}\]  
where \(B^{gray}\) is the gray-mode background. The result of vehicle detection is in the form of binary image, that is, white represents the vehicle and black represents the background. By subtracting the background image from the current frame and by binarizing the subtracting result, vehicle segmentation could be realized. The result of subtracting the background from current frame under gray mode could be expressed as follows:  
\[\delta_{k}^{gray}=|I_{k}^{gray}-B^{gray}| \tag{4}\]  
where \(I_{k}^{gray}\) is the gray-mode image of the current frame \(k\). The [PERSON]'s method [28] was used to binarize the subtracting result in this study and the result of vehicle detection under gray mode could be expressed as \(I_{k}^{binary_{gray}}\). To illustrate the proposed detection method better, we selected a benchmark dataset for example. The video is called M-30 and could be obtained from the Road-Traffic Monitoring (GRAM-RTM) dataset [29], and a frame (\(k=539\)) from M-30 was selected as the current image for further illustration. The current image under RGB mode (\(I_{539}^{RGB}\)) and under gray mode (\(I_{539}^{gray}\)) are shown in Figure 2a,b. Under the proposed detection method, the established gray-mode background (\(B^{gray}\)) is shown in Figure 3a, and the detection under gray mode (\(I_{539}^{binary_{gray}}\)) is shown in Figure 3b. As shown in Figure 3b, the detection under gray mode was incomplete, especially for the red vehicle on the right. The reason for this phenomenon is that the background subtraction values at the position of the red vehicle is too small under gray mode.