Python 图像扩充之旋转、平移、缩放、裁剪
Python 图像扩充之旋转、平移、缩放、裁剪
旋转
def rotate_img(img, degrees):
'''
旋转图片
:param img: 原始图片
:param degrees: 旋转角度
:return:
'''
height, width = img.shape[:2]
# 旋转后的尺寸
heightNew = int(width * fabs(sin(radians(degrees))) + height * fabs(cos(radians(degrees))))
widthNew = int(height * fabs(sin(radians(degrees))) + width * fabs(cos(radians(degrees))))
# 这里的第一个参数为旋转中心,第二个为旋转角度,第三个为旋转后的缩放因子
# 可以通过设置旋转中心,缩放因子,以及窗口大小来防止旋转后超出边界的问题
matRotation = cv2.getRotationMatrix2D((width / 2, height / 2), degrees, 1)
matRotation[0, 2] += (widthNew - width) / 2
matRotation[1, 2] += (heightNew - height) / 2
resultImg = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))
return resultImg
if __name__ == '__main__':
img = cv2.imread('D:/test/rabbit.jpg')
resultImg = rotate_img(img, 45)
cv2.imshow('origin', img)
cv2.imshow('result', resultImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
原图:
旋转后:
平移
def pan_img(img, x, y):
'''
图片平移
:param img: 原始图片
:param x: 横向移动像素值
:param y: 纵向移动像素值
:return:
'''
M = np.float32([[1, 0, x], [0, 1, y]])
resultImg = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
return resultImg
if __name__ == '__main__':
img = cv2.imread('D:/test/rabbit.jpg')
resultImg = tools.pan_img(img, -50, -50)
cv2.imshow('origin', img)
cv2.imshow('result', resultImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
平移后:
缩放
def resize_proportion(img, h_zoom, w_zoom):
'''
缩放
:param img: 原始图片
:param h_zoom: 纵向比例
:param w_zoom: 横向比例
:return:
'''
height, width = img.shape[:2]
resultImg = cv2.resize(img, (int(width * w_zoom), int(height * h_zoom)))
return resultImg
if __name__ == '__main__':
img = cv2.imread('D:/test/rabbit.jpg')
resultImg = tools.resize_proportion(img, 1, 0.5)
cv2.imshow('origin', img)
cv2.imshow('result', resultImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
缩放后:
裁剪
def cropped_center(img, x1, y1, x2, y2):
'''
裁剪
:param img: 原始图片
:param x1: 左边界
:param y1: 上边界
:param x2: 右边界
:param y2: 下边界
:return:
'''
resultImg = img[y1:y2, x1:x2]
return resultImg
if __name__ == '__main__':
img = cv2.imread('D:/test/rabbit.jpg')
resultImg = tools.cropped_center(img, 20, 20, 400, 400)
cv2.imshow('origin', img)
cv2.imshow('result', resultImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
裁剪后:
Python 图像扩充之旋转、平移、缩放、裁剪相关教程