将opencv仿射矩阵转换为CGAffineTransform [英] convert an opencv affine matrix to CGAffineTransform

查看:101
本文介绍了将opencv仿射矩阵转换为CGAffineTransform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从OpenCV中获取一个仿射矩阵:

Mat T = getAffineTransform(src_pt, dst_pt);

,然后将其转换为CGAffineTransform,以用于Core Graphics/Objective-C/iOS. ( CGAffineTransform文档)

我尝试过:

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

这对于x和y平移工作正常,但如果矩阵中有任何旋转,则无效.由于生成的图像似乎怪异地歪斜,因此似乎缺少了某些东西.我试过将.b.c乘以-1并切换.b.c,但是这些似乎都不起作用.图像仍然显示为不正确的偏斜.

我要说的是,几乎已正确旋转.当我切换b和c时,它至少朝着正确的方向旋转.似乎有点偏离,因为旋转太远了.

解决方案

您的问题是opencv是您想要的行主行,而CGAffineTransform是您想要的列主行

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

您可以知道,因为在CGAffineTransform文档中采用的格式是

[a  b  0
 c  d  0
 tx ty 1]

请注意,tx和ty在底行.在标准行主要矩阵中,翻译组件位于最右边的列中

[a c tx
 b d ty
 0 0 1]

如果进行此更改后问题仍然存在(您的问题表明您已经尝试过),则需要发布更多信息.正如您所建议的那样,问题可能出在坐标系的原点,但没有有关原点的任何信息,没人会帮助您.

I'd like to take an affine matrix that I have from OpenCV:

Mat T = getAffineTransform(src_pt, dst_pt);

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

I've 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 fine for x and y translations, but NOT if there is any rotation in the matrix. Something seems to be missing since the resulting image seems to be skewed strangely. I've tried multiplying .b and .c by -1 and switching .b and .c, but neither of those seemed to work. The image still appeared incorrectly skewed.

Edit: I should mention that it's almost rotated correctly. When I switch b and c it's at least rotated in the right direction. It just seems a bit off, as in rotated a little too far.

解决方案

Your problem is that opencv is row major and CGAffineTransform is column major 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 CGAffineTransform takes the form

[a  b  0
 c  d  0
 tx ty 1]

note that tx and ty are in the bottom row. In standard row major matrices the translation components go in the rightmost column

[a c tx
 b d ty
 0 0 1]

If the problem persists after making this change (which your question suggests you have already tried) then you need to post more information. As you suggest the problem could be in the origin of your coordinate system but without any information about your origin nobody will be able to help you.

这篇关于将opencv仿射矩阵转换为CGAffineTransform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆