How do I stitch images with two different angles?
I have two images as shown above taken from two cameras put on the same pole. How do I stitch these two images together?
Thanks. ARK
I have two images as shown above taken from two cameras put on the same pole. How do I stitch these two images together?
Thanks. ARK
You must use a mask in detectAndCompute parameters. Mask is a rectangle at left part of first image and a rectangle at right in second image. Hence you will have only descriptors in common part of both image. You can match descriptors and findhomography and warp image...
results is :
code is here
#include "opencv2/opencv.hpp"
#include <iostream>
#include <fstream>
#include <ctype.h>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
Mat img1 = imread("14680560057580021.jpg");
Mat img2 = imread("14680560155728094.jpg");
namedWindow("I2", WINDOW_NORMAL); namedWindow("I1", WINDOW_NORMAL);
imshow("I1",img1);
imshow("I2",img2);
Ptr<ORB> o1 = ORB::create();
Ptr<ORB> o2 = ORB::create();
vector<KeyPoint> pts1,pts2;
Mat desc1,desc2;
vector<DMatch> matches;
Rect r1(1720,40,200,1040);
Rect r2(0,0,150,1080);
Mat mask1 = Mat::zeros(img1.size(),CV_8UC1);
Mat mask2 = Mat::zeros(img1.size(),CV_8UC1);
mask1(r1)=1;
mask2(r2)=1;
o1->detectAndCompute(img1,mask1,pts1,desc1);
o2->detectAndCompute(img2,mask2,pts2,desc2);
BFMatcher descriptorMatcher(NORM_HAMMING,true);
descriptorMatcher.match(desc1, desc2, matches, Mat());
// Keep best matches only to have a nice drawing.
// We sort distance between descriptor matches
Mat index;
int nbMatch=int(matches.size());
Mat tab(nbMatch, 1, CV_32F);
for (int i = 0; i<nbMatch/2; i++)
{
tab.at<float>(i, 0) = matches[i].distance;
}
sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING);
vector<DMatch> bestMatches;
vector<Point2f> src,dst;
for (int i = 0; i < nbMatch/2; i++)
{
int j = index.at<int>(i,0);
cout << pts1[matches[j].queryIdx].pt<<"\t"<<pts2[matches[j].trainIdx].pt<<"\n";
src.push_back(pts1[matches[j].queryIdx].pt+Point2f(0,img1.rows)); // necessary offset
dst.push_back(pts2[matches[j].trainIdx].pt);
}
cout << "\n";
Mat h=findHomography(src,dst,RANSAC);
Mat result;
cout<<h<<endl;
warpPerspective(img2, result, h.inv(), Size(3*img2.cols +img1.cols , 2*img2.rows+img1.rows));
Mat roi1(result, Rect(0, img1.rows, img1.cols, img1.rows));
img1.copyTo(roi1);
namedWindow("I3", WINDOW_NORMAL);
imshow("I3",result);
imwrite("result.jpg",result);
waitKey();
return 0;
}
I can not stitch with your code!!!The end of the stitch image is so bad!
i tried to stitch images by stitching.cpp but it failed to stitch them.
then i tried a tricky way (inspired by @LBerger 's solution) and get this image. it is not perfect but, it is better than nothing.
i made a Pull Request to change stitching.cpp.
Asked: 2016-07-09 04:22:08 -0600
Seen: 5,719 times
Last updated: Aug 29 '17
Image stitching from a live video stream
Image Stitching using graph cut
Problem when stitching images using stitcher module
Panorama stitching using opencv crashes for more than 2 images
How can I add my own feature code into sticther pipeline of OpenCV?
stitcher produces empty panorama [closed]
Utilise known extrinsic parameters when stitching panoramas
Solving native open cv code error in androd.mk for developing panorama on android