在fbc_cv库中,提供了对图像进行任意角度旋转的函数rotate,其实内部也是调用了仿射变换函数warpAffine。如果图像仅是进行90度倍数的旋转,是没有必要用warpAffine函数的。这里通过transpose和flip函数实现对图像进行顺时针90度、180度、270度的旋转。
用fbc::transpose、fbc::flip和cv::transpose、cv::flip实现的结果是完全一致的。
通过fbc_cv库实现代码如下:
#include "test_rotate90.hpp" #include #include #include int test_rotate90() { cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/1.jpg", 1); if (!matSrc.data) { std::cout << "read image fail" << std::endl; return -1; } int width = matSrc.cols; int height = matSrc.rows; fbc::Mat_ mat1(height, width, matSrc.data); fbc::Mat_ matTranspose(width, height); fbc::transpose(mat1, matTranspose); // clockwise rotation 90 fbc::Mat_ matRotate90(width, height); fbc::flip(matTranspose, matRotate90, 1); cv::Mat tmp2(width, height, CV_8UC3, matRotate90.data); cv::imwrite("E:/GitCode/OpenCV_Test/test_images/rotate_90.jpg", tmp2); // clockwise rotation 180 fbc::Mat_ matRotate180(height, width); fbc::flip(mat1, matRotate180, -1); cv::Mat tmp3(height, width, CV_8UC3, matRotate180.data); cv::imwrite("E:/GitCode/OpenCV_Test/test_images/rotate_180.jpg", tmp3); // clockwise rotation 270 fbc::Mat_ matRotate270(width, height); fbc::flip(matTranspose, matRotate270, 0); cv::Mat tmp4(matTranspose.rows, matTranspose.cols, CV_8UC3, matRotate270.data); cv::imwrite("E:/GitCode/OpenCV_Test/test_images/rotate_270.jpg", tmp4); return 0; }
结果图像如下:
原图
顺时针旋转90度
顺时针旋转180度
顺时针旋转270度