# A Deep Similarity Metric Method Based on Incomplete Data for Traffic Anomaly Detection in IoT
## 3 Methodology
### Anomaly Detection Based on Background Modeling  
For anomaly detection, locating the object is one of the most critical parts. In this paper, the image difference between background masks based on background modeling is adopted to locate the abnormal objects. In surveillance videos, the traditional backgrounds usually include road surface, roadside cornerstone, and green belts. All kinds of image blocks have the characteristics of pixel similarity and continuity. According to the situation above, the combination of spatial-temporal information for background modeling is adopted. The conventional background modeling method such as the Gaussian Mixture Model (GMM) depends on statistics of a single pixel, without considering the spatial distribution of pixels in the background. In view of this problem, we propose the spatial-temporal information modeling algorithm for the improved K-Means pixel clustering to process the pixels in videos. The image is converted to greyscale and processed by average down-sampling. Then \(n\) random pixels are considered as the initial clustering center. K-Means clustering on all pixels is performed through Euclidean distance. The efficiency of the category center is determined by the Calinski-Harabasz criterion:  
\[CH(k)=\frac{trB(k)/(k-1)}{trW(k)/(n-1)} \tag{9}\]  
where \(n\) is the number of clusters, \(k\) is the current class, \(trB(k)\) is the trace of the inter-class covariance matrix, and \(trW(k)\) is the trace of the intra-class covariance matrix. The larger \(CH\) indicates that the inter-class distance is closer while the intra-class distance is farther, leading to a better clustering result. The pixels closest to the center of the category in each class are recorded. Then the center points of \(n\) transferred categories are found in the original image, and their RGB pixel values are recorded as the category center in the subsequent clustering process. Afterwards, \(n\) transferred category pixel values are used to classify each block in the original image and the location distribution of each category on the modeled image is obtained. In the proposed method, the temporal background information is modeled by the K-nearest neighbor method. The nearest K instances in the training data-set are found to classify a new input instance. An adaptive background modeling method is performed to get a real-time model and a time-delayed model. Specifically, the KNN background modeling is to find a hypersphere with volume \(V\) and diameter \(D\) centered on a sample point in the RGB space, which satisfies the requirement that there are more than \(K\) (generally \(0.1*T\)) of the \(T\) samples inside the sphere. The Formula (10) is used to calculate whether the sample points \(\overrightarrow{x}\) are the background points. That is, each element in the sample set \(X_{T}\) should be compared to determine whether it belongs to the background. The comparison between the two-pixel points is to calculate the Euclidean distance between them, and the initialization probability is as follows:  
\[P_{init}\left(\overrightarrow{x}|X_{T},BG_{Init}\right)=\frac{1}{TV}\sum_{ m=t-T}^{t}K\left(\frac{\left\|\overrightarrow{x}^{(m)}-\overrightarrow{x} \right\|}{D}\right)=\frac{k}{TV} \tag{10}\]  
where \(T\) is the number of historical frames, and \(t\) is the number of current video frames, \(K\) is a Gaussian kernel function. The point is considered as the background when its probability is greater than the probability of the background. Each pixel is classified into different categories according to its distance to each class center acquired from spatial information. Whether each category is in the background depends on the initialization of the spatial information and the spatial distribution. Then, we can discriminate whether the pixel is the background point by the class and location of the pixels.