Cut and shift of OpenCV image (python)

1. Image cutting

Get the pixel matrix in the clipping range, such as:
assuming: the left vertex of the picture is the origin, the right is the x-axis, and the down is the y-axis img [0: 250, 200: 450] Get the point (200, 0) as the clip Cut the left vertex of the picture with a 250 × 250 pixel matrix. Assume: take the left vertex of the picture as the origin, the x-axis to the right, and the y-axis to the down \\ img[0:250,200:450] Get the point (200, 0) as the cut Cut the 250×250 pixel matrix of the left vertex of the pictureFalse provided : In FIG sheet left top point as the original point , to the right as the x -axis , the lower is the y -axisimg[0:250,200:. 4 . 5 0 ] is eligible to take point ( 2 0 0 ,0 ) to shear cut FIG sheet of a left top point 2 . 5 0×2 . 5 0 of the image pixel moment matrix

import cv2
img = cv2.imread('image.jpg', 1)
cv2.imshow('img', img)
imgInfo = img.shape
dst = img[0:250,200:450] # 剪切
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

The results are as follows:

Second, the image shift

1. API call implementation
import numpy as np
import cv2
img = cv2.imread('image.jpg', 1)
cv2.imshow('img', img)
imgInfo = img.shape
height = imgInfo[0] # 获取高
width = imgInfo[1] # 获取宽
matShift = np.float32([[1,0,50],[0,1,80]]) # 2×3矩阵
# 参数: 1 图片 2 移位矩阵matShift 3 尺寸info
dst = cv2.warpAffine(img, matShift, (width, height))
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

Implementation principle:
Split [[1, 0, 50], [0, 1, 80]] into 2 × 2 and 2 × 1 matrices [[1, 0], [0, 1]] 2 × 2 A [[50], [80]] 2 × 1 is set to B [[x], [y]] 2 × 1 is set to CA ∗ C + B = [[1 × x + 0 × y], [0 × x + 1 × y]] + [[50], [80]] = [[x + 50], [y + 80]] That is, move 50 px to the right on the x-axis and down on the y-axis Move 80 px to split [[1,0,50],[0,1,80]] into a matrix of 2\times2 and 2\times1 \\ [[1,0],[0,1]]\quad \quad 2\times2 \quad\quad set to A \\ [[50],[80]]\quad\quad 2\times1 \quad\quad set to B \\ [[x],[y]]\quad \quad 2\times1 \quad\quad set to C \\ A*C+B = [[1\times x+0\times y], [0\times x+1\times y]]+[[50] ,[80]]=[[x+50],[y+80]] \\ That is, move 50px to the right on the x-axis and move down 80px on the y-axisPut [ [ 1 ,0,50],[0,1,. 8 0 ] ] split points is 2×2 and 2×1 the moment matrix[[1,0],[0,1]]2×2Set to A[[50],[80]]2×1Set to B[[x],[ and ] ]2×1Set to CAC+B=[[1×x+0×and ] ,[0×x+1×and ] ]+[[50],[80]]=[[x+50],[ and+80]]That is , in the x -axis on the right- shift movement . 5 0 P x , the y -axis on the lower -shift movement . 8 0 P x
Shift Results:

According to the above principle, the same can be achieved scaled image (similar tothe recent clinical Interpolation Method):
Suppose A is: [[0.5, 0], [0, 0.5]], B is: 0, and C is: [[x], [y]] A ∗ C + B = [[0.5 x ], [0.5 y]] Suppose A is: [[0.5,0],[0,0.5]], B is: 0, C is: [[x],[y]] \\ A*C+ B = [[0.5x],[0.5y]]False set A is:[[0.5,0],[0,0.5]],Let B be:0,Let C be : [ [ x ] ,[ and ] ]AC+B=[[0.5x],( 0 . 5 y ] ]

import cv2
import numpy as np
img = cv2.imread('image.jpg', 1)
cv2.imshow('img', img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
matShift = np.float32([[0.5,0,0], [0,0.5,0]]) # 缩放比为0.5
dst = cv2.warpAffine(img, matShift, (round(width*0.5), round(height*0.5)))
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

The results are as follows:

2. Realize pixel position transformation directly through loop
import cv2
import numpy as np
img = cv2.imread('image.jpg', 1)
cv2.imshow('img', img)
imgInfo = img.shape
height = imgInfo[0] # 获取高
width = imgInfo[1] # 获取宽
h = 60 #在高方向上移动的像素
w = 100 # 在宽方向上移动的像素
dst = np.zeros((height, width,3), np.uint8) # 创建一个画板
for i in range(height-h):
    for j in range(width-w):
        dst[i+h][j+w] = img[i][j]
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

The image is moved 60px down and 100px to the right. The result is as follows:

Guess you like

Origin blog.csdn.net/xwmrqqq/article/details/108853818