import cv2
import numpy as np
img1 = cv2.imread('20.jpg')
# 移动
def move():
H = np.float32([[1, 0, 50], [0, 1, 25]]) # [1, 0, 50]表示X轴移动50,[0, 1, 25]表示Y轴移动25
img2 = img1.copy()
row, col = img2.shape[:2] # 快速读取三维数组前俩个数组的长度
print(img2.shape)
print(row, col)
print(H)
new = cv2.warpAffine(img2, H, (col, row)) # 变换原始图像矩阵(移动)
cv2.imshow('window1', img2)
cv2.imshow('window2', new)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 缩放
def change_size():
img3 = img1.copy()
# 缩放有几种不同的插值(interpolation)方法,在缩小时推荐使用cv2.INTER_AREA,扩大时推荐使用cv2.INTER_CUBIC和cv2.INTER_LINEAR
# 一是通过设置图像缩放比例,即缩放因子,来对图像进行放大或缩小
res1 = cv2.resize(img3, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
height, width = img3.shape[:2]
# 二是直接设置图像的大小,不需要缩放因子
res2 = cv2.resize(img3, (int(0.8 * width), int(0.8 * height)), interpolation=cv2.INTER_AREA)
cv2.imshow('origin_picture', img3)
cv2.imshow('res1', res1)
cv2.imshow('res2', res2)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 旋转
def rotate():
img = cv2.imread('4.jpg')
rows, cols = img.shape[:2]
# 第一个参数是旋转中心,第二个参数是旋转角度,第三个参数是缩放比例
M1 = cv2.getRotationMatrix2D((cols/2, rows/2), 45, 0.5)
M2 = cv2.getRotationMatrix2D((cols/2, rows/2), 45, 2)
M3 = cv2.getRotationMatrix2D((cols/2, rows/2), 45, 1)
res1 = cv2.warpAffine(img, M1, (cols, rows))
res2 = cv2.warpAffine(img, M2, (cols, rows))
res3 = cv2.warpAffine(img, M3, (cols, rows))
cv2.imshow('res1', res1)
cv2.imshow('res2', res2)
cv2.imshow('res3', res3)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 仿射
def affine():
img = cv2.imread('4.jpg')
rows, cols = img.shape[:2]
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
# 类似于构造矩阵
M = cv2.getAffineTransform(pts1, pts2)
res = cv2.warpAffine(img, M, (cols, rows))
cv2.imshow('原图', img)
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 透射
def transmission():
img = cv2.imread('4.jpg')
rows, cols = img.shape[:2]
pts1 = np.float32([[56, 65], [238, 52], [28, 237], [239, 240]])
pts2 = np.float32([[0, 0], [200, 0], [0, 200], [200, 200]])
M = cv2.getPerspectiveTransform(pts1, pts2)
res = cv2.warpPerspective(img, M, (cols, rows))
cv2.imshow('yuantu', img)
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
图像移动
最新推荐文章于 2020-04-13 15:47:07 发布