从 OpenCV 中的旋转图像旋转回点

Rotating back points from a rotated image in OpenCV(从 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 中的旋转图像旋转回点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
STL BigInt class implementation(STL BigInt 类实现)
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)