# Integrated Image Processing Toolset for Tracking Direction of Metal Grain Deformation
## 3 Integrated Image Processing Toolset
### Fast Normalized Cross-Correlation Algorithm for Grain Matching  
To track the grain location, this image processing toolset integrates a fast normalized cross-correlation (f-NCC) algorithm [51; 52], which is widely used for feature tracking in computer vision. Here, it is used to trace the feature information of the grain location from the stitched 50-scope images. Similar to the proposed image-stitching algorithm, the idea of this algorithm is to cross and match the 200-scope and stitched 50-scope images in the \(x\) and \(y\) axes, creating a correlation map to determine the grain location. An f-NCC algorithm is an ideal approach for a feature detection that emphasizes the matching quality computation efficiency compared to several algorithms commonly used in academia and the industry because of its advantages of a matching accuracy, adaptability, and resistance to the linear transformation of the gray value by preserving the calculated NCC value [51; 52; 53]. The implementation of cross-correlation for template matching was facilitated by the squares' Euclidean distance to interpret the similarity between the image and the feature, which is defined mathematically as follows [51]:  
\[d_{f,t}^{2}(u,v)=\sum_{x,y}[f(x,y)-t(x-u,y-v)]^{2} \tag{5}\]  
where \(f(x,y)\) is the sum of the images in the \(x\) and \(y\) directions under the window containing image feature \(t\) at specific positions \(u\) and \(v\). In the form of the expansion of \(d^{2}\),  
\[d_{f,t}^{2}(u,v)=\sum_{x,y}\Bigl{[}f^{2}(x,y)-2f(x,y)t(x-u,y-v)+t^{2}(x-u,y-v) \Bigr{]} \tag{6}\]the terms \(\sum\limits_{x,y}f^{2}(x,y)\) and \(\sum\limits_{x,y}t^{2}(x-u,y-v)\) are approximately constant; therefore, the similarity score \(corr(u,v)\) between the image and the feature can be computed as follows:  
\[corr(u,v)=\sum\limits_{x,y}f(x,y)-t(x-u,y-v) \tag{7}\]  
However, directly calculating the similarity score using Equation (7) for the image matching led to several problems. In the experiment, it was found that an ambiguous matching score between an exactly matching region and a blank bright area appeared during the computation, which disturbed the final results [54]. In addition, the dependent size of the feature and the invariant issue of the changes in image amplitude caused by lighting conditions [51] must be addressed in the case of implementing cross-correlation for tracing the 200-scope feature location on the stitched 50-scope template image. Therefore, the normalized cross-correlation coefficient was used at each feature point to overcome these problems using the variance of the zero-mean image \(f(x,y)-\overline{f}_{u,v}\) and template \(t(x-u,y-v)-\overline{t}\) functions, which are defined mathematically as follows:  
\[\gamma(u,v)=\frac{\sum_{x,y}\left[f(x,y)-\overline{f}_{u,v}\right]^{1}\cdot \left[t(x-u,y-v)-\overline{t}_{,v}\right]^{1}}{\sqrt{\sum_{x,y}\left[f(x,y)- \overline{f}_{u,v}\right]^{2}\cdot\sum_{x,y}\left[t(x-u,y-v)-\overline{t}_{,v }\right]^{2}}} \tag{8}\]  
where \(\overline{f}_{u,v}\) and \(\overline{t}_{v}\) denote the mean value of the image and the feature template, respectively, within the area of template \(t\) shifted to \((u,v)\). Finally, to address the problem of ambiguous correlation scores and feature invariants, \(\gamma(u,v)\) was evaluated independent of the changes in the brightness and contrast. However, it is widely recognized that the size of the sliding neighborhood windows directly affects the computational complexity of the traditional NCC [54; 55; 56]. The careless selection of small windows might increase the computational efficiency but also degrade the effectiveness of detection. Similarly, selecting a large window size requires a complex computational process. To address this problem, the sum table of the image function constructed by [PERSON] (2003) was recursively used to address these challenges [51].