翻转 | cv::flip(cv::InputArray src, cv::OutputArray dst, int flipCode) |
旋转 | void cv::warpAffine(cv::InputArray src, cv::OutputArray dst, cv::InputArray M, cv::Size dsize, int flags = 1, int borderMode = 0, const cv::Scalar &borderValue = cv::Scalar()) |
1.图像翻转
C++ void cv::flip(cv::InputArray src, cv::OutputArray dst, int flipCode)
flipCode:
0 : 垂直方向翻转;
1 : 水平方向翻转;
-1 :水平、垂直方向同时翻转
#include<opencv2/opencv.hpp>
#include<iostream>
#include<imgproc.hpp>
#include<cmath>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat image = imread("C:/Users/YY/Pictures/Saved Pictures/2.jpg");
imshow("原图", image);
Mat out;
flip(image, out, 0);//竖直翻转
imshow("竖直翻转", out);
flip(image, out, 1);//水平翻转
imshow("水平翻转", out);
flip(image, out, -1);//180翻转
imshow("180翻转", out);
waitKey(0);
destroyAllWindows();
return 0;
}
2.图片旋转
详情:warpAffine函数
C++ void cv::warpAffine(cv::InputArray src,
cv::OutputArray dst,
cv::InputArray M, 变换矩阵
cv::Size dsize, 输出图像大小
int flags = 1, 输出图像的插值方法
int borderMode = 0, 图像边界的处理方法
const cv::Scalar &borderValue = cv::Scalar()边界颜色设置
)
#include<opencv2/opencv.hpp>
#include<iostream>
#include<imgproc.hpp>
#include<cmath>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat image = imread("C:/Users/YY/Pictures/Saved Pictures/frose.jpg");
imshow("原图", image);
Mat out;
int w = image.cols;
int h = image.rows;
Mat M = getRotationMatrix2D(Point2f(w / 2, h / 2), 45, 1.0);
//图片大小不变
warpAffine(image, out, M, image.size(), 1, 0, Scalar(0, 0, 0));
imshow("result1",out);
//图片大小适应地改变
//M:[cos,sin,x;-sin,cos,y]
double cos = M.at<double>(0, 0);
double sin = M.at<double>(0, 1);
int nw = cos * w + sin * h;
int nh = sin * w + cos * h;
M.at<double>(0, 2) += abs((nw - w) / 2);
M.at<double>(1, 2) += abs((nh - h) / 2);
warpAffine(image, out, M, Size(nw,nh), 1, 0, Scalar(0, 0, 0));
imshow("result2", out);
waitKey(0);
destroyAllWindows();
return 0;
}