Having some difficulty in image stitching using OpenCV

前端 未结 2 1839
我寻月下人不归
我寻月下人不归 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

    It sounds like you are going about this sensibly, but if you have access to both of the cameras, and they will remain stationary with respect to each other, then calibrating offline, and simply applying the transformation online will make your application more efficient.

    One point to note is, you say you are using the findHomography function from OpenCV. From the documentation, this function:

    Finds a perspective transformation between two planes.
    

    However, your points are not restricted to a specific plane as they are imaging a 3D scene. If you wanted to calibrate offline, you could image a chessboard with both cameras, and the detected corners could be used in this function.

    Alternatively, you may like to investigate the Fundamental matrix, which can be calculated with a similar function. This matrix describes the relative position of the cameras, but some work (and a good textbook) will be required to extract them.

    If you can find it, I would strongly recommend having a look at Part II: "Two-View Geometry" in the book "Multiple View Geometry in computer vision", by Richard Hartley and Andrew Zisserman, which goes through the process in detail.

    0 讨论(0)
  • 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<Mat> planes;
    /* compute corners 
    of warped image
    */
    corners1.at<float>(0,0)=0;
    corners2.at<float>(0,0)=0;
    corners1.at<float>(0,1)=0;
    corners2.at<float>(0,1)=target.rows;
    corners1.at<float>(0,2)=target.cols;
    corners2.at<float>(0,2)=0;
    corners1.at<float>(0,3)=target.cols;
    corners2.at<float>(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<Vec2f>(0,0)[0], (double)corners.at<Vec2f> (0,1)[0]),0.0);
    double x_end   = max( max( (double)corners.at<Vec2f>(0,2)[0], (double)corners.at<Vec2f>(0,3)[0]), (double)base.cols);
    double y_start = min( min( (double)corners.at<Vec2f>(0,0)[1], (double)corners.at<Vec2f>(0,2)[1]), 0.0);
    double y_end   = max( max( (double)corners.at<Vec2f>(0,1)[1], (double)corners.at<Vec2f>(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<target.channels();i++){
    planes.push_back(panorama);
    }
    
    merge(planes,panorama);
        // create translation matrix in order to copy both images to correct places
    Mat T;
    T=Mat::zeros(3,3,CV_64F);
    T.at<double>(0,0)=1;
    T.at<double>(1,1)=1;
    T.at<double>(2,2)=1;
    T.at<double>(0,2)=-x_start;
    T.at<double>(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

    0 讨论(0)
提交回复
热议问题