2013-05-16 59 views
2

使用SURF在场景中查找参考图像时,我想裁剪找到的场景中的物体,并使用warpPerspective将其“回正”单应矩阵。

含义,让我们说我有这个SURF结果:
enter image description here

现在,我想裁剪找到的对象场景:
enter image description here

和 “拉直” 只有使用反转的单应性矩阵,使用warpPerspective裁剪图像。我所瞄准的结果是,我将得到一幅图像,大致只包含对象,以及原始场景中的一些扭曲的残留物(因为裁剪不是单独的100%对象)。

裁剪找到的对象,并找到单应性矩阵并反转它很简单。问题是,我似乎无法理解warp的结果。看起来像所产生的图像仅包含裁剪图像的一小部分,并且尺寸非常大。
在研究warpPerspective期间,我发现由于过程的性质而产生的图像非常大,但我似乎无法围绕如何正确地做到这一点。好像我只是不理解这个过程。我是否需要扭曲查看原始(未裁剪)的图像,然后裁剪“拉直”的对象?

有什么建议吗?OpenCV 2.4.3 - 在裁剪后的图像上具有反转单应矩阵的warpPerspective

+0

喜做ü找到任何解决办法? – user1140237

+0

不幸的是,我不得不离开这个来处理另一个项目,但这绝对是我会回头的。如果你确实想出了一个解决方案,如果你能发布你的发现,那将是非常好的! –

+0

米能够建立图书馆使用冲浪和筛选两个,但问题是我必须为Android实施..所以工作... ... – user1140237

回答

1

试试这个。

考虑到您的对象的轮廓不相关(例如,框轮廓的外角点),您可以使用逆单应矩阵将它们转换并调整该单应矩阵以将该转换的结果放置到左上角区域图片。

  1. 计算其中那些对象点将被扭曲至(使用逆单应性和所述轮廓点作为输入):

    cv::Rect computeWarpedContourRegion(const std::vector<cv::Point> & points, const cv::Mat & homography) 
    { 
        std::vector<cv::Point2f> transformed_points(points.size()); 
    
        for(unsigned int i=0; i<points.size(); ++i) 
        { 
         // warp the points 
         transformed_points[i].x = points[i].x * homography.at<double>(0,0) + points[i].y * homography.at<double>(0,1) + homography.at<double>(0,2) ; 
         transformed_points[i].y = points[i].x * homography.at<double>(1,0) + points[i].y * homography.at<double>(1,1) + homography.at<double>(1,2) ; 
        } 
    
        // dehomogenization necessary? 
        if(homography.rows == 3) 
        { 
         float homog_comp; 
         for(unsigned int i=0; i<transformed_points.size(); ++i) 
         { 
          homog_comp = points[i].x * homography.at<double>(2,0) + points[i].y * homography.at<double>(2,1) + homography.at<double>(2,2) ; 
          transformed_points[i].x /= homog_comp; 
          transformed_points[i].y /= homog_comp; 
         } 
        } 
    
        // now find the bounding box for these points: 
        cv::Rect boundingBox = cv::boundingRect(transformed_points); 
        return boundingBox; 
    } 
    
  2. 修改逆单应性(computeWarpedContourRegion和inverseHomography作为输入的结果)

    cv::Mat adjustHomography(const cv::Rect & transformedRegion, const cv::Mat & homography) 
    { 
        if(homography.rows == 2) throw("homography adjustement for affine matrix not implemented yet"); 
    
        // unit matrix 
        cv::Mat correctionHomography = cv::Mat::eye(3,3,CV_64F); 
        // correction translation 
        correctionHomography.at<double>(0,2) = -transformedRegion.x; 
        correctionHomography.at<double>(1,2) = -transformedRegion.y; 
    
    
        return correctionHomography * homography; 
    } 
    
  3. 你要给像

cv::warpPerspective(objectWithBackground, output, adjustedInverseHomography, sizeOfComputeWarpedContourRegionResult);

希望这有助于=)