简体   繁体   中英

Unable to stitch images via OpenCV in C++

I need to stitch few images using OpenCV in C++, so I wrote the following code:

#include <opencv2/opencv.hpp>
#include <opencv2/stitching.hpp>

#include <cstdio>
#include <vector>

void main()
{
  std::vector<cv::Mat> vImg;
  cv::Mat rImg;

  vImg.push_back(cv::imread("./stitching_img/S1.png"));
  vImg.push_back(cv::imread("./stitching_img/S2.png"));
  vImg.push_back(cv::imread("./stitching_img/S3.png"));

  cv::Stitcher stitcher = cv::Stitcher::createDefault();

  unsigned long AAtime = 0, BBtime = 0;
  AAtime = cv::getTickCount();

  cv::Stitcher::Status status = stitcher.stitch(vImg, rImg);

  BBtime = cv::getTickCount();
  printf("%.2lf sec \n", (BBtime - AAtime) / cv::getTickFrequency());

  if (cv::Stitcher::OK == status)
    cv::imshow("Stitching Result", rImg);
  else
    std::printf("Stitching fail.");

  cv::waitKey(0);
}

Unfortunately, it always says "Stitching fail" on the following files -- http://imgur.com/a/32ZNS while it works on these files -- http://imgur.com/a/ve5sY

What am I doing wrong? How can I fix it?

Thanks in advance.

cv::Stitchers works by finding common features in the separate images and use those to figure out where the images fit together. In your samples where the stitching works you can find a lot of overlap: the blue roof, the features of the buildings across the road, etc.

In the set where it fails for you, there is no overlap, so the algorithm can't figure out how to fit them together. It seems like you can 'stitch' these images by just putting them next together. For this you can use hconcat as described at this answer: https://stackoverflow.com/a/20079134/1737727

There is a very simple way of displaying two images side by side. The following function can be used which is provided by opencv.

Mat image1, image2;
hconcat(image1,image2,image1);//Syntax->
hconcat(source1,source2,destination);

This function can also be used to copy a set of columns from an image to another image.

Mat image;
Mat columns=image.colRange(20,30);
hconcat(image,columns,image);

vconcat is a similar function to stich images vertically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM