OpenCV 2.4.3-在裁切后的图像上使用反向单应性的warpPerspective [英] OpenCV 2.4.3 - warpPerspective with reversed homography on a cropped image

查看:240
本文介绍了OpenCV 2.4.3-在裁切后的图像上使用反向单应性的warpPerspective的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用SURF在场景中查找参考图像时,我想裁剪场景中找到的对象,然后使用warpPerspective和反向单应性矩阵将其拉直".
意思是说我有这个SURF结果:


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


然后使用反向单应性矩阵使用warpPerspective仅拉直"裁剪的图像.我要瞄准的结果是,我将获得一张图像,其中仅包含对象,并且原始场景中残留了一些扭曲的残影(因为裁剪并不是仅对象的100%).

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

有什么建议吗?

When finding a reference image in a scene using SURF, I would like to crop the found object in the scene, and "straighten" it back using warpPerspective and the reversed homography matrix.

Meaning, let's say I have this SURF result:


Now, I would like to crop the found object in the scene:


and "straighten" only the cropped image with warpPerspective using the reversed homography matrix. The result I'm aiming at is that I'll get an image containing, roughly, only the object, and some distorted leftovers from the original scene (as the cropping is not a 100% the object alone).

Cropping the found object, and finding the homography matrix and reversing it are simple enough. Problem is, I can't seem to understand the results from warpPerspective. Seems like the resulting image contains only a small portion of the cropped image, and in a very large size.
While researching warpPerspective I found that the resulting image is very large due to the nature of the process, but I can't seem to wrap my head around how to do this properly. Seems like I just don't understand the process well enough. Would I need to warpPerspective the original (not cropped) image and than crop the "straightened" object?

Any advice?

推荐答案

尝试一下.

如果您具有对象的未连接轮廓(例如,框轮廓的外角点),则可以使用反单应性对其进行变换并将其调整为将变换的结果放置到对象的左上角区域图片.

given that you have the unconnected contour of your object (e.g. the outer corner points of the box contour) you can transform them with your inverse homography and adjust that homography to place the result of that transformation to the top left region of the image.

  1. 计算那些对象点将变形的位置(使用反单应性和轮廓点作为输入):

  1. compute where those object points will be warped to (use the inverse homography and the contour points as input):

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

  • 修改逆单应性(computeWarpedContourRegion和inverseHomography的结果作为输入)

  • modify your inverse homography (result of computeWarpedContourRegion and inverseHomography as input)

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

  • 您将称呼类似

  • you will call something like

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

    希望这有帮助=)

    这篇关于OpenCV 2.4.3-在裁切后的图像上使用反向单应性的warpPerspective的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆