OpenCV的战争

时间:2011-05-22 10:06:01

标签: c++ image image-processing opencv

出于某种原因,每当我使用OpenCV的warpPerspective()函数时,最终的扭曲图像都不包含原始图像中的所有内容。图像的左侧部分似乎被切断了。我认为发生这种情况的原因是因为warpPerspective()在画布最左边的位置创建了扭曲图像。有办法解决这个问题吗?感谢

9 个答案:

答案 0 :(得分:31)

问题出现是因为单应性将图像的一部分映射到图像区域外的负x,y值,因此无法绘制。 我们希望做的是将扭曲的输出偏移一定数量的像素,以将整个变形图像“分流”为正坐标(因此在图像区域内)。

可以使用矩阵乘法来组合Homographies(这就是为什么它们如此强大)。如果A和B是单应性,则AB表示首先应用B的单应性,然后是A.

因此,我们需要做的就是偏移输出,然后通过一些偏移为平移创建单应矩阵,然后用我们原始的单应矩阵预乘它

2D单应矩阵如下所示:

[R11,R12,T1]
[R21,R22,T2]
[ P , P , 1]

其中R表示旋转矩阵,T表示平移,P表示透视扭曲。 因此,纯粹的翻译单应性看起来像这样:

[ 1 , 0 , x_offset]
[ 0 , 1 , y_offset]
[ 0 , 0 ,    1    ]

所以,只需用类似于上面的矩阵预乘你的单应性,输出图像就会偏移。

(确保使用矩阵乘法,而不是元素乘法!)

答案 1 :(得分:4)

秘密分为两部分:变换矩阵(单应性)和结果图像尺寸。

  • 使用getPerspectiveTransform()计算正确的变换。从原始图像中取4个点,计算它们在目的地中的正确位置,将它们以相同的顺序放在两个向量中,然后用它们计算透视变换矩阵。

  • 确保目标图像大小(warpPerspective()的第三个参数)正是您想要的。将其定义为大小(myWidth,myHeight)。

答案 2 :(得分:4)

我做了一个方法...... 它正在发挥作用。

  perspectiveTransform(obj_corners,scene_corners,H);
int maxCols(0),maxRows(0);

 for(int i=0;i<scene_corners.size();i++)
{
   if(maxRows < scene_corners.at(i).y)
        maxRows = scene_corners.at(i).y;
   if(maxCols < scene_corners.at(i).x)
        maxCols = scene_corners.at(i).x;
}

我只是分别找到x点和y点的最大值并将其放在

warpPerspective( tmp, transformedImage, homography, Size( maxCols, maxRows ) );

答案 3 :(得分:2)

请尝试以下homography_warp

void homography_warp(const cv::Mat& src, const cv::Mat& H, cv::Mat& dst);

src是源图片。

H是你的单应性。

dst是扭曲的图像。

homography_warp按照https://stackoverflow.com/users/1060066/matt-freeman的答案调整你的单应性https://stackoverflow.com/a/8229116/15485

// Convert a vector of non-homogeneous 2D points to a vector of homogenehous 2D points.
void to_homogeneous(const std::vector< cv::Point2f >& non_homogeneous, std::vector< cv::Point3f >& homogeneous)
{
    homogeneous.resize(non_homogeneous.size());
    for (size_t i = 0; i < non_homogeneous.size(); i++) {
        homogeneous[i].x = non_homogeneous[i].x;
        homogeneous[i].y = non_homogeneous[i].y;
        homogeneous[i].z = 1.0;
    }
}

// Convert a vector of homogeneous 2D points to a vector of non-homogenehous 2D points.
void from_homogeneous(const std::vector< cv::Point3f >& homogeneous, std::vector< cv::Point2f >& non_homogeneous)
{
    non_homogeneous.resize(homogeneous.size());
    for (size_t i = 0; i < non_homogeneous.size(); i++) {
        non_homogeneous[i].x = homogeneous[i].x / homogeneous[i].z;
        non_homogeneous[i].y = homogeneous[i].y / homogeneous[i].z;
    }
}

// Transform a vector of 2D non-homogeneous points via an homography.
std::vector<cv::Point2f> transform_via_homography(const std::vector<cv::Point2f>& points, const cv::Matx33f& homography)
{
    std::vector<cv::Point3f> ph;
    to_homogeneous(points, ph);
    for (size_t i = 0; i < ph.size(); i++) {
        ph[i] = homography*ph[i];
    }
    std::vector<cv::Point2f> r;
    from_homogeneous(ph, r);
    return r;
}

// Find the bounding box of a vector of 2D non-homogeneous points.
cv::Rect_<float> bounding_box(const std::vector<cv::Point2f>& p)
{
    cv::Rect_<float> r;
    float x_min = std::min_element(p.begin(), p.end(), [](const cv::Point2f& lhs, const cv::Point2f& rhs) {return lhs.x < rhs.x; })->x;
    float x_max = std::max_element(p.begin(), p.end(), [](const cv::Point2f& lhs, const cv::Point2f& rhs) {return lhs.x < rhs.x; })->x;
    float y_min = std::min_element(p.begin(), p.end(), [](const cv::Point2f& lhs, const cv::Point2f& rhs) {return lhs.y < rhs.y; })->y;
    float y_max = std::max_element(p.begin(), p.end(), [](const cv::Point2f& lhs, const cv::Point2f& rhs) {return lhs.y < rhs.y; })->y;
    return cv::Rect_<float>(x_min, y_min, x_max - x_min, y_max - y_min);
}

// Warp the image src into the image dst through the homography H.
// The resulting dst image contains the entire warped image, this
// behaviour is the same of Octave's imperspectivewarp (in the 'image'
// package) behaviour when the argument bbox is equal to 'loose'.
// See http://octave.sourceforge.net/image/function/imperspectivewarp.html
void homography_warp(const cv::Mat& src, const cv::Mat& H, cv::Mat& dst)
{
    std::vector< cv::Point2f > corners;
    corners.push_back(cv::Point2f(0, 0));
    corners.push_back(cv::Point2f(src.cols, 0));
    corners.push_back(cv::Point2f(0, src.rows));
    corners.push_back(cv::Point2f(src.cols, src.rows));

    std::vector< cv::Point2f > projected = transform_via_homography(corners, H);
    cv::Rect_<float> bb = bounding_box(projected);

    cv::Mat_<double> translation = (cv::Mat_<double>(3, 3) << 1, 0, -bb.tl().x, 0, 1, -bb.tl().y, 0, 0, 1);

    cv::warpPerspective(src, dst, translation*H, bb.size());
}

答案 4 :(得分:1)

warpPerspective()工作正常。无需重写它。 你可能错误地使用它。

请记住以下提示:

  1. (0,0)像素不在中心,而是在左上角。因此,如果您放大图像x2,您将丢失下部和右部,而不是边框​​(如在matlab中)。
  2. 如果两次扭曲图像,最好将变换相乘并激活一次该函数。
  3. 我认为它只适用于char / int矩阵而不适用于float / double。
  4. 进行转换时,将应用第一个缩放/倾斜/旋转/透视,最后应用转换。因此,如果缺少部分图像,只需更改矩阵中的转换(最后一列的两个上行)。

答案 5 :(得分:1)

解决变形图像投影到变形输出之外的简单方法是将变形图像平移到正确的位置。主要挑战在于找到正确的翻译偏移量。

翻译的概念已经在此处给出的其他答案中进行了讨论,因此我将解释如何获得正确的偏移量。想法是,两幅图像中的匹配特征在最终缝合的图像中应具有相同的坐标。

假设我们按以下方式引用图片:

  • “源图像”(si):需要变形的图像
  • “目标图片”(di):透视图“源图片”将变形的图片
  • “变形的源图像”(wsi):源图像 将其扭曲到目标图像透视图之后

以下是您需要执行的操作,以便计算平移的偏移量:

  1. 在对好的匹配进行采样并从单应性图中找到蒙版之后,存储最佳匹配的关键点(一个距离最小且为整数的(应从单应性计算中获得的蒙版中值为1))在sidi中。假设分别在si and di is bm_si and bm_di`中最匹配的关键点。

    bm_si = [x1, y1,1]

    bm_di = [x2, y2, 1]

  2. 只需将bm_si与单应性矩阵(wsi)相乘即可找到Hbm_wsi = np.dot(H,bm_si)中的位置。 bm_wsi = [x/bm_wsi[2] for x in bm_wsi]

    di

  3. 根据要在si变形(= {wsi)的输出上放置bm_di的位置,调整si

    让我们说,如果您要从左图向右图(例如,左图为di,右图为di)变形,那么您将wsi置于右图bm_di[0] += si.shape[0]边,因此x_offset = bm_di[0] - bm_si[0]

  4. 完成上述步骤后

    y_offset = bm_di[1] - bm_si[1]

    si

  5. 使用计算出的偏移量找到新的单应性矩阵并扭曲T = np.array([[1, 0, x_offset], [0, 1, y_offset], [0, 0, 1]])

    translated_H = np.dot(T.H)

    wsi_frame_size = tuple(2*x for x in si.shape)

    stitched = cv2.warpPerspective(si, translated_H, wsi_frame_size)

    stitched[0:si.shape[0],si.shape[1]:] = di

    MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/'

答案 6 :(得分:0)

这是我的解决方案

因为&#34; warpPerspective()&#34;中的第三个参数是一个转换矩阵,

我们可以制作转换矩阵, 它首先向后移动图像,然后旋转图像,最后向前移动图像。

就我而言,我的图像高度为160像素,宽度为160像素。 我想围绕[80,80]而不是[0,0]

左右旋转图像

首先,向后移动图像(即T1)

然后

旋转图像(表示R)

最后向前移动图像(即T2)

void rotateImage(Mat &src_img,int degree)
{
float radian=(degree/180.0)*M_PI;
Mat R(3,3,CV_32FC1,Scalar(0));
R.at<float>(0,0)=cos(radian);R.at<float>(0,1)=-sin(radian);
R.at<float>(1,0)=sin(radian);R.at<float>(1,1)=cos(radian);
R.at<float>(2,2)=1;
Mat T1(3,3,CV_32FC1,Scalar(0));
T1.at<float>(0,2)=-80;
T1.at<float>(1,2)=-80;
T1.at<float>(0,0)=1;
T1.at<float>(1,1)=1;
T1.at<float>(2,2)=1;
Mat T2(3,3,CV_32FC1,Scalar(0));
T2.at<float>(0,2)=80;
T2.at<float>(1,2)=80;
T2.at<float>(0,0)=1;
T2.at<float>(1,1)=1;
T2.at<float>(2,2)=1;

std::cerr<<T1<<std::endl;
std::cerr<<R<<std::endl;
std::cerr<<T2<<std::endl;
std::cerr<<T2*R*T1<<"\n"<<std::endl;

cv::warpPerspective(src_img, src_img, T2*R*T1, src_img.size(), cv::INTER_LINEAR);
}

答案 7 :(得分:0)

马特的答案是一个好的开始,他说你需要将单应性乘以

是正确的。
[ 1 , 0 , x_offset]
[ 0 , 1 , y_offset]
[ 0 , 0 ,    1    ]

但是他没有指定x_offset和y_offset是什么。其他答案说只是进行透视变换,但这是不正确的。您想进行逆视透视变换。

仅因为点0,0转换为-10,-10,并不意味着将图像移位10,10不会导致图像被裁剪。这是因为点10,10不一定映射到0,0。
您要做的是找出映射到0,0的点,然后将图像移动这么多。为此,您应采用单应性的倒数(cv2.invert)并应用PerspectiveTransform。

enter image description here 不暗示: enter image description here

您需要应用逆向变换来找到正确的点。

enter image description here

这将获得正确的x_offset和y_offset以对齐您的左上点。从那里找到正确的边界框并完美拟合整个图像,您需要找出偏斜度(在正常,非逆变换之后,图像偏左或偏上多少)并将其添加到x_offset和y_offset中也是

编辑:这全是理论。图片在我的测试中相差了几个像素,我不确定为什么。

答案 8 :(得分:-1)

这是针对您的问题的opencv-python解决方案,我把它放在github上:https://github.com/Sanster/notes/blob/master/opencv/warpPerspective.md

关键点是user3094631所说的,得到两个平移矩阵(T1,T2)并应用于旋转矩阵(M)T2*M*T1

在我给出的代码中,T1来自原点图像的中心点,T2来自变换的boundingBox的左上角。转换的boundingBox来自原点角点:

height = img.shape[0]
width = img.shape[1]
#..get T1
#..get M
pnts = np.asarray([
    [0, 0],
    [width, 0],
    [width, height],
    [0, height]
    ], dtype=np.float32)
pnts = np.array([pnts])
dst_pnts = cv2.perspectiveTransform(pnts, M * T1)[0]
dst_pnts = np.asarray(dst_pnts, dtype=np.float32)
bbox = cv2.boundingRect(dst_pnts)
T2 = np.matrix([[1., 0., 0 - bbox[0]],
                [0., 1., 0 - bbox[1]],
                [0., 0., 1.]])