opencv中并没有直接封装图像旋转任意角度的函数,一般我们可以使用仿射变换获得旋转后的图像,这时候可以进行任意角度的旋转,但是如果我们需要将图像旋转90度,例如只是对图像进行左右翻转,或者旋转90度将图像放倒,那么如果还使用仿射变换,显得有些不是很简单,有点过于复杂。
实际上可以使用求转置矩阵的方式将图像旋转90度,然后可以沿着指定的坐标轴对旋转后的图像进行翻转变化。
使用transpose(src, dst);对目标图像进行转置变换,可以将垂直的图像变为水平放置。
然后使用flip()函数对图像进行翻转。
整个过程非常简单,可以看下下面的代码就非常清晰的了解了。
// ImageFlip.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "opencv/cv.h" #include "opencv/highgui.h" #include "stdio.h" #include "iostream" using namespace cv; using namespace std; int _tmain(int argc, _TCHAR* argv[]) { Mat src = imread("lena.jpg"); Mat dst; transpose(src, dst); Mat dst2; flip(dst, dst2, 1); // flip by y axis Mat dst3; flip(dst, dst3, 0); // flip by x axis Mat dst4; flip(dst, dst4, -1); // flip by both axises imshow("src", src); imshow("dst", dst); imshow("dst2", dst2); imshow("dst3", dst3); imshow("dst4", dst4); cvWaitKey(); return 0; }
原始图像:
转置以后:
flip(dst, dst2, 1); // flip by y axis