Question

I'm trying to use cvPerspectiveTransform to transform four 2D points. I got the transformation matrix (3x3) already through cvFindHomography. I can't figure out what kind of structure to provide to not run into some error. Would anybody be so kind to show me how to do it with these points?

  • x:y
  • 0:0
  • 640:0
  • 0:480
  • 640:480

I'm using OpenCV 2.4.0 on Win.

Was it helpful?

Solution

This is one way to initialize your matrices correctly. It's probably not the most elegant, but it works:

CvMat* input = cvCreateMat(1, 4, CV_32FC2);
CvMat* output = cvCreateMat(1, 4, CV_32FC2);

float data[8] = {0,0,0,640,480,0,640,480};

for (int i =0; i < 8; i++)
{
    input->data.fl[i] = data[i];
}

cvPerspectiveTransform(input, output, matrix_from_cvFindHomography);

The C++ API offers a more intuitive implementation. Many OpenCV functions, like perspectiveTransform, accept vectors of points as inputs, which can be initialized in this manner:

std::vector<cv::Point2f> inputs;
std::vector<cv::Point2f> outputs;

inputs.push_back(cv::Point2f(0,0));
inputs.push_back(cv::Point2f(640,0));
inputs.push_back(cv::Point2f(0,480));
inputs.push_back(cv::Point2f(640,480));

cv::perspectiveTransform(inputs, outputs, matrix_from_findHomography);

OTHER TIPS

assuming you have a 3x3 cv::Mat of floats, you can convert that to (if you want double change all the f's to d's)

cv::Matx33f transform(your_cv_Mat);
cv::Matx31f pt1(0,0,1);
cv::Matx31f pt2(640,0,1);
...
pt1 = transform*pt1;
pt2 = transform*pt2;
...

make sure you normalize by the third coordinate, read up on homogenous coordinates if that does not make sense

pt1 *= 1/pt1(2);
pt2 *= 1/pt2(2);
...
cv::Point2f final_pt1(pt1(0),pt1(1));
cv::Point2f final_pt2(pt2(0),pt2(1));

You do not need to do this with Matx, it will work with cv::Mat just as well. Personally I like Matx for working with transforms because its size and type is easier to keep track of and its contents can be more easily viewed in the debugger.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top