OpenCv图像处理之resize(缩放)、transpose、rotate(旋转)、flip(翻转)介绍

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轴为对称轴
在这里插入图片描述

 

posted @ 2022-03-30 17:03  量子与太极  阅读(1564)  评论(0编辑  收藏  举报