Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Applying Affine transform to single points rather than entire image

Tags:

c++

opencv

I've got a Affine transform matrix in OpenCV from the KeypointBasedMotionEstimator class.

It comes in a form like:

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

I would now like to apply the transform to a vector< Pointf >, so that it will transform each point as it would be if they were in the image.

The OpenCV does not seem to allow transforming points only, the function:

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

Only seems to take images as inputs and outputs.

Is there a way I can apply an affine transform to single points in OpenCV?

like image 953
SvaLopLop Avatar asked May 12 '15 14:05

SvaLopLop


1 Answers

you can use

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

not sure about Point datatype, but the transformation must have double type, e.g. CV_64FC1

see http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#perspectivetransform too

like image 194
Micka Avatar answered Sep 21 '22 16:09

Micka