opencv warpAffine 忽略 flags=cv2.INTER_AREA

Кон*_*ков 3 python opencv

我有一个问题cv2.warpAffine。我想使用cv2.INTER_AREA插值法。cv2.INTER_LINEAR但它的工作原理与我将图像大小从 3kx3k 调整为 100x100 时的效果相同。在文档中有一个类似插值类型的标志,但它不起作用=(

https://docs.opencv.org/3.1.0/da/d54/group__imgproc__transform.html#ga0203d9ee5fcd28d40dbc4a1ea4451983

# 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也不例外。

Кон*_*ков 5

我在 opencv C++ 代码中找到了答案。

https://github.com/opencv/opencv/blob/174b4ce29d8e1ddbd899095c4b9fb4443444af45/modules/imgproc/src/imgwarp.cpp#L2600

warpAffine 函数中有下一行代码

if( interpolation == INTER_AREA )
         interpolation = INTER_LINEAR;
Run Code Online (Sandbox Code Playgroud)

对于我来说,我将仿射变换拆分为旋转、移动和调整大小。并使用旋转和移位与线性插值。并通过interpolation= cv2.INTER_AREA插值调整大小。