通过transpose和flip实现图像旋转90/180/270度
在fbc_cv庫(kù)中,提供了對(duì)圖像進(jìn)行任意角度旋轉(zhuǎn)的函數(shù)rotate,其實(shí)內(nèi)部也是調(diào)用了仿射變換函數(shù)warpAffine。如果圖像僅是進(jìn)行90度倍數(shù)的旋轉(zhuǎn),是沒(méi)有必要用warpAffine函數(shù)的。這里通過(guò)transpose和flip函數(shù)實(shí)現(xiàn)對(duì)圖像進(jìn)行順時(shí)針90度、180度、270度的旋轉(zhuǎn)。
用fbc::transpose、fbc::flip和cv::transpose、cv::flip實(shí)現(xiàn)的結(jié)果是完全一致的。
通過(guò)fbc_cv庫(kù)實(shí)現(xiàn)代碼如下:
#include "test_rotate90.hpp"
#include <opencv2/opencv.hpp>
#include <transpose.hpp>
#include <flip.hpp>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_<uchar, 3> mat1(height, width, matSrc.data);fbc::Mat_<uchar, 3> matTranspose(width, height);fbc::transpose(mat1, matTranspose);// clockwise rotation 90fbc::Mat_<uchar, 3> 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 180fbc::Mat_<uchar, 3> 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 270fbc::Mat_<uchar, 3> 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;
}
結(jié)果圖像如下:
原圖
順時(shí)針旋轉(zhuǎn)90度?
?順時(shí)針旋轉(zhuǎn)180度
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?順時(shí)針旋轉(zhuǎn)270度? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
GitHub:https://github.com/fengbingchun/OpenCV_Test
總結(jié)
以上是生活随笔為你收集整理的通过transpose和flip实现图像旋转90/180/270度的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: OpenCV代码提取:flip函数的实现
- 下一篇: C++11中rvalue referen