OpenCv圖像處理之resize(縮放)、transpose、rotate(旋轉)、flip(翻轉)介紹:
OpenCv圖像處理之resize、transpose、rotate、flip介紹
cv::resize操作
cv::transpose操作
cv::rotate操作
cv::flip操作
cv::resize操作
縮放是處理圖像中經常用到的方法,opencv中也專門封裝了此類函數,就是cv::resize。
CV_EXPORTS_W void resize(InputArray src, OutputArray dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR);
InputArray src輸入圖像cv::Mat類型
OutputArray dst輸出圖像cv::Mat類型
Size dsize縮放后的尺寸
double fx = 0x縮放比例,默認為0
double fy = 0y縮放比例,默認為0
int interpolation = INTER_LINEAR插值法,默認為雙線性插值
再來看一個例子:
#include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { Mat frame, clone_frame; frame = imread("D:/cat.jpg", 3); clone_frame = frame.clone(); cout << "front of the scale image.rows:" << clone_frame.rows << " " << "image.cols:" << clone_frame.cols << endl; double scale = 0.5; //放縮會導致圖像失真,縮小時interpolation使用INTER_AREA,放大時使用INTER_LINEAR減少失真 resize(clone_frame, clone_frame, Size(int(frame.cols * scale), int(frame.rows * 0.5)), 0, 0, INTER_AREA); cout << "behind of the scale image.rows:" << clone_frame.rows << " " << "image.cols:" << clone_frame.cols << endl; imshow("resize_img", clone_frame); waitKey(0); return 0; }
front of the scale image.rows:745 image.cols:746 behind of the scale image.rows:372 image.cols:373
效果顯示
注意使用resize進行縮放的時候,會出現圖像失真問題,插值法有利於減少失真。
常見插值法
1 #include <iostream> 2 #include "opencv2/opencv.hpp" 3 4 using namespace std; 5 using namespace cv; 6 7 8 int main() { 9 Mat frame, clone_frame; 10 frame = imread("D:/cat.jpg", 3); 11 clone_frame = frame.clone(); 12 double scale = 0.5; 13 resize(clone_frame, clone_frame, Size(int(frame.cols * scale), 14 int(frame.rows * scale)), 15 0, 0, INTER_AREA); 16 transpose(clone_frame, clone_frame); 17 imshow("transpose_frame", clone_frame); 18 waitKey(0); 19 return 0; 20 }
cv::rotate操作
先來看一下源碼種rotate的函數原型
1 CV_EXPORTS_W void rotate(InputArray src, OutputArray dst, int rotateCode); 2 enum RotateFlags { 3 ROTATE_90_CLOCKWISE = 0, //!<Rotate 90 degrees clockwise 4 ROTATE_180 = 1, //!<Rotate 180 degrees clockwise 5 ROTATE_90_COUNTERCLOCKWISE = 2, //!<Rotate 270 degrees clockwise 6 };
源碼描述:
1 /** @brief Rotates a 2D array in multiples of 90 degrees. 2 The function cv::rotate rotates the array in one of three different ways: 3 * Rotate by 90 degrees clockwise (rotateCode = ROTATE_90_CLOCKWISE). 4 * Rotate by 180 degrees clockwise (rotateCode = ROTATE_180). 5 * Rotate by 270 degrees clockwise (rotateCode = ROTATE_90_COUNTERCLOCKWISE). 6 @param src input array. 7 @param dst output array of the same type as src. The size is the same with ROTATE_180, 8 and the rows and cols are switched for ROTATE_90_CLOCKWISE and ROTATE_90_COUNTERCLOCKWISE. 9 @param rotateCode an enum to specify how to rotate the array; see the enum #RotateFlags 10 @sa transpose , repeat , completeSymm, flip, RotateFlags 11 */
上面這段話大概意思就是rotate函數能夠按照順時針的方法以三種不同的角度進行旋轉,三種角度分別是90度,180度,270度。rotateCode參數可以取枚舉類型RotateFlags中的值,0表示90度,1表示180度,2表示270度
接下來我們使用cv::rotate來演示一下順時針旋轉180度的操作:
1 #include <iostream> 2 #include "opencv2/opencv.hpp" 3 4 using namespace std; 5 using namespace cv; 6 7 8 int main() { 9 Mat frame, clone_frame; 10 frame = imread("D:/cat.jpg", 3); 11 clone_frame = frame.clone(); 12 double scale = 0.5; 13 resize(clone_frame, clone_frame, Size(int(frame.cols * scale), 14 int(frame.rows * scale)), 15 0, 0, INTER_AREA); 16 cv::rotate(clone_frame, clone_frame, 1); 17 imshow("rotate_180_frame", clone_frame); 18 waitKey(0); 19 return 0; 20 }
效果顯示
1 #include <iostream> 2 #include "opencv2/opencv.hpp" 3 4 using namespace std; 5 using namespace cv; 6 7 8 int main() { 9 Mat frame, clone_frame; 10 frame = imread("D:/cat.jpg", 3); 11 clone_frame = frame.clone(); 12 double scale = 0.5; 13 resize(clone_frame, clone_frame, Size(int(frame.cols * scale), 14 int(frame.rows * scale)), 15 0, 0, INTER_AREA); 16 //y 17 flip(clone_frame, clone_frame, 1); 18 //x,y 19 //flip(clone_frame,clone_frame,-1); 20 //x 21 //flip(clone_frame,clone_frame,0); 22 imshow("flip_y_frame", clone_frame); 23 waitKey(0); 24 return 0; 25 }
以y軸為對稱軸
分別以x軸,y軸為對稱軸
以x軸為對稱軸