DSP

OpenCV 图像处理 02 图像剪切和位移

2019-07-13 16:53发布

#100-> 200 x #100-> 300 y import cv2 img = cv2.imread('image0.jpg',1) imgInfo =img.shape dst = img[100:200,100:300] cv2.imshow('image',dst) cv2.waitKey(0)    方法一: # 1 API 2算法原理 3源代码来实现 import cv2 import numpy as np img = cv2.imread('image0.jpg',1) cv2.imshow('src',img) imgInfo = img.shape height = imgInfo[0] width = imgInfo[1] matShift = np.float32([[1,0,100],[0,1,200]]) dst = cv2.warpAffine(img,matShift,(height,width))# 1 data 2 mat 3 info #移位 矩阵 cv2.imshow('dst',dst) cv2.waitKey(0) # [1,0,100],[0,1,200] 2*2 2*1
# [[1,0],[0,1]] 2*2 A
#[[100],[200]] 2*1  B
#xy c
#A*C+B = [1*X + 0*Y],[0*X+1*Y]+[[100],[200]]
# = [[X+100],[Y+200]]
 方法2: import cv2 import numpy as np img = cv2.imread('image0.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)