# A Real-Time Map Restoration Algorithm Based on ORB-SLAM3
## 2 Materials and Methods
### Loop Closure Detection Acceleration Based on Grayscale Histogram  
The grayscale histogram of image frame has a small computational cost, but it can well reflect the pixel distribution characteristics of an image. In addition, the grayscale histogram has the advantages of translation, rotation and scaling invariance, which is very suitable for frame measurement in the SLAM system. In this paper, we use the grayscale histogram to accelerate the loop closure detection of each frame, so that the real-time performance of the map restoration algorithm is improved. In the ORB-SLAM3 system, the local mapping thread will continuously insert keyframes into the keyframe queue of the loop closing thread, and then find out the similar keyframes in the database for the similarity judgment. If the similarity is high, this keyframe will be used as a candidate keyframe. When inserting a keyframe into the keyframe queue of the loop closing detection thread, we calculate the grayscale histogram of the keyframe. In the working process of the SLAM system, there is almost no case that the two frames are exactly the same. Therefore, the grayscale image \(I_{gray}\) of the keyframe is adaptively clipped so that the grayscale histogram of the image can retain the main information of the image, and at the same time, the uncertainty caused by the perspective difference is reduced,  
\[I^{\prime}_{gray}=Rect(I_{gray},\theta_{1},\lambda_{1},\theta_{2},\lambda_{ 2}), \tag{7}\]  
where \(Rect\) denotes that the grayscale image \(I_{gray}\) is cut from coordinates (\(\theta_{1}\),\(\lambda_{1}\)) to coordinates (\(\theta_{2}\),\(\lambda_{2}\)); \(\theta\) represents the abscissa of the image coordinate system, \(\lambda\) represents the ordinate. Then, the grayscale histogram of the cut \(I^{\prime}_{gray}\) is calculated to obtain \(Hist\). A total of 256 grayscale levels are taken from 0 to 255, and the number of pixels corresponding to each grayscale level is calculated, which can represent the frequency of the grayscale level,  
\[ \begin{split} H(P)&=[h(x_{0}),h(x_{1}),\ldots,h(x_{ 255})]\\ h(x_{i})&=\frac{S(x_{i})}{\sum_{j}S(x_{j})}\end{split} , \tag{8}\]  
where \(S(x_{i})\) is the number of pixels in a grayscale level, \(\sum_{j}S(x_{j})\) is the total number of pixels. After calculating the grayscale histogram, in order to maintain the relative relationship between the two sets of data and make the data more comparable, the range of \(Hist\) is normalized by Equation (9),  
\[x_{out}=\frac{\beta-\alpha}{\delta-\gamma}(x_{in}-\gamma)+\alpha, \tag{9}\]  
where \(x_{in}\) and \(x_{out}\) are the original values and the normalized values of each grayscale level, respectively. The range of values is mapped from (\(\gamma\),\(\delta\)) to (\(\alpha\),\(\beta\)). For example, Figure 3 shows the process of normalizing histogram (b) of the grayscale image (a) to obtain (c). Then, the normalized grayscale histogram is used to calculate the mean and standard deviation according to Equation (10) as a rough image feature measure. \[ \begin{split}\mu&=\frac{1}{M\times N}\sum_{i=1}^{M} \sum_{j=1}^{N}P(i,j)\\ SD&=\sqrt{\frac{1}{M\times N}\sum_{i=1}^{M}\sum_{j=1 }^{N}(P(i,j)-\mu)^{2}}\end{split}  \tag{10}\]  
where \(M\times N\) is the size of the histogram, \(P(i,j)\) represents the value of line \(i\) and column \(j\) of the histogram. After obtaining the histogram information of the keyframe, the keyframe is inserted into the loop closure detection queue, and \((\mu,SD)\) is used as the coordinate index to add the keyframe to the container that is connected by a mean and standard deviation. This operation makes the keyframes with a similar mean and standard deviation always stored in a certain interval of the container. Filter candidate keyframes first when loop closure detection.