Ask Your Question
3

How do I stitch images with two different angles?

asked 2016-07-09 04:23:19 -0600

ark gravatar image

image description image description

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

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
6

answered 2016-07-09 16:13:57 -0600

LBerger gravatar image

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 :

image description

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;
}
edit flag offensive delete link more

Comments

I can not stitch with your code!!!The end of the stitch image is so bad!

SerenaChen gravatar imageSerenaChen ( 2016-07-19 06:52:38 -0600 )edit
1

answered 2017-08-29 22:20:50 -0600

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.

image description

edit flag offensive delete link more

Comments

What is that tricky way?

ricoseeds gravatar imagericoseeds ( 2019-09-14 17:14:03 -0600 )edit

Question Tools

3 followers

Stats

Asked: 2016-07-09 04:22:08 -0600

Seen: 5,794 times

Last updated: Aug 29 '17