0

I'm trying to translate the code in the end of that page from python to C++. I'm stuck at this line:

image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=resample);

Apparently I will have to use

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

which expects "a transformation 2x2 or 2x3 floating-point matrix" as a m. I reckon that I have to put the (a,b,c,d,e,f) part of the python code therein, but how exactly? what would be the format of the required 2x3 matrix in terms of the above elements?

1 Answer 1

1

You want to use cv::WarpAffine not cv::transform, since it is the function to make affine transformations on an image (cv::transform is for matrices of points). It's pretty easy to match the (a,b,c,d,e,f) from PIL to what OpenCV wants by checking the docs.

From PIL docs we see:

Data is a 6-tuple (a, b, c, d, e, f) which contain the first two rows from an affine transform matrix. For each pixel (x, y) in the output image, the new value is taken from a position (a x + b y + c, d x + e y + f)

And from the OpenCV docs:

function warpAffine transforms the source image using the specified matrix:

dst(x,y) = src( M11 x + M12 y + M13, M21 x + M22 y + M23)

So you can initialise your matrix as cv::Mat warp_mat( 2, 3, CV_32FC1 ); and then populate the entries with a,b,c,d,e,f.

Sign up to request clarification or add additional context in comments.

1 Comment

so warp_mat.at<float>(0,0) = a, warp_mat.at<float>(0,1) = b, warp_mat.at<float>(0,2) = c, warp_mat.at<float>(1,0) = d, warp_mat.at<float>(1,1) = e, warp_mat.at<float>(1,2) = f, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.