环境:Windows
python版本:3.6
安装anaconda,下载地址:https://www.anaconda.com/download/

32位电脑就选择32位,64位就选择64位。
安装之后,要下载OpenCV

接下来就是代码部分
# 1 API 2 算法原理 3 源代码
import cv2
import numpy as np
# 读取图片
img = cv2.imread('2.jpg',1)
cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
matShift = np.float32([[1,0,100],[0,1,200]]) # 2*3
dst = cv2.warpAffine(img,matShift,(height,width)) # 1 data 2 mat 3 info
cv2.imshow('dst',dst)
cv2.waitKey(0)
下面是用源代码实现图片的位移
import cv2
import numpy as np
img = cv2.imread('2.jpg',1)
cv2.imshow('src',img)
imgInfo = img.shape
dst = np.zeros(img.shape,np.uint8)
height = imgInfo[0]
width = imgInfo[1]
for i in range(0,height):
for j in range(0,width-100):
dst[i,j+100] = img[i,j]
cv2.imshow('image',dst)
cv2.waitKey(0)
最终的效果图如下

网友评论