使用 OpenCV 将图像旋转 90 度的最简单方法?
将 IplImage/cv::Mat 旋转 90 度的最佳方法(在 c/c++ 中)是什么?我认为一定有比使用矩阵进行转换更好的东西,但我似乎在 API 和在线中找不到除此之外的任何东西。
What is the best way (in c/c++) to rotate an IplImage/cv::Mat by 90 degrees? I would assume that there must be something better than transforming it using a matrix, but I can't seem to find anything other than that in the API and online.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
从 OpenCV3.2 开始,生活变得更容易了,您现在可以用一行代码旋转图像:
对于方向,您可以选择以下任意一项:
As of OpenCV3.2, life just got a bit easier, you can now rotate an image in a single line of code:
For the direction you can choose any of the following:
旋转是转置和翻转的组合。
OpenCV 中可以这样写(Python 示例如下):
Rotation is a composition of a transpose and a flip.
Which in OpenCV can be written like this (Python example below):
这是我的 python
cv2
实现:Here is my python
cv2
implementation:换位更新:
您应该使用
cvTranspose()
或cv::transpose()
,因为(正如您正确指出的那样)它更有效。再次,我建议升级到 OpenCV2.0,因为大多数cvXXX
函数只是将IplImage*
结构转换为Mat
对象(无深拷贝)。如果您将图像存储在Mat
对象中,Mat.t()
将返回转置。任何旋转:
您应该通过在变换矩阵的总体框架中定义旋转矩阵来使用cvWarpAffine。我强烈建议升级到 OpenCV2.0,它具有多种功能以及
Mat< /code> 封装矩阵和图像的类。在 2.0 中,您可以对上述内容使用 warpAffine 。
Update for transposition:
You should use
cvTranspose()
orcv::transpose()
because (as you rightly pointed out) it's more efficient. Again, I recommend upgrading to OpenCV2.0 since most of thecvXXX
functions just convertIplImage*
structures toMat
objects (no deep copies). If you stored the image in aMat
object,Mat.t()
would return the transpose.Any rotation:
You should use cvWarpAffine by defining the rotation matrix in the general framework of the transformation matrix. I would highly recommend upgrading to OpenCV2.0 which has several features as well as a
Mat
class which encapsulates matrices and images. With 2.0 you can use warpAffine to the above.这是一个没有新 C++ 接口的示例(适用于 90、180 和 270 度,使用参数 = 1、2 和 3)。使用后记得在返回的图像上调用
cvReleaseImage
。This is an example without the new C++ interface (works for 90, 180 and 270 degrees, using param = 1, 2 and 3). Remember to call
cvReleaseImage
on the returned image after using it.这是我的 EmguCV(OpenCV 的 C# 端口)解决方案:
将其转换回 C++ 应该不会太难。
Here's my EmguCV (a C# port of OpenCV) solution:
Shouldn't be too hard to translate it back into C++.
嗯,我正在寻找一些细节,但没有找到任何例子。因此,我发布了一个
transposeImage
函数,我希望该函数能够帮助其他正在寻找直接旋转 90° 而不丢失数据的方法的人:问题:为什么会这样?
答案:因为它有效:) 我发现的唯一不翻译图像的中心。不过如果有人有解释我会很感兴趣。
Well I was looking for some details and didn't find any example. So I am posting a
transposeImage
function which, I hope, will help others who are looking for a direct way to rotate 90° without losing data:Question : Why this?
Answer : Because it works :) The only center I found that doesn't translate image. Though if somebody has an explanation I would be interested.