使用opencv裁剪图片和移位

1.裁剪
# x轴   100 --200
# y轴   100 -- 300
import cv2
img = cv2.imread("图片的位置",0或者1)
imgInfo = img.shape
dst = img[100:200,100:300]
cv2.imshow('image',dst)
cv2.waitKey(0)



2.# 步骤    API     算法原理       源代码
import  cv2
img = imread("图片的位置",0或1)
cv2.imshow('src',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]

matShift = np.float([[1,0,100],[0,1,200]])   # 里面是一个2*3的矩阵
dst = cv2.warpAffine(img,matShift,(height,width)) # 参数:1.img原始图片的信息   2.matShift移位矩阵    3.  图片的宽高

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
    [[x],[y]]   C
    A*C+B = [[1*x+0*y],[0*x+1*y]] + [[100],[200]]
          = [[x+100],[y+200]]


import  cv2
import numpy as np
img = imread("图片的位置",0或1)
cv2.imshow('src',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dst = np.zeros(img.shap,np.uint8)
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)

猜你喜欢

转载自blog.csdn.net/qq_15256443/article/details/84065016