<h5 dir="auto">System information (version)</h5>
<ul dir="auto">
<li>OpenCV =&gt; 4.5.4</li>
<li>Language=&gt; python/cpp</li>
<li>Operating System / Platform =&gt; Windows</li>
<li>Compiler =&gt; MSVC 19.32.31332.0<br>
I am trying to replicate PIL and scikit-image nearest-neighbor o/p in OpenCV but getting INTER_NEAREST o/p.</li>
</ul>
<ul dir="auto">
<li>
<p dir="auto">Original image:<br>
img<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/17100687/177693650-83d8d354-2aa7-4680-99f2-ce7206f0aacb.png"><img src="https://user-images.githubusercontent.com/17100687/177693650-83d8d354-2aa7-4680-99f2-ce7206f0aacb.png" alt="temp" style="max-width: 100%;"></a></p>
</li>
<li>
<p dir="auto">OpenCV INTER_NEAREST:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/17100687/177693674-5a3dfcef-6b28-45f3-a3b7-72248d2c756e.png"><img src="https://user-images.githubusercontent.com/17100687/177693674-5a3dfcef-6b28-45f3-a3b7-72248d2c756e.png" alt="temp1" style="max-width: 100%;"></a></p>
</li>
</ul>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content=" cv2.resize(x,(0,0),fx=0.5,fy=0.5,interpolation=cv2.INTER_NEAREST)"><pre class="notranslate"> <span class="pl-s1">cv2</span>.<span class="pl-en">resize</span>(<span class="pl-s1">x</span>,(<span class="pl-c1">0</span>,<span class="pl-c1">0</span>),<span class="pl-s1">fx</span><span class="pl-c1">=</span><span class="pl-c1">0.5</span>,<span class="pl-s1">fy</span><span class="pl-c1">=</span><span class="pl-c1">0.5</span>,<span class="pl-s1">interpolation</span><span class="pl-c1">=</span><span class="pl-s1">cv2</span>.<span class="pl-v">INTER_NEAREST</span>)</pre></div>
<ul dir="auto">
<li>OpenCV INTER_NEAREST_EXACT:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/17100687/177693731-f3121c7b-ff99-4efd-9cc6-981c5b595fa9.png"><img src="https://user-images.githubusercontent.com/17100687/177693731-f3121c7b-ff99-4efd-9cc6-981c5b595fa9.png" alt="temp2" style="max-width: 100%;"></a></li>
</ul>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content=" cv2.resize(x,(0,0),fx=0.5,fy=0.5,interpolation=cv2.INTER_NEAREST)"><pre class="notranslate"> <span class="pl-s1">cv2</span>.<span class="pl-en">resize</span>(<span class="pl-s1">x</span>,(<span class="pl-c1">0</span>,<span class="pl-c1">0</span>),<span class="pl-s1">fx</span><span class="pl-c1">=</span><span class="pl-c1">0.5</span>,<span class="pl-s1">fy</span><span class="pl-c1">=</span><span class="pl-c1">0.5</span>,<span class="pl-s1">interpolation</span><span class="pl-c1">=</span><span class="pl-s1">cv2</span>.<span class="pl-v">INTER_NEAREST</span>)</pre></div>
<ul dir="auto">
<li>PIL :<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/17100687/177693745-8897be90-3690-403a-be02-43d349dd9beb.png"><img src="https://user-images.githubusercontent.com/17100687/177693745-8897be90-3690-403a-be02-43d349dd9beb.png" alt="temp3" style="max-width: 100%;"></a></li>
</ul>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="x1=Image.fromarray(img)
x1.thumbnail((256,256),Image.Resampling.NEAREST) "><pre class="notranslate"><span class="pl-s1">x1</span><span class="pl-c1">=</span><span class="pl-v">Image</span>.<span class="pl-en">fromarray</span>(<span class="pl-s1">img</span>)
<span class="pl-s1">x1</span>.<span class="pl-en">thumbnail</span>((<span class="pl-c1">256</span>,<span class="pl-c1">256</span>),<span class="pl-v">Image</span>.<span class="pl-v">Resampling</span>.<span class="pl-v">NEAREST</span>) </pre></div>
<ul dir="auto">
<li>scikit-image :<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/17100687/177693760-fc64e5be-4215-4d2a-bf2e-c39beb35130a.png"><img src="https://user-images.githubusercontent.com/17100687/177693760-fc64e5be-4215-4d2a-bf2e-c39beb35130a.png" alt="temp5" style="max-width: 100%;"></a></li>
</ul>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="rescale(x,0.5,order=0,preserve_range=True, anti_aliasing=False) "><pre class="notranslate"><span class="pl-en">rescale</span>(<span class="pl-s1">x</span>,<span class="pl-c1">0.5</span>,<span class="pl-s1">order</span><span class="pl-c1">=</span><span class="pl-c1">0</span>,<span class="pl-s1">preserve_range</span><span class="pl-c1">=</span><span class="pl-c1">True</span>, <span class="pl-s1">anti_aliasing</span><span class="pl-c1">=</span><span class="pl-c1">False</span>) </pre></div>
<p dir="auto">As it can be seen, OpenCV INTER_NEAREST and OpenCV INTER_NEAREST_EXACT are matching, which is not expected.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/17100687/177694208-ae66b04d-2319-4e60-a4fb-2f06b1e1380b.png"><img src="https://user-images.githubusercontent.com/17100687/177694208-ae66b04d-2319-4e60-a4fb-2f06b1e1380b.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">The expected was that the OpenCV INTER_NEAREST_EXACT  should match PIL and scikit-image o/p, but I am getting this difference:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/17100687/177694183-90234e99-eb3b-4ce3-acb3-f7c3785d9cdc.png"><img src="https://user-images.githubusercontent.com/17100687/177694183-90234e99-eb3b-4ce3-acb3-f7c3785d9cdc.png" alt="image" style="max-width: 100%;"></a></p>
<h5 dir="auto">Issue submission checklist</h5>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I report the issue, it's not a question</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I checked the problem with documentation, FAQ, open issues,<br>
forum.opencv.org, Stack Overflow, etc and have not found any solution</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I updated to the latest OpenCV version and the issue is still there</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> There is reproducer code and related data files: videos, images, onnx, etc</li>
</ul>
<h5 dir="auto">System information (version)</h5>
<ul dir="auto">
<li>OpenCV =&gt; 2.4.9</li>
<li>Operating System / Platform =&gt; Ubuntu 16.04</li>
<li>Compiler =&gt; gcc 5.4.0</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">Nearest neighbor interpolation using cv2.resize does not give expected results.</p>
<h5 dir="auto">Steps to reproduce</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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)"><pre class="notranslate"><code class="notranslate">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)
</code></pre></div>
<p dir="auto">gives</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="nearest [[0 1 3]]"><pre class="notranslate"><code class="notranslate">nearest [[0 1 3]]
</code></pre></div>
<p dir="auto">expected</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="nearest [[0 2 4]]"><pre class="notranslate"><code class="notranslate">nearest [[0 2 4]]
</code></pre></div>