方法一.利用cv2.warpAffine()方法实现图片的位移
源码:
import cv2
import numpy as np
img = cv2.imread('image0.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
imgShift = np.float32([[1,0,100],[0,1,200]])# [1,0,100]的意思是,宽右移距离100 [0,1,200]高下移200
dst = cv2.warpAffine(img,imgShift,(width,height))
cv2.imshow('img',dst)
cv2.waitKey(0)
方法二.利用源代码方式实现图片的位移
源码:
import cv2
import numpy as np
img = cv2.imread('image0.jpg',1)
imgInfo = img.shape
dst = np.zeros(img.shape,np.uint8)
height = imgInfo[0]
width = imgInfo[1]
for i in range(height-200):# 高下移200
for j in range(width-100):# 宽右移100
dst[i+200,j+100] = img[i,j]
cv2.imshow('img',dst)
cv2.waitKey(0)
两种方法的效果一样,效果图如下