Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opencv warpAffine ignores flags=cv2.INTER_AREA

Tags:

python

opencv

I have a problem with cv2.warpAffine. I want to use cv2.INTER_AREA interpolation. But it works the same as cv2.INTER_LINEAR when I resize image from 3kx3k to 100x100. In docs there is a flag like interpolation type, but it doesn't work=(

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())

And image is equal, there isn't any exception in assert.

like image 990
Константин Гудков Avatar asked Oct 14 '25 09:10

Константин Гудков


1 Answers

I found the answer in opencv C++ code.

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

There is next lines of code in warpAffine function

if( interpolation == INTER_AREA )
         interpolation = INTER_LINEAR;

As for me I split affine transform to rotate, shift and resize. And use rotate and shift with linear interpolation. And resize with interpolation= cv2.INTER_AREA interpolation.

like image 120
Константин Гудков Avatar answered Oct 16 '25 21:10

Константин Гудков



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!