# An Improved SIFT Underwater Image Stitching Method
## 3 Materials and Methods
### Underwater Image Stitching
#### 3.2.2 The Proposed Improved SIFT Method
```
Input:\(reference\)\(Img\), \(train\)\(Img\) Output:\(target\)\(Img\)\(kpsReference\), \(reference\)Descriptors \(kpsTrain\), \(train\)Descriptors \(matches\leftarrow\) matchKeyPointsKNN \(\mathbf{H}\leftarrow\) getHomography \(h_{q}\),\(w_{q}\leftarrow\) shape (\(reference\)\(Img\)) \(origin\leftarrow\) array\(\big{(}[0,0]\), \([0,h_{q}\)\(-1]\), \([w_{q}\)\(-1\), \(h_{q}\)\(-1\)], \([w_{q}\)\(-1\), \(0]\big{)}\) \(pts\leftarrow\) origin. reshape (-1,2) \(dst\leftarrow\) perspectiveTransform (\(pts\), \(\mathbf{H}\)). squeeze () ifmin (\(dst\)) < 0 then ifmin (\(dst\)\([\)\(:,0]\)) \(<\) min (\(dst\)\([\)\(:,1]\)) then \(dst\)\(\leftarrow\)\(dst\)\(-\) min (\(dst\)\([\)\(:,0]\)) \(dst\)[\(:,1\)] \(\leftarrow\)\(dst\)\([\)\(:,1]\) - min (\(dst\)\([\)\(:,1]\)) else \(dst\)\(\leftarrow\)\(dst\)\(-\) min (\(dst\)\([\)\(:,1]\)) \(dst\)\([\)\(:,0]\) \(\leftarrow\)\(dst\)\([:,0]\) - min (\(dst\)\([:,0]\)) endif else \(dst\)[\(:,0]\) \(\leftarrow\)\(dst\)\([:,0]\) - min (\(dst\)\([:,0]\)) \(dst\)[\(:,1]\) \(\leftarrow\)\(dst\)\([:,1]\) - min (\(dst\)\([:,1]\)) endif \(target\)\(Img\)\(\leftarrow\) warpPerspective
```  
**Algorithm 1** SIFT-based coarse matching stage. In the second phase, precise feature extraction is carried out using the SIFT algorithm on both the target image (image C) and the reference image (image A). The SIFT algorithm is independently applied to extract features within the overlapping regions of these two images. During feature extraction, a bidirectional k-nearest neighbors (KNN) matching algorithm is utilized for feature point matching. This approach is particularly effective in capturing more precise feature points, especially in flat areas of the image. To elucidate the process, an initial key point is selected from image 1, the two closest feature points in image 2 to this key point in image 1 are identified, and their positions are recorded. If the distance ratio between the nearest feature point and the second-nearest feature point falls below the matching threshold of 0.75, the nearest feature point is retained. A similar procedure is then applied to determine the two closest feature points in image 1 for each feature point in image 2 among the nearest feature points in image 2. To create a realistic large-scale underwater scene image, this paper introduces an image-pasting fusion stitching method. Firstly, the feature matching pairs are ranked in descending order according to their matching scores. Then, the logarithmic values of the matching feature points derived from the final filter are compared against a predefined threshold. If the values are below this threshold, the top 10% of the feature matching pairs with the highest scores are selected to determine the relative position of the two underwater images, denoted as \(x_{sub}\) and \(y_{sub}\). The equations of the relative position correction symbols for the two images are as follows:  
\[x_{sub}=\frac{\sum\limits_{i=1}^{n}x_{B(i)}-x_{A(i)}}{n} \tag{13}\]  
\[y_{sub}=\frac{\sum\limits_{i=1}^{n}y_{B(i)}-y_{A(i)}}{n}, \tag{14}\]  
where \(x_{sub}\) is the relative position correction symbol in the horizontal direction, \(y_{sub}\) is the relative position correction symbol in the vertical direction, \((x_{A},y_{A})\) and \((x_{B},y_{B})\) are the coordinates of matching feature points for the two images, respectively, and \(n\) represents the number of feature matching points between two images. If the number of feature pairs resulting from the final matching process exceeds the predefined threshold, Formulas (13) and (14) are employed to determine the relative positions of the features in all matched pairs. The threshold value is chosen from the range of 50 to 100, depending on the image size. Through experiments conducted on the images presented in this article, it was observed that setting the threshold below 50 leads to a significant increase in matching errors. Conversely, if the threshold exceeds 100, it results in reduced computational efficiency. Therefore, in this study, the threshold was set to 60.