How to merge two images in opencv?

前端 未结 4 1437
时光取名叫无心
时光取名叫无心 2020-12-08 07:41

I have calculated homography ,taken out perspective transform .I am able two display two images in one window but unable to merge them.Here are my example images->

4条回答
  •  时光取名叫无心
    2020-12-08 08:22

    If you do more carefully, such as do not crop and stack, but use alphaBlend, then you will find something strange.

    This is the matched image:

    This is the wrapped img2:

    This is the mask to do alphablend:

    This is the alphaBlended:


    We can easily find the ghost in the blended image. This is because the warpPerspective and perspectiveTransform can't really find the real camera projection equation. Mostly because the image panel is a panel, but a cylindrical surface or spherical surface or more complex. So the job we have done is not enough.

    While the good new is OpenCV provides High level stiching API, We can do stitching easily by OpenCV Stitching API. Here is the result.

    Code:

    //! 2018.01.16 14:43:26 CST
    //! 2018.01.16 17:32:15 CST
    #include 
    #include 
    #include "opencv2/imgcodecs.hpp"
    #include "opencv2/highgui.hpp"
    #include "opencv2/stitching.hpp"
    using namespace std;
    using namespace cv;
    
    Stitcher::Status stitchImages(vector&imgs,string fname, Stitcher::Mode mode = Stitcher::PANORAMA, bool try_gpu=false) {
        Ptr stitcher = Stitcher::create(mode, try_gpu);
        Mat pano;
        Stitcher::Status status = stitcher->stitch(imgs, pano);
        if (status != Stitcher::OK){
            cout << "Can't stitch images, error code = " << int(status) << endl;
            return -1;
        }
        imwrite(fname, pano);
        return status;
    }
    
    int main(int argc, char* argv[])
    {
    
        vectorfnames = {
            "img1.jpg", "img2.jpg"
        };
    
        vector imgs;
        for(auto fname: fnames) {
            imgs.emplace_back(imread(fname));
        }
        stitchImages(imgs, "scans.png",Stitcher::SCANS );
        stitchImages(imgs, "pano.png",Stitcher::PANORAMA );
    }
    

    Some useful links:

    1. OpenCV Stitching: https://docs.opencv.org/3.3.0/d8/d19/tutorial_stitcher.html

    2. Alpha Blending in OpenCV C++ : Combining 2 images with transparent mask in opencv

    3. Alpha Blending in OpenCV Python: Gradient mask blending in opencv python

提交回复
热议问题