通过transpose和flip实现图像旋转90/180/270度
生活随笔
收集整理的這篇文章主要介紹了
通过transpose和flip实现图像旋转90/180/270度
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在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 <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;
}
結果圖像如下:
原圖
順時針旋轉90度?
?順時針旋轉180度
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?順時針旋轉270度? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
GitHub:https://github.com/fengbingchun/OpenCV_Test
總結
以上是生活随笔為你收集整理的通过transpose和flip实现图像旋转90/180/270度的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV代码提取:flip函数的实现
- 下一篇: C++11中rvalue referen