opencv-图像的仿射变换和透视变换

仿射变换

/**
仿射变换:
	flip(src,dst,flipCode) # 图像翻转 flipCode = 0 : 垂直翻转  > 0 : 水平翻转  < 0 :同时垂直水平翻转
------------------------------------------------------------------------
	mapMatrix = getAffineTransform(src, dst) #取得图像仿射矩阵
	由已知的两个矩阵求解变换矩阵:src和dst只能为3行两列数组,类型只能为CV_64F/CV_32F
		Point2f src[]={Point2f(0,0),Point2f(200,0),Point2f(0,100)};
		Point2f dst[]={Point2f(0,0),Point2f(100,0),Point2f(0,50)};
		Mat trans = getAffineTransform(src,dst)
----------------------------------------------------------------------
	getRotationMatrix2D() 				  #取得旋转角度的Matrix
	warpAffine()						 #图像仿射
	dst = getRotationMatrix2D(center,Angle,Scale)
	instance:
		const int HEIGHT = img.rows;
		const int WIDTH = img.cols;
		m = getRotationMatrix2D(Point(WIDTH / 2, HEIGHT / 2), 45, 1.0);
		double cos = abs(m.at<double>(0, 0));
		double sin = abs(m.at<double>(0, 1));
		int newW = cos * WIDTH + sin * HEIGHT; // 旋转后的图像背景需要改变
		int newH = sin * WIDTH + cos * HEIGHT;
		m.at<double>(0, 2) += (newW / 2 - WIDTH / 2);
		m.at<double>(1, 2) += (newH / 2 - HEIGHT / 2);
		warpAffine(img, dst, m, Size(newW, newH), INTER_LINEAR, 0, Scalar(0, 0,122));
-------------------------------------------------------------------------------------
    图像的缩放:
		resize(src,dst,dSize,fx,fy,INTER_LINEAR);dsize是dst的尺寸,dSize与fx fy二者有线性计算关系
    图像变换的插值方式:
		INTER_NEAREST		最邻近插值点法
		INTER_LINEAR		双线性插值法
		INTER_AREA			临域像素再取样
		INTER_CUBIC			双立方插补
**/

透视变换

/**
	warpPerspective() #图像透视
	Mat getPerspectiveTransform(const Point2f src[], const Point2f dst[])
	取得图像透视的4个点起止值,返回值为图像透视变换矩阵
		import cv2
		import numpy as np
		# 读取名称为 p19.jpg的图片
		img = cv2.imread("p19.jpg",1)
		img_org = cv2.imread("p19.jpg",1)
		# 得到图片的高和宽
		img_height,img_width = img.shape[:2]
		# 定义对应的点
		points1 = np.float32([[75,55], [340,55], [33,435], [400,433]])
		points2 = np.float32([[0,0], [360,0], [0,420], [360,420]])
		# 计算得到转换矩阵
		M = cv2.getPerspectiveTransform(points1, points2)
		# 实现透视变换转换
		processed = cv2.warpPerspective(img,M,(360, 420))
**/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值