Convert opencv affine matrix to CGAffineTransform

I would like to take the affine matrix I got from OpenCV:

Mat T = getAffineTransform(src_pt, dst_pt);

      

and then convert it to CGAffineTransform for use in Core Graphics / Objective-C / iOS. ( CGAffineTransform docs )

I tried:

CGAffineTransform t = CGAffineTransformIdentity;
t.a = T.at<float>(0,0);
t.b = T.at<float>(0,1);
t.c = T.at<float>(1,0);
t.d = T.at<float>(1,1);
t.tx = T.at<float>(0,2);
t.ty = T.at<float>(1,2);

      

This works great for x and y transitions, but NOT if there is any rotation in the matrix. Something seems to be missing, as the resulting image looks strange. I tried multiplying .b

both .c

by -1 and switching .b

and .c

, but none of them worked. The image is still not properly distorted.

Edit: I should mention that it almost rotates correctly. When I switch b and c, it at least turns in the right direction. It looks a bit like being rotated too much.

+1


source to share


1 answer


Your problem is that opencv is a string and CGAffineTransform is the main column you want

t.a = T.at<float>(0,0);
t.b = T.at<float>(1,0);
t.c = T.at<float>(0,1);
t.d = T.at<float>(1,1);

      

you can tell because in the documentation the CGAffineTransform takes the form

[a  b  0
 c  d  0
 tx ty 1]

      



note that tx and ty are on the bottom line. In standard matrices of main strings, translation components are included in the rightmost column

[a c tx
 b d ty
 0 0 1]

      

If the issue persists after making this change (which you already talked about in your question, you've already tried it), then you need to post more information. Since you are guessing that the problem may be in your coordinate system, but without any information about your origin, no one can help you.

+1


source







All Articles