【问题标题】:OpenCV - Image StitchingOpenCV - 图像拼接
【发布时间】:2011-08-26 10:51:57
【问题描述】:

我正在使用以下代码拼接输入图像。对于一个未知数 原因输出结果是废话! 看来单应矩阵是错误的(或被错误地影响了) 因为转换后的图像就像一颗“被剥削的星星”! 我已经评论了我猜是问题根源的部分 但我无法意识到。 任何帮助或观点都是值得的!

祝你有美好的一天, 阿里

void Stitch2Image(IplImage *mImage1, IplImage *mImage2) 
{ 

    // Convert input images to gray 
    IplImage* gray1 = cvCreateImage(cvSize(mImage1->width, mImage1->height), 8, 1); 

    cvCvtColor(mImage1, gray1, CV_BGR2GRAY); 
    IplImage* gray2 = cvCreateImage(cvSize(mImage2->width, mImage2->height), 8, 1); 

    cvCvtColor(mImage2, gray2, CV_BGR2GRAY); 
    // Convert gray images to Mat 
    Mat img1(gray1); 
    Mat img2(gray2); 
    // Detect FAST keypoints and BRIEF features in the first image 
    FastFeatureDetector detector(50); 
    BriefDescriptorExtractor descriptorExtractor; 
    BruteForceMatcher<L1<uchar> > descriptorMatcher; 
    vector<KeyPoint> keypoints1; 
    detector.detect( img1, keypoints1 ); 
    Mat descriptors1; 
    descriptorExtractor.compute( img1, keypoints1, descriptors1 );

/* Detect FAST keypoints and BRIEF features in the second image*/


    vector<KeyPoint> keypoints2; 
    detector.detect( img1, keypoints2 ); 
    Mat descriptors2; 
    descriptorExtractor.compute( img2, keypoints2, descriptors2 ); 
    vector<DMatch> matches; 
    descriptorMatcher.match(descriptors1, descriptors2, matches); 
    if (matches.size()==0) 
            return; 
    vector<Point2f> points1, points2; 
    for(size_t q = 0; q < matches.size(); q++) 
    { 
            points1.push_back(keypoints1[matches[q].queryIdx].pt); 
            points2.push_back(keypoints2[matches[q].trainIdx].pt); 
    } 
    // Create the result image 
    result = cvCreateImage(cvSize(mImage2->width * 2, mImage2->height), 8, 3); 
    cvZero(result); 

   // Copy the second image in the result image 

    cvSetImageROI(result, cvRect(mImage2->width, 0, mImage2->width, mImage2->height)); 
    cvCopy(mImage2, result); 
    cvResetImageROI(result); 

  // Create warp image 
    IplImage* warpImage = cvCloneImage(result); 
    cvZero(warpImage); 

  /************************** Is there anything wrong here!? *******************/ 
   // Find homography matrix 
    Mat H = findHomography(Mat(points1), Mat(points2), 8, 3.0); 
    CvMat HH = H; // Is this line converted correctly? 
   // Transform warp image 
    cvWarpPerspective(mImage1, warpImage, &HH); 
  // Blend 
    blend(result, warpImage);
  /*******************************************************************************/ 

    cvReleaseImage(&gray1); 
    cvReleaseImage(&gray2); 
    cvReleaseImage(&warpImage); 
}

【问题讨论】:

    标签: image-processing opencv image-stitching


    【解决方案1】:

    这是我建议您按以下顺序尝试的方法:

    1) 使用 CV_RANSAC 选项进行单应性。参考http://opencv.willowgarage.com/documentation/cpp/calib3d_camera_calibration_and_3d_reconstruction.html

    2) 尝试其他描述符,尤其是 OpenCV 附带的 SIFT 或 SURF。对于某些图像,FAST 或 Brief 描述符的区分度不够。编辑(2012 年 8 月):基于 Brief 的 ORB 描述符非常好和快!

    3) 尝试查看 Homography 矩阵(在调试模式下单步执行或打印),看看它是否一致。

    4) 如果上面没有给你一个线索,试着看看形成的匹配。是否将一幅图像中的一个点与另一幅图像中的多个点匹配?如果是这样,问题又应该出在描述符或检测器上。

    我的直觉是描述符(所以 1)或 2)应该修复它)。

    【讨论】:

      【解决方案2】:

      同时在 BruteForceMatcher 中切换到 Hamming 距离而不是 L1 距离。应该使用汉明距离来比较简短的描述符。

      【讨论】:

        【解决方案3】:

        您的单应性,可能基于错误的匹配计算,因此代表错误的对齐方式。 我建议通过对行之间的相互依赖关系的额外检查来引导矩阵。

        您可以使用以下代码:

        bool cvExtCheckTransformValid(const Mat& T){
        
            // Check the shape of the matrix
            if (T.empty())
               return false;
            if (T.rows != 3)
               return false;
            if (T.cols != 3)
               return false;
        
            // Check for linear dependency.
            Mat tmp;
            T.row(0).copyTo(tmp);
            tmp /= T.row(1);
            Scalar mean;
            Scalar stddev;
            meanStdDev(tmp,mean,stddev);
            double X = abs(stddev[0]/mean[0]);
            printf("std of H:%g\n",X);
            if (X < 0.8)
               return false;
        
            return true;    
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-30
          • 2019-01-01
          • 2020-03-10
          • 2020-03-25
          • 2012-01-02
          相关资源
          最近更新 更多