图像处理(十)旋转矩阵,缩放矩阵

矩阵的目的就是线性变换,线性变换分平移、旋转、缩放

cv2内置函数进行线性变换cv2.warpAffine(image,M,outimg.shape) 其中M是线性变换矩阵
旋转矩阵直接可以通过cv2.getRotationMatrix2D()生成,函数内部有3个参数,1、元祖:旋转中心点;2、旋转角度;3、缩放比例

平移矩阵:1 0 x0
0 1 y0
0 0 1

旋转矩阵 cos -sin
sin cos

缩放矩阵 m 0
0 n

代码实现:仅实现旋转和缩放

import numpy as np
import cv2

path = 'test.jpg'
image = cv2.imread(path,0)

def xuanzhuan(image):
	hight,width = image.shape
	img = np.zeros((int(hight*2),int(width*2)))
	theta = np.deg2rad(30)
	x_matrix = np.matrix([[np.cos(theta),-np.sin(theta)],[np.sin(theta),np.cos(theta)]])
	for i in range(hight):
		for j in range(width):
			old_box = np.matrix([[j],[i]])
			new_box = np.asarray(x_matrix*old_box)
			x = int(new_box[0][0])
			y = int(new_box[1][0])
			img[y+50,x+200] = image[i,j]
	return img

def resize_(image,scale):
	hight,width = image.shape
	img = np.zeros((hight*scale,width*scale))
	resize_matrix = np.matrix([[scale,0],[0,scale]])
	for i in range(hight):
		for j in range(width):
			old_box = np.matrix([[j],[i]])
			new_box = resize_matrix*old_box
			x = new_box[0][0]
			y = new_box[1][0]
			img[y,x] = image[i,j]

	return img

#img = xuanzhuan(image)
img = resize_(image,2)
img = img.astype(np.uint8)
cv2.imshow('test',img)
cv2.imwrite('resize.jpg',img)
cv2.waitKey(0)
cv2.destroyWindows()

效果图如下:
图像处理(十)旋转矩阵,缩放矩阵_第1张图片
图像处理(十)旋转矩阵,缩放矩阵_第2张图片

你可能感兴趣的:(图像处理)