通过api、算法原理和源码解释图像移位:
- api实现
# api 算法 源码
import cv2
import numpy as np
img = cv2.imread('face.jpg', 1)
cv2.imshow('src', img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
# 移位矩阵 水平(width)移动100像素 竖直y(height)方向移动200像素
matShift = np.float32([[1, 0, 100], [0, 1, 200]]) # 2*3
print('hello')
# 放射变换之wrapAffine
dst = cv2.warpAffine(img, matShift, (height, width))
cv2.imshow('dst', dst)
cv2.waitKey(0)
结果如下:
image.png
-
算法原理
23 -> 22 and 21
[1, 0] [0, 1] A
[[100], [200]] B
xy C
AC+B = [[x * 1 + 0 * y], [0 * x + 1 * y]] + [[100], [200]]
= [[x +100], [y + 200]]
(10, 20) -> (110, 220) -
源码实现
import cv2
import numpy as np
img = cv2.imread('face.jpg', 1)
cv2.imshow('src', img)
imgInfo = img.shape
dst = np.zeros((img.shape), np.uint8)
height = imgInfo[0]
width = imgInfo[1]
# 水平右移100像素 竖直下移200像素
for i in range(0, height - 200):
for j in range(0, width - 100):
dst[i + 200, j + 100] = img[i, j]
cv2.imshow('dst', dst)
cv2.waitKey(0)
结果如下:
image.png
网友评论