# A Multi-Loop Vehicle-Counting Method under Gray Mode and RGB Mode
## 3 Methods
### Vehicle Counting Based on Multiple Loops
#### 3.2.2 Judgement of Loop States  
The first step to counting the vehicles is to judge the state of each loop, and the designed loops are set on the result of binary detection \(l_{k}^{binary}\). As shown in Figure 9, when a pixel position is in the foreground, the pixel value at that position is 1; otherwise, it is 0. Therefore, the judgement of loop states can be realized by analyzing the change of pixel value in the position of each loop. The binary detection \(l_{539}^{binary}\) set with loops is shown in Figure 13.  
For each \(PLoop^{i}\) and \(SLoop^{i}\) at frame \(k\), we set up a state detector to record the state of \(PLoop^{i}\) and \(SLoop^{i}\), which could be expressed as \(PState_{k}^{i}\) and \(SSState_{k}^{i}\), respectively, where \(k\) is the frame number of current image. The study on the state of primary loop and secondary loop is similar. For the convenience of description, in the subsequent paragraphs of this subsection, we collectively refer to them as \(Loop^{i}\) and \(State_{k}^{i}\).  
The state of \(Loop^{i}\) was divided into active state and inactive state, representing the state with and without vehicles in \(Loop^{i}\), and the corresponding values are 1 and 0, respectively. As mentioned above, the pixel value at each loop position is the key to determining the state of the loop and we designed three indicators to make the judgement.  
The first two are the ratio of the number of pixels with a value of 1 to the total number of pixels contained in the upper line and the lower line, which could be expressed as \(upper_{k}^{i}\) and \(lower_{k}^{i}\). When a vehicle enters or leaves one loop, the values of these two indicators change. However, it is not enough to only take the upper line and the lower line of \(Loop^{i}\) as the discriminant basis because some small noises may also cause \(upper_{k}^{i}\) or \(lower_{k}^{i}\) to be large. Therefore, the situation of pixel values on the whole region of loop should also be used as the discriminant basis, which could be expressed as \(region_{k}^{i}\).  
When there are no vehicles or noise in \(Loop^{i}\), \(upper_{k}^{i}\), \(lower_{k}^{i}\), and \(region_{k}^{i}\) are all zero. When there is noise in \(Loop^{i}\) but no vehicle, \(region_{k}^{i}\), \(upper_{k}^{j}\), or \(lower_{k}^{i}\) are small. However, \(region_{k}^{i}\), \(upper_{k}^{i}\) or \(lower_{k}^{i}\) are large when there is a vehicle in \(Loop^{i}\). In this case, the state of \(Loop^{i}\) in frame \(k\) could be judged based on the three indicators, which could be expressed as follows:  
\[State_{k}^{i}=\left\{\begin{array}{ll}1,&if~{}~{}(upper_{k}^{i}>T^{upper})~{ }\vee~{}(lower_{k}^{i}>T^{lower})~{}\wedge~{}(region_{k}^{i}>T^{region})\\ 0,&others\end{array}\right. \tag{12}\]  
where \(T^{region}\), \(T^{upper}\) and \(T^{lower}\) are the thresholds for the whole region, the upper line, and the lower line. Additionally, it is worth noting that the activation threshold of the secondary loop is slightly higher than that of the primary loop.  
Figure 13: Binary detection \(l_{539}^{binary}\) set with (**a**) primary loops and (**b**) secondary loops.  
As shown in Figure 13, the state of \(PLoop^{1}\), \(SLoop^{1}\) and \(SLoop^{2}\) are active while the state of other primary loops and secondary loops are inactive. The mathematical form of the states in frame 539 is expressed as Table 1.