我有一个问题cv2.warpAffine。我想使用cv2.INTER_AREA插值法。cv2.INTER_LINEAR但它的工作原理与我将图像大小从 3kx3k 调整为 100x100 时的效果相同。在文档中有一个类似插值类型的标志,但它不起作用=(
# create random image
image = np.random.randint(0, 255, (3000, 3500, 3)).astype(np.uint8)
pts1 = np.float32([[0,0],[3000, 3500],[0,3500]])
t_size = 100
pts2 = np.float32([[5,12],[t_size + 20,t_size + 10],[0,t_size-50]])
# calculate transformation matrix
M = cv2.getAffineTransform(pts1,pts2)
# warp with different flags
image_linear = cv2.warpAffine(image, M, (t_size, t_size), flags = cv2.INTER_LINEAR)
image_area = cv2.warpAffine(image, M, (t_size, t_size), flags = cv2.INTER_AREA)
assert((image_linear == image_area).all())
Run Code Online (Sandbox Code Playgroud)
和image是平等的,assert也不例外。
我在 opencv C++ 代码中找到了答案。
warpAffine 函数中有下一行代码
if( interpolation == INTER_AREA )
interpolation = INTER_LINEAR;
Run Code Online (Sandbox Code Playgroud)
对于我来说,我将仿射变换拆分为旋转、移动和调整大小。并使用旋转和移位与线性插值。并通过interpolation= cv2.INTER_AREA插值调整大小。