Affine transformation of selected area

648 views Asked by At

I want to create a tool for affine transformation on selected part of image(4 corners polygon, see image link). http://s14.postimg.org/fn9btus8h/explanation.png

I tried to use opencv but without success.

Thanks for help. Regards

2

There are 2 answers

0
yaroslav On BEST ANSWER

Thanks everyone for help. I found solution.

I used Qt functionality. Here is my code:

QImage img;
img.load("D:\\sample.png");
QPolygonF polygonIn;
polygonIn  << QPointF(29, 25)
    << QPointF(147, 9)
    << QPointF(210, 100)
    << QPointF(0, 84);

QPolygonF polygonOut;   
polygonOut  << QPointF(29, 25)
    << QPointF(147, 25)
    << QPointF(210, 132)
    << QPointF(0, 84);

QTransform transform;
auto isOk = QTransform::quadToQuad(polygonIn, polygonOut, transform);
if(!isOk)
    throw std::runtime_error("Transformation impossible with such parameters.");

img = img.transformed(transform.toAffine(), Qt::TransformationMode::SmoothTransformation);
0
Kamyar Infinity On

You should show us what you have tried, and how you got no success. You may be using wrong points for creating the transformation matrix. The correct procedure is described below.

You should use getAffineTransform to create an affine transformation matrix. This maps an input triangle to an output triangle, as mentioned in the documentation. But you should make sure that you pass three corresponding points of your two planes.

If you are not sure how to choose three corresponding points of your two planes, just use getPerspectiveTransform and pass in four points of two planes, in their respective order. It still works, as affine transform is just a special case of perspective transform.

After getting the transformation matrix, you should use warpAffine or warpPerspective to map the input image.