数字图像处理的python实践(4)——几何变换之平移、镜像、转置

  • Post author:
  • Post category:python


几何变换是图像处理中的重要模块,标题中所涉及的内容都是很简单的,不多说明直接上代码了。首先是平移,因为像素点都是离散的,如果出现不是整数的平移矢量,需要进行一些其他的判断,我们暂时不讨论这种比较复杂的情况,在之后的缩放中再看当处理后的图像的点在原图中的对应位置并不落在像素点上的处理方法。

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

def Move(src, target, vector = (0,0), expand = False):
	'''
	vector:move vector.Its componment should be integer.
	'''
	im = Image.open(src)
	imarray = np.array(im)
	height, width = imarray.shape
	vx, vy = vector
	
	if not expand:
		new_arr = np.zeros((height, width), dtype = 'uint8')
		for i in range(height):
			for j in range(width):
			 if i - vx >= 0 and i - vx < height and j -vy >= 0 and j - vy < width:
			 	new_arr[i, j] = imarray[i - vx, j - vy]
			 else:
			 	new_arr[i, j] = 255
	else:
		new_arr = np.ones((height + abs(vx), width + abs(vy)), dtype = "uint8")*255
		for i in range(relu(vx), height + relu(vx)):
			for j in range(relu(vy), width + relu(vy)):
			 	new_arr[i, j] = imarray[i - relu(vx), j - relu(vy)]

	new_im = Image.fromarray(new_arr)
	new_im.save(target)

def relu(x):
	if x >= 0:
		return x
	else:
		return 0

gray_girl = "C:/Users/60214/Desktop/python_work/DigitalExecution/gray_girl.jpg"
tar = "



版权声明:本文为baidu_41902768原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。