Having some difficulty in image stitching using OpenCV

前端 未结 2 1850
我寻月下人不归
我寻月下人不归 2021-02-11 09:54

I\'m currently working on Image stitching using OpenCV 2.3.1 on Visual Studio 2010, but I\'m having some trouble.

Problem Description I\'m trying to wri

2条回答
  •  渐次进展
    2021-02-11 10:33

    I have been working lately on image registration. My algorithm takes two images, calculates the SURF features, find correspondences, find homography matrix and then stitch both images together, I did it with the next code:

    void stich(Mat base, Mat target,Mat homography, Mat& panorama){
    
    
    Mat corners1(1, 4,CV_32F);
    Mat corners2(1,4,CV_32F);
    Mat corners(1,4,CV_32F);
    vector planes;
    /* compute corners 
    of warped image
    */
    corners1.at(0,0)=0;
    corners2.at(0,0)=0;
    corners1.at(0,1)=0;
    corners2.at(0,1)=target.rows;
    corners1.at(0,2)=target.cols;
    corners2.at(0,2)=0;
    corners1.at(0,3)=target.cols;
    corners2.at(0,3)=target.rows;
    
    planes.push_back(corners1);
    planes.push_back(corners2);
    
    merge(planes,corners);
    
    perspectiveTransform(corners, corners, homography);
    
    /* compute size of resulting 
    image and allocate memory
    */
    double x_start = min( min( (double)corners.at(0,0)[0], (double)corners.at (0,1)[0]),0.0);
    double x_end   = max( max( (double)corners.at(0,2)[0], (double)corners.at(0,3)[0]), (double)base.cols);
    double y_start = min( min( (double)corners.at(0,0)[1], (double)corners.at(0,2)[1]), 0.0);
    double y_end   = max( max( (double)corners.at(0,1)[1], (double)corners.at(0,3)[1]), (double)base.rows);
    
    /*Creating image
    with same channels, depth
    as target
    and proper size
    */
    panorama.create(Size(x_end - x_start + 1, y_end - y_start + 1), target.depth());
    
    planes.clear();
    
    /*Planes should
    have same n.channels
    as target
    */
    for (int i=0;i(0,0)=1;
    T.at(1,1)=1;
    T.at(2,2)=1;
    T.at(0,2)=-x_start;
    T.at(1,2)=-y_start;
    
    // copy base image to correct position within output image
    
     warpPerspective(base, panorama, T,panorama.size(),INTER_LINEAR| CV_WARP_FILL_OUTLIERS);
     // change homography to take necessary translation into account
    gemm(T, homography,1,T,0,T);
        // warp second image and copy it to output image
    warpPerspective(target,panorama, T, panorama.size(),INTER_LINEAR);
     //tidy
    corners.release();
    T.release();
    
    }
    

    Any question I will try

提交回复
热议问题