Stitching 2 images in opencv

11,903

You create a new larger image of the correct combined size, then make ROIs of the size of the existing images in the positions you want them in the final image and copy the existing images to the ROIs.

Share:
11,903
Admin
Author by

Admin

Updated on July 20, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm trying to stitch 2 images just for start for panography. I"ve already found keypoints, found homography using RANSAC but I can't figure out how to align these 2 images (i'm new at opencv). Now part of code

    vector<Point2f> points1, points2;
    for( int i = 0; i < good_matches.size(); i++ )
    {
        //-- Get the keypoints from the good matches
        points1.push_back( keypoints1[ good_matches[i].queryIdx ].pt );
        points2.push_back( keypoints2[ good_matches[i].trainIdx ].pt );
    }
    
    /* Find Homography */
    Mat H = findHomography( Mat(points2), Mat(points1), CV_RANSAC );
    
    /* warp the image */
    warpPerspective(mImg2, warpImage2, H, Size(mImg2.cols*2, mImg2.rows*2), INTER_CUBIC);
    

    and I need to stitch Mat mImg1 where is loaded the first image and Mat warpImage2 where is the warped second image. Can you pls show me how to do that? I also have the warped image cut up and I know I have to change the homography matrix but for now I need to align these two images. Thank you for helping.

    Edit: With Martin Beckett's help I added this code

    //Point a cv::Mat header at it (no allocation is done)
    Mat final(Size(mImg2.cols*2 + mImg1.cols, mImg2.rows*2),CV_8UC3);
    
    //velikost img1
    Mat roi1(final, Rect(0, 0,  mImg1.cols, mImg1.rows));
    Mat roi2(final, Rect(0, 0, warpImage2.cols, warpImage2.rows));
    warpImage2.copyTo(roi2);
    mImg1.copyTo(roi1);
    imshow("final", final);
    

    and it's working now

  • Y.AL
    Y.AL almost 11 years
    I did your steps, as edited by Bodyboard, But I get always the final image as a gray image totally. ! while I'm sure that my resulted image is warped correctly. Any help will be appreciated,
  • Martin Beckett
    Martin Beckett almost 11 years
    @dervish are your source images 3channel? If not you have to convert them before stitching, or make the result the same type as the source and then convert