# An Improved SIFT Underwater Image Stitching Method
## 3 Materials and Methods
### Underwater Image Stitching
#### 3.2.2 The Proposed Improved SIFT Method
The calculation formula is provided below:  
\[result=\frac{D(q,t_{1})}{D(q,t_{2})}-V_{f}, \tag{11}\]  
where \(V_{f}\) represents a predetermined threshold, typically within the range of 0.4 to 0.8. In this paper, we set \(V_{f}\) to 0.75. For our underwater images, this threshold was thoughtfully selected to maintain a specific quantity of feature matching pairs while ensuring a high level of matching accuracy. If \(result\) is below 0, it signifies that the distance ratio is below the defined threshold, indicating a successful match of feature pairs. Conversely, if \(result\) exceeds 0, it implies that the distance ratio surpasses the specified threshold, and subsequently, the removal of the potential matching pair. To further improve the accuracy of feature alignment and stitching, the RANSAC algorithm is employed. Through multiple iterations, it fits the optimal mathematical model to eliminate matching pairs with significant errors. Firstly, a subset of elements is selected from the set of coarsely matched feature points and designated as the inlier group. Within the inlier group, each element is incorporated into the model to compute the model parameters. The calculation formula of the model is shown in Formula (2). Secondly, the remaining points from the feature point set, excluding those in the inlier group, are inserted into the parameter model obtained in the previous steps. The distance from the model is computed for each element point, and points that fall within the allowable error range are marked as inlier elements, while those outside this range are designated as outliers. The error threshold is set to 4. Utilizing all of the element points contained within the inlier group at this stage, we repeat steps 1 and 2 \(N\) times, where the expression for the number of iterations, \(N\), is as follows:  
\[N=\frac{\lg(1-p)}{\lg(1-\phi^{m})}, \tag{12}\]where \(p\) represents the confidence level, typically in the range of 0.95 to 0.99, \(\phi\) represents the proportion of inlier element points in the dataset, and \(m\) represents the total number of feature points in the dataset. The best-fitting model, containing the most inlier element points during the iterative process, is chosen as the optimal model. The element points within this inlier group at this stage are considered to be the final feature point matches, and the corresponding homography matrix \(\mathbf{H}\) is calculated. Once the homography matrix \(\mathbf{H}\) for image A and image B is obtained, multiply this matrix with image B and conduct a coordinate system correction. The correction aligns two of the four vertices of the training image with the X and Y axes, ensuring that all coordinate points are positive. This enhancement significantly accelerates the computational speed of subsequent stitching. Finally, the superfluous blank areas resulting from the rotation are eliminated to yield the target image (image C). The proposed SIFT-based coarse matching stage is shown in Algorithm 1. The SIFT-based coarse matching stage employs the next input parameters--the reference image (image A) \(reference\)\(Img\) and the training image (image B) \(train\)\(Img\)--and employs the next output parameter: the target image (image C) \(target\)\(Img\); \(kpsReference\) and \(kpsTrain\) represent the key points detected in image A and image B, respectively; \(reference\)\(Descriptors\) and \(train\)\(Descriptors\) represent the feature descriptors for key points detected in image A and image B, respectively; \(matches\) represents the matching feature point pairs obtained using the KNN method; \(\mathbf{H}\) represents the estimated perspective transformation matrix; \(h_{q}\) and \(w_{q}\) represent the height and width of image A, respectively; \(origin\) represents the boundary corners of the original image; \(pts\) represents the coordinates of these corner points; \(dst\) represents the positions of these corner points on the target image after perspective transformation.