# INTER\_AREA unexpected result

**URL:** <https://forum.opencv.org/t/inter-area-unexpected-result/7186>\
**Category:** Python\
**Created:** [January 18, 2022, 2:50pm UTC](https://forum.opencv.org/t/inter-area-unexpected-result/7186 "2022-01-18T14:50:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![99991](https://avatars.discourse-cdn.com/v4/letter/9/a9adbd/32.png) [@99991](https://forum.opencv.org/u/99991)\
**Post date:** [January 18, 2022, 2:50pm UTC](https://forum.opencv.org/t/inter-area-unexpected-result/7186/1 "2022-01-18T14:50:56Z")

</div>

I believe that there is a bug in `cv2.resize` in conjunction with INTER\_AREA interpolation, but I wanted to ask here first just in case I missed something in the docs. My question is: Is this a bug or where is this behavior documented?

Resizing a 3x1 image to 1x2 should produce two pixels where the value of both is equal to the average of the 3 pixels, but that does not seem to be the case.

##### Example

- 3x1 input Image: `[[0.1, 0.2, 0.3]]`
- Resized to 1x1: `[[(0.1 + 0.2 + 0.3) / 3]] = [[0.2]]` (works correctly as expected)
- Expected result when resizing to 1x2:

```auto
[[0.2],
 [0.2]]

```

Actual result when resizing to 1x2:

```auto
[[0.16666667]
 [0.16666667]]

```

##### Code example

```python
import numpy as np
import cv2

src = np.float64([[0.1, 0.2, 0.3]])

print("Resized to 1x1:\n")
print(cv2.resize(src, (1, 1), interpolation=cv2.INTER_AREA))

print("\nResized to 1x2:\n")
print(cv2.resize(src, (1, 2), interpolation=cv2.INTER_AREA))

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 18, 2022, 3:26pm UTC](https://forum.opencv.org/t/inter-area-unexpected-result/7186/2 "2022-01-18T15:26:23Z")

</div>

> [@99991](#):
>
> Resizing a 3x1 image to 1x2 should produce two pixels where the value of both is equal to the average of the 3 pixels,

you squeeze a 1x3-shaped matrix (3 wide, 1 high) into a 1 wide 2 high matrix (2x1-shaped).

that is a weird thing to do, but okay.

both pixels of the result should be equal, but their value can be unexpected. I see now why you’d expect it to be 0.2. I actually would think that too.

be aware that opencv uses fixed point (integer) math. that can be (barely) noticeable in some instances.

maybe this helps with the details:

> <https://github.com/opencv/opencv/issues/9096#issuecomment-953656558>
>
> \##### System information (version)
> 
> \- OpenCV =\> 2.4.9
> \- Operating System / Pl…atform =\> Ubuntu 16.04
> \- Compiler =\> gcc 5.4.0
> 
> \##### Detailed description
> 
> Nearest neighbor interpolation using cv2.resize does not give expected results.
> 
> \##### Steps to reproduce
> \`\`\`
> import cv2
> import numpy as np
> 
> a = np.array(\[\[0, 1, 2, 3, 4\]\], dtype=np.uint8)
> print 'nearest', cv2.resize(a, dsize=(3, 1), interpolation=cv2.INTER\_NEAREST)
> \`\`\`
> gives
> \`\`\`
> nearest \[\[0 1 3\]\]
> \`\`\`
> expected
> \`\`\`
> nearest \[\[0 2 4\]\]
> \`\`\`

---

<div class="post-metadata">

**Author:** ![99991](https://avatars.discourse-cdn.com/v4/letter/9/a9adbd/32.png) [@99991](https://forum.opencv.org/u/99991)\
**Post date:** [January 18, 2022, 3:52pm UTC](https://forum.opencv.org/t/inter-area-unexpected-result/7186/3 "2022-01-18T15:52:11Z")

</div>

I’d expect that OpenCV would use the [floating point resize implementation](https://github.com/opencv/opencv/blob/d24befa0bc7ef5e73bf8b1402fa1facbdbf9febb/modules/imgproc/src/resize.cpp) since the input is not uint8 and conversion would be inefficient.

Anyway, this really seems to be a bug, so I’ve opened an issue now 🙂[INTER\_AREA unexpected result · Issue #21472 · opencv/opencv · GitHub](https://github.com/opencv/opencv/issues/21472)

---

<div class="post-metadata">

**Author:** ![Steve\_in\_Denver](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Steve\_in\_Denver](https://forum.opencv.org/u/Steve_in_Denver)\
**Post date:** [January 18, 2022, 3:58pm UTC](https://forum.opencv.org/t/inter-area-unexpected-result/7186/4 "2022-01-18T15:58:07Z")

</div>

I haven’t looked into it much at all, but reading this page:

> **[What is OpenCV’s INTER\_AREA Actually Doing?](https://medium.com/@wenrudong/what-is-opencvs-inter-area-actually-doing-282a626a09b3)**
>
> Recently I’m studying computer vision, and I came across the resize function in OpenCV. Resizing an image needs a way to calculate pixel…

(figure 3, specifically) would lead me to expect 0.133, 0.266 for the pixel values, but I might be misunderstanding something.
