8

我在图像上运行 cv::warpPerspective() 函数,以及如何获取我在源图像中获得的结果图像的一些点的位置,这里我走了多远:

 int main (){
    cv::Point2f srcQuad[4],dstQuad[4];
    cv::Mat warpMatrix;
    cv::Mat src, dst,src2;
    src = cv::imread("card.jpg",1);
            srcQuad[0].x = 0; //src Top left
    srcQuad[0].y = 0;
    srcQuad[1].x = src.cols - 1; //src Top right
    srcQuad[1].y = 0;
    srcQuad[2].x = 0; //src Bottom left
    srcQuad[2].y = src.rows - 1;
    srcQuad[3].x = src.cols -1; //src Bot right
    srcQuad[3].y = src.rows - 1;
    dstQuad[0].x = src.cols*0.05; //dst Top left
    dstQuad[0].y = src.rows*0.33;
    dstQuad[1].x = src.cols*0.9; //dst Top right
    dstQuad[1].y = src.rows*0.25;
    dstQuad[2].x = src.cols*0.2; //dst Bottom left
    dstQuad[2].y = src.rows*0.7;
    dstQuad[3].x = src.cols*0.8; //dst Bot right
    dstQuad[3].y = src.rows*0.9;

    warpMatrix =cv::getPerspectiveTransform(srcQuad,dstQuad);

    cv::warpPerspective(src,dst,warpMatrix,src.size());
    cv::imshow("source", src);
    cv::imshow("destination", dst);
    cv::warpPerspective(dst,src2,warpMatrix,dst.size(),CV_WARP_INVERSE_MAP);
    cv::imshow("srouce 2 " , src2);
    cv::waitKey();
    return 0;

我的问题是,如果我从dst中选择一个点,如何在 ** src 或 src2 ** 中获取它的坐标,因为cv::warpPerspective函数不将 cv::Point 作为参数?

4

2 回答 2

8

您需要perspectiveTransform(适用于Points 向量)而不是warpPerspective。取warpMatrix的逆;您可能需要调整最后一列。

vector<Point2f> dstPoints, srcPoints;
dstPoints.push_back(Point2f(1,1));

cv::perspectiveTransform(dstPoints,srcPoints,warpMatrix.inv());
于 2013-07-25T18:57:07.873 回答
6

透视变换以下列方式关联两点:

[x']   [m00 m01 m02] [x]
[y'] = [m10 m11 m12] [y]
[1]    [m20 m21 m22] [1]

(x,y)原始 2D 点坐标在哪里,(x', y')是转换后的坐标。

在你的情况下,你知道(x', y'),并且想知道(x, y)。这可以通过将已知点乘以变换矩阵的逆来实现:

cv::Matx33f warp = warpMatrix;          // cv::Matx is much more useful for math
cv::Point2f warped_point = dstQuad[3];  // I just use dstQuad as an example
cv::Point3f homogeneous = warp.inv() * warped_point;
cv::Point2f result(homogeneous.x, homogeneous.y);  // Drop the z=1 to get out of homogeneous coordinates
// now, result == srcQuad[3], which is what you wanted
于 2013-07-25T16:12:40.177 回答