通過transpose和flip實現影象旋轉90/180/270度
阿新 • • 發佈:2018-11-15
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
在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 90 fbc::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 180 fbc::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 270 fbc::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