opencv中进行图像旋转的方法 flip函数 rotate函数但是这两种函数只能进行90 180 270的旋转
CV_EXPORTS_W void flip(InputArray src, OutputArray dst, int flipCode);
enum RotateFlags {
ROTATE_90_CLOCKWISE = 0, //!<Rotate 90 degrees clockwise顺时针旋转90
ROTATE_180 = 1, //!<Rotate 180 degrees clockwise 旋转180
ROTATE_90_COUNTERCLOCKWISE = 2, //!<Rotate 270 degrees clockwise旋转270度
};
CV_EXPORTS_W void rotate(InputArray src, OutputArray dst, int rotateCode);
代码如下
Mat dst;
flip(image, dst, 0); //flip 0 上下翻转 1左右翻转 -1 旋转180度对角线翻转
rotate(image, dst, ROTATE_180);
imshow("dst", dst);
另外一种旋转方式如下 可以旋转任意角度
Mat dst, M;
int w = image.cols;
int h = image.rows;
M = getRotationMatrix2D(Point2f(w / 2, h / 2), 45, 1.0); //原来图像的中心位置,角度,图像放大缩小多少 图像旋转
double cos1 = abs(M.at<double>(0, 0));
double sin1 = abs(M.at<double>(0, 1));
int nw = cos1 * w + sin1 * h;
int nh = sin1 * w + cos1 * h;
std::cout << M.at<double>(0, 2) << std::endl;
M.at<double>(0, 2) += (nw / 2 - w / 2); //代表中心点水平平移的距离 正数代表右平移
std::cout << M.at<double>(0, 2) << std::endl;
M.at<double>(1, 2) += (nh / 2 - h / 2); //代表中心点竖直平移的距离 正数代表向下平移
warpAffine(image, dst, M, Size(nw, nh), INTER_LINEAR, 0);
imshow("旋转演示", dst);
此处要注意 得到的m矩阵是一个2*3维的矩阵M矩阵如下
cos -sin 0
sin cos 0
同时在进行图像旋转后,调整完旋转后图像的大小图像的中心位置也发生偏移,需要进行调整