【问题标题】:OpenCV 2.4.3 - warpPerspective with reversed homography on a cropped imageOpenCV 2.4.3 - 在裁剪图像上具有反向单应性的 warpPerspective
【发布时间】:2013-05-11 08:05:05
【问题描述】:

当使用 SURF 在场景中查找参考图像时,我想裁剪场景中找到的对象,并使用 warpPerspective 和反向单应矩阵将其“拉直”。

意思是,假设我有这个 SURF 结果:


现在,我想裁剪场景中找到的对象:


并使用反向单应矩阵仅使用 warpPerspective 对裁剪后的图像进行“拉直”。我的目标是得到一张图像,大致上只包含对象,以及原始场景中的一些失真剩余部分(因为裁剪不是 100% 单独的对象)。

裁剪找到的对象,找到单应矩阵并反转它很简单。问题是,我似乎无法理解 warpPerspective 的结果。似乎生成的图像仅包含裁剪图像的一小部分,并且尺寸非常大。
在研究 warpPerspective 时,我发现由于过程的性质,生成的图像非常大,但我似乎无法理解如何正确执行此操作。好像我只是不太了解这个过程。我是否需要对原始(未裁剪)图像进行 warpPerspective 并裁剪“拉直”对象?

有什么建议吗?

【问题讨论】:

  • 嗨,你找到解决办法了吗?
  • 不幸的是,我不得不离开这里去处理另一个项目,但这绝对是我会回来的。如果您确实找到了解决方案,如果您能发布您的发现,那就太好了!
  • 可以同时使用 surf 和 sift 构建库,但问题是我必须为 android 实现.. 所以正在努力...

标签: opencv surf homography


【解决方案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);

希望这会有所帮助 =)

【讨论】:

  • 你的解释中的“图像”是什么意思?假设我们有一个对象图像和一个场景图像。你是说物体图像吗?
猜你喜欢
  • 1970-01-01
  • 2012-02-12
  • 2012-12-31
  • 1970-01-01
  • 1970-01-01
  • 2017-08-04
  • 2023-03-05
  • 1970-01-01
  • 2018-08-27
相关资源
最近更新 更多