关于图片的几种变换:倾斜、平移、翻转、旋转、放大放小

 

倾斜:  

    Mat img = imread("1.png");
	Mat dst, dst2, dst3, dst4;

	Point2f imgPoints[3];
	Point2f dstPoints[3];
	//cout <<dstPoints[3];      //倾斜
	imgPoints[0] = Point2f(0, 0); //0,0
	imgPoints[1] = Point2f(0, img.rows);//0,375
	imgPoints[2] = Point2f(img.cols, 0);//585,0
	//cout << imgPoints[2];
	dstPoints[0] = Point2f(0, img.rows*0.3); //[0, 112.2]
	dstPoints[1] = Point2f(img.cols*0.25, img.rows*0.75);//[146.25, 280.5]
	dstPoints[2] = Point2f(img.cols*0.75, img.rows*0.25); //[438.75, 93.5]
	cout << dstPoints[0];

	Mat M1 = getAffineTransform(imgPoints, dstPoints);  //由三个点对计算变换矩阵
	warpAffine(img, dst, M1, img.size());//倾斜
	imshow("原图", img);
	imshow("修改", dst);
	waitKey(0);

   效果:

 

 

                                            

 

平移:

    imgPoints[0] = Point2f(0, 0); //0,0   //平移
	imgPoints[1] = Point2f(0, img.rows);//0,375
	imgPoints[2] = Point2f(img.cols, 0);//585,0

	dstPoints[0] = Point2i(img.cols / 3, 0);
	dstPoints[1] = Point2i(img.cols / 3, img.rows);
	dstPoints[2] = Point2i(img.cols + img.cols / 3, 0);
	cout << dstPoints[2];
	Mat M3 = getAffineTransform(imgPoints, dstPoints);  //由三个点对计算变换矩阵
	//warpAffine(img, dst, M1, img.size());   //倾斜
	warpAffine(img, dst, M3, Size(img.cols + img.cols / 3, img.rows));
	imshow("原图", img);
	imshow("修改", dst);
	waitKey(0);

   效果:

                                          

旋转: 

    Point center(img.cols / 2, img.rows / 2);//旋转中心
	//Point center(img.rows / 3, img.cols / 3);//旋转中心
	double angle = 45;//逆时针旋转45度    正数逆时针负数顺时针
	double scale = 1;//缩放比例 
	Mat M2 = getRotationMatrix2D(center, angle, scale);
	warpAffine(img, dst2, M2, img.size());
	imshow("原图", img);
	imshow("修改", dst2);
	waitKey(0);

效果:

 

                                          

翻转:

	Point2f imgPoints2[3];   //翻转
	Point2f stdPoints2[3];
	imgPoints2[0] = Point2i(0, 0);
	imgPoints2[1] = Point2i(0, img.rows);
	imgPoints2[2] = Point2i(img.cols, 0);

	stdPoints2[0] = Point2i(img.cols, 0);
	stdPoints2[1] = Point2i(img.cols, img.rows);
	stdPoints2[2] = Point2i(0, 0);

	Mat M4 = getAffineTransform(imgPoints2, stdPoints2);
	warpAffine(img, dst3, M4, Size(img.cols, img.rows));
	imshow("原图", img);
	imshow("修改", dst3);
	waitKey(0);

效果:

 

 

                                                      

放大放小:

	//放大放小
	float p = 1.5; //缩小放大多少倍
	Mat src2(Size(int(img.cols*p), int(img.rows*p)), img.type(), Scalar(0));

	for (int i = 0; i < src2.rows; i++)
	{
		for (int j = 0; j < src2.cols; j++)
		{ 
			//if ((0 <= int(i / p) && int(i / p) < img.rows) && (0 <= int(j / p) && int(j / p) < img.cols)) {
				src2.at<Vec3b>(i, j) = img.at<Vec3b>(int(i / p), int(j / p));

			//}
		}
	}
	imshow("img", img);
	imshow("放大缩小", src2);
	waitKey(0);

效果:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值