Opencv concatenates two images

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.