## 4 Methods
### Mosaic of borehole Video
#### 4.3.2 Proposed PTSAD Algorithm  
The accuracy and operational efficiency are two important factors when choosing the TM algorithm. The method that gives more accurate results is often associated with more computational expense and procedure complexity, whereas the faster method is simpler but leads to an inaccurate result. To ensure the accuracy of the mosaic for each video frame without losing operational efficiency and while meeting the real-time application requirement of video image processing, we selected an appropriate template and a search area, and proposed the PTSAD method to measure the similarity between the template image and target image. * Template selection  
The best template in the source image includes as many distinctive features as possible. However, selecting a larger template requires more computations for the image matching. On the other hand, a smaller template requires less computation, but few distinctive features will reduce the matching performance. In this paper, because the resolution of the front view image decreased gradually in the radial direction, the resolution of the borehole-wall unfolded image was also reduced from top to bottom. Accordingly, the template was selected at the center of the image, as shown by the red box in Figure 9.  
* Search area selection  
There are two ways to reduce the running time of the template matching algorithm: One is to decrease the cost of computing in each matching point, and the other is to narrow the search area. In the traditional matching method, such as full-search template matching (FSTM) [32], a large number of search areas are invalid, causing a heavy computational cost. In order to reduce the unnecessary computational time of the template matching in whole video sequences, an effective measure reducing the search area is also very significant. Considering that the displacement between two adjacent video frames was relatively small, the search area could be controlled within a proper range around the template, as shown by the green box in Figure 9, where the computational cost could be reduced dramatically by minimizing the search range. The search area selection in this paper eliminated a great number of unnecessary points, and the operation speed was tripled, at least. * Similarity measure method of PTSAD  
As the similarity measure method, the NCC (normalized cross correlation) and SAD (sum of absolute difference) algorithms are the most popular and widely applied to the TM [22]. NCC is more robust against illumination changes than SAD, nevertheless, NCC is more time-consuming than SAD. The SAD algorithm is typically used for the template matching of the video image due to its simplicity and higher operating speed. In this paper, to improve the accuracy of the SAD algorithm, a gray projection transformation (GPT) was introduced into the TM method, which provided a new PTSAD algorithm. The architecture of the PTSAD algorithm is shown in Figure 10.  
Figure 9: The selection of the template and search area. SAD measures the degree of similarity between a template and the examined image. It is a method that sums the total differences of the gray level of each pixel between the template and the examined image. Suppose \(T\) is the template of size \(m\times n\) and \(E\) is the examined image of size \(u\times v\), where \(u>m\) and \(v>n\). SAD at point \((x,y)\) in the examined image is defined as follows [33; 34]:  
\[SAD(x,y)=\sum_{i=0}^{m-1}\sum_{j=0}^{n-1}\bigl{|}E(x+i,y+i)-T(i,j)\bigr{|}, \tag{2}\]  
where, \(E\) (\(i,j\)) and \(T\) (\(i,j\)) are the pixel values at coordinate (\(i,j\)) in the examined image and template respectively. \((x,y)\) represents a pixel position of the current search point. The smaller the value of SAD, the more similar to the template, and the candidate image with the minimum SAD value is usually regarded as the best matching image. The GPT reduces a two-dimensional image to a one-dimensional gray scale projection curve.