Directly paste the source code
Source: http://www.myexception.cn/image/1498389.html
Lab results
Left.jpg ????????????????????????????
Right.jpg
Imagematch.jpg
?
# Include <iostream>
# Include <iomanip>
# Include "opencv2/CORE. HPP"
# Include "opencv2/objdetect. HPP"
# Include "opencv2/features2d/features2d. HPP"
# Include "opencv2/highgui. HPP"
# Include "opencv2/calib3d/calib3d. HPP"
# Include "opencv2/nonfree. HPP"
# Include "opencv2/imgproc/imgproc_c.h"
# Include "opencv2/legacy. HPP"
# Include "opencv2/legacy/compat. HPP"
?
Using namespace CV;
Using namespace STD;
?
Int main ()
{
???? Mat leftimg = imread ("left.jpg ");
???? Mat rightimg = imread ("right.jpg ");
???? If (leftimg. Data = NULL | rightimg. Data = NULL)
???????? Return 0;
?
???? // Convert to grayscale
???? Mat leftgray;
???? Mat rightgray;
???? Cvtcolor (leftimg, leftgray, cv_bgr2gray );
???? Cvtcolor (rightimg, rightgray, cv_bgr2gray );
?
???? // Obtain the common feature points of the two images
???? Int minhessian = 400;
???? Surffeaturedetector detector (minhessian );
???? Vector <keypoint> leftkeypoints, rightkeypoints;
???? Detector. Detect (leftgray, leftkeypoints );
???? Detector. Detect (rightgray, rightkeypoints );
???? Surfdescriptorextractor extractor;
???? Mat leftdescriptor, rightdescriptor;
???? Extractor. Compute (leftgray, leftkeypoints, leftdescriptor );
???? Extractor. Compute (rightgray, rightkeypoints, rightdescriptor );
???? Flannbasedmatcher;
???? Vector <dmatch> matches;
???? Matcher. Match (leftdescriptor, rightdescriptor, matches );????
???? Int matchcount = leftdescriptor. Rows;
???? If (matchcount> 15)
???? {
???????? Matchcount = 15;
???????? // Sort (matches. Begin (), matches. Begin () + leftdescriptor. Rows, distancelessthan );
???????? Sort (matches. Begin (), matches. Begin () + leftdescriptor. Rows );
????}????
???? Vector <point2f> leftpoints;
???? Vector <point2f> rightpoints;
???? For (INT I = 0; I <matchcount; I ++)
???? {
???????? Leftpoints. push_back (leftkeypoints [matches [I]. queryidx]. Pt );
???????? Rightpoints. push_back (rightkeypoints [matches [I]. trainidx]. Pt );
????}
?
???? // Obtain the projection ing from the left image to the right image
???? Mat homo = findhomography (leftpoints, rightpoints );
???? Mat shftmat = (MAT _ <double> (1.0) <1.0, 0, leftimg. cols, 0, 1.0, 0 );
?
???? // Splice the image
???? Mat tiledimg;
???? Warpperspective (leftimg, tiledimg, shftmat * homo, size (leftimg. Cols + rightimg. cols, rightimg. Rows ));
???? Rightimg. copyto (MAT (tiledimg, rect (leftimg. cols, 0, rightimg. cols, rightimg. Rows )));
?
???? // Save the image
???? Imwrite ("tiled.jpg", tiledimg );
????????
???? // Display the spliced Image
???? Imshow ("tiled image", tiledimg );
???? Waitkey (0 );
???? Return 0;
}
Opencv concatenates two images