Rotating back points from a rotated image in OpenCV(从OpenCV中的旋转图像旋转回点)
问题描述
我遇到了轮换问题.我想做的是这样的:
I’m having troubles with rotation. What I want to do is this:
- 旋转图片
- 检测旋转图像上的特征(点)
- 将点向后旋转,这样我就可以得到与初始图像相对应的点坐标
我在第三步有点卡住了.
I’m a bit stuck on the third step.
我设法使用以下代码旋转了图像:
I manage to rotated the image with the following code:
cv::Mat M(2, 3, CV_32FC1);
cv::Point2f center((float)dst_img.rows / 2.0f, (float)dst_img.cols / 2.0f);
M = cv::getRotationMatrix2D(center, rotateAngle, 1.0);
cv::warpAffine(dst_img, rotated, M, cv::Size(rotated.cols, rotated.rows));
我尝试使用以下代码旋转点:
I try to rotate back the points with this code:
float xp = r.x * std::cos( PI * (-rotateAngle) / 180 ) - r.y * sin(PI * (rotateAngle) / 180);
float yp = r.x * sin(PI * (-rotateAngle) / 180) + r.y * cos(PI * (rotateAngle) / 180);
工作不正常,但图像上的点不能很好地恢复.有一个偏移量.
It is not to fare to be working but the points don’t go back well on the image. There is an offset.
感谢您的帮助
推荐答案
如果 M 是你从 cv::getRotationMatrix2D 得到的旋转矩阵,来旋转一个 cv::Point p 使用这个矩阵,你可以这样做:
If M is the rotation matrix you get from cv::getRotationMatrix2D, to rotate a cv::Point p with this matrix you can do this:
cv::Point result;
result.x = M.at<double>(0,0)*p.x + M.at<double>(0,1)*p.y + M.at<double>(0,2);
result.y = M.at<double>(1,0)*p.x + M.at<double>(1,1)*p.y + M.at<double>(1,2);
如果要旋转一个点,生成M的逆矩阵或者使用cv::getRotationMatrix2D(center, -rotateAngle, scale)生成矩阵用于反向旋转.
If you want to rotate a point back, generate the inverse matrix of M or use cv::getRotationMatrix2D(center, -rotateAngle, scale) to generate a matrix for reverse rotation.
这篇关于从OpenCV中的旋转图像旋转回点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从OpenCV中的旋转图像旋转回点
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
