OpenCV - 图像拼接

8
我使用以下代码来拼接两张输入图片。不知何故,输出结果很糟糕!似乎单应性矩阵是错误的(或者被错误地影响了),因为变换后的图像就像是一个“被利用的星星”!我已经注释了我认为是问题源头的部分,但我无法理解它。任何帮助或指点都将不胜感激!
祝你有愉快的一天, Ali
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); 
}
3个回答

7
这是我建议你尝试的步骤:
1)使用CV_RANSAC选项进行单应性矩阵估计。请参考http://opencv.willowgarage.com/documentation/cpp/calib3d_camera_calibration_and_3d_reconstruction.html 2)尝试其他描述符,特别是OpenCV中提供的SIFT或SURF。对于某些图像,FAST或BRIEF描述符可能不够区分。编辑('12年8月):基于BRIEF的ORB描述符非常好且快速!
3)尝试查看单应性矩阵(在调试模式下逐步执行或打印它),并检查其是否一致。
4)如果以上步骤都无法解决问题,请尝试查看匹配点。是否将一个图像中的一个点与另一个图像中的多个点匹配?如果是,则问题可能出在描述符或检测器上。
我猜测问题出在描述符上(因此1)或2)可以解决它)。

2
在BruteForceMatcher中,也要切换到Hamming距离而不是L1距离。BRIEF描述符应该使用Hamming距离进行比较。

1

你的单应矩阵可能是基于错误匹配计算的,因此代表了错误的对齐。

我建议通过对其行之间的相互依赖性进行额外检查来修正矩阵。

你可以使用以下代码:

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;    
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接