OpenCV将仿射变换应用于单个点而不是整个图像

时间:2015-05-12 14:42:34

标签: c++ opencv

我在KeypointBasedMotionEstimator类的OpenCV中有一个仿射变换矩阵。

它的形式如下:

[1.0008478, -0.0017408683, -10.667297;
0.0011812132, 1.0009096, -3.3626099;
0, 0, 1]

我现在想将变换应用于矢量< Pointf&gt ;,这样它就可以转换每个点,就像它们在图像中一样。

OpenCV似乎不允许转换点,函数:

 void cv::warpAffine    (   InputArray      src,
    OutputArray     dst,
    InputArray      M,
    Size    dsize,
    int     flags = INTER_LINEAR,
    int     borderMode = BORDER_CONSTANT,
    const Scalar &      borderValue = Scalar() 
)   

似乎只将图像作为输入和输出。

有没有办法可以在OpenCV中对单点应用仿射变换?

2 个答案:

答案 0 :(得分:3)

你可以使用

void cv::perspectiveTransform(InputArray src, OutputArray dst, InputArray m)

e.g。

cv::Mat yourAffineMatrix(3,3,CV_64FC1);
[...] // fill your transformation matrix

std::vector<cv::Point2f> yourPoints;
yourPoints.push_back(cv::Point2f(4,4));
yourPoints.push_back(cv::Point2f(0,0));

std::vector<cv::Point2f> transformedPoints;

cv::perspectiveTransform(yourPoints, transformedPoints, yourAffineMatrix);

不确定Point数据类型,但转换必须具有double类型,例如CV_64FC1

另见http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#perspectivetransform

答案 1 :(得分:2)

它有点笨拙,但你可以手动矩阵乘以你的点数:

// the transformation matrix
Mat_<float> M(3,3); 
M << 1.0008478, -0.0017408683, -10.667297,
     0.0011812132, 1.0009096, -3.3626099,
     0, 0, 1;

// a point
Point2f p(4,4);

// make a Mat for multiplication, 
// must have same type as transformation mat !
Mat_<float> pm(3,1);
pm << p.x,p.y,1.0;

// now , just multiply:
Mat_<float> pr =  M * pm;

// retrieve point:    
Point2f pt(pr(0), pr(1));
cerr << pt << endl;
[-6.67087, 0.645753]