图像翻转和旋转

翻转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;
}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值