<p dir="auto">I'm using the following code to resize the label images of semantic segmentation task [PASCAL VOC] ( images with values in range 0 to <code class="notranslate">n_classes</code> with <code class="notranslate">n_classes</code> = 21).</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    lbl = m.imread(lbl_path)
    lbl = np.array(lbl, dtype=np.int32)
    lbl = self.encode_segmap(lbl)
    print 'Unique: ', np.unique(lbl)
    plt.imshow(lbl)
    plt.show()
    lbl = m.imresize(lbl, (self.img_size, self.img_size), 'nearest')
    print 'Unique after resize: ', np.unique(lbl)
    plt.imshow(lbl)
    plt.show()"><pre class="notranslate"><code class="notranslate">    lbl = m.imread(lbl_path)
    lbl = np.array(lbl, dtype=np.int32)
    lbl = self.encode_segmap(lbl)
    print 'Unique: ', np.unique(lbl)
    plt.imshow(lbl)
    plt.show()
    lbl = m.imresize(lbl, (self.img_size, self.img_size), 'nearest')
    print 'Unique after resize: ', np.unique(lbl)
    plt.imshow(lbl)
    plt.show()
</code></pre></div>
<p dir="auto">When I run this, I get this output:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Unique: [ 0  1 15] 
Unique after resize: [ 0  17 255]"><pre class="notranslate"><code class="notranslate">Unique: [ 0  1 15] 
Unique after resize: [ 0  17 255]
</code></pre></div>
<p dir="auto">Shouldn't the values after resize ( using <code class="notranslate">nearest</code>) only belong to the set ( 0, 1, 15 )?</p>
<p dir="auto">The images I get as output are :</p>
<p dir="auto">Original<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/6251729/24836419/be5704a2-1d38-11e7-94ef-2aab572c0201.png"><img src="https://cloud.githubusercontent.com/assets/6251729/24836419/be5704a2-1d38-11e7-94ef-2aab572c0201.png" alt="original" style="max-width: 100%;"></a></p>
<p dir="auto">Resized<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/6251729/24836420/be5b2fd2-1d38-11e7-93e8-3307d127c20c.png"><img src="https://cloud.githubusercontent.com/assets/6251729/24836420/be5b2fd2-1d38-11e7-93e8-3307d127c20c.png" alt="resized" style="max-width: 100%;"></a></p>
<p dir="auto">scipy.misc.imresize changes minimum, mean and maximum values of image. That is not what user can expect.</p>
<p dir="auto">Following code (EDIT: updated for removal of <code class="notranslate">lena</code>, so doesn't quite match the opencv version below):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import numpy as np
from scipy import misc

img = np.random.randint(25, 246, 400*400).reshape(400, 400)
print(img.min(), img.mean(), img.max())
img2 = misc.imresize(img.astype('uint8'), [1000, 1000])
print(img2.min(), img2.mean(), img2.max())
img2 = misc.imresize(img.astype('float32'), [1000, 1000])
print(img2.min(), img2.mean(), img2.max())"><pre class="notranslate"><code class="notranslate">import numpy as np
from scipy import misc

img = np.random.randint(25, 246, 400*400).reshape(400, 400)
print(img.min(), img.mean(), img.max())
img2 = misc.imresize(img.astype('uint8'), [1000, 1000])
print(img2.min(), img2.mean(), img2.max())
img2 = misc.imresize(img.astype('float32'), [1000, 1000])
print(img2.min(), img2.mean(), img2.max())
</code></pre></div>
<p dir="auto">produces following output:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(25, 134.83063125000001, 245)
(26, 134.33983000000001, 245)
(1, 126.824645, 255)"><pre class="notranslate"><code class="notranslate">(25, 134.83063125000001, 245)
(26, 134.33983000000001, 245)
(1, 126.824645, 255)
</code></pre></div>
<p dir="auto">The changes are very significant. cv2 (opencv) library has better situation:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import cv2
import scipy.misc
import numpy 

img = scipy.misc.ascent()
print(img.min(), img.mean(), img.max())
img2 = cv2.resize(img.astype('uint8'), dsize=(1000,1000))
print(img2.min(), img2.mean(), img2.max())
img2 = cv2.resize(img.astype('float32'), dsize=(1000,1000))
print(img2.min(), img2.mean(), img2.max())"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">cv2</span>
<span class="pl-k">import</span> <span class="pl-s1">scipy</span>.<span class="pl-s1">misc</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> 

<span class="pl-s1">img</span> <span class="pl-c1">=</span> <span class="pl-s1">scipy</span>.<span class="pl-s1">misc</span>.<span class="pl-en">ascent</span>()
<span class="pl-en">print</span>(<span class="pl-s1">img</span>.<span class="pl-en">min</span>(), <span class="pl-s1">img</span>.<span class="pl-en">mean</span>(), <span class="pl-s1">img</span>.<span class="pl-en">max</span>())
<span class="pl-s1">img2</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-en">resize</span>(<span class="pl-s1">img</span>.<span class="pl-en">astype</span>(<span class="pl-s">'uint8'</span>), <span class="pl-s1">dsize</span><span class="pl-c1">=</span>(<span class="pl-c1">1000</span>,<span class="pl-c1">1000</span>))
<span class="pl-en">print</span>(<span class="pl-s1">img2</span>.<span class="pl-en">min</span>(), <span class="pl-s1">img2</span>.<span class="pl-en">mean</span>(), <span class="pl-s1">img2</span>.<span class="pl-en">max</span>())
<span class="pl-s1">img2</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-en">resize</span>(<span class="pl-s1">img</span>.<span class="pl-en">astype</span>(<span class="pl-s">'float32'</span>), <span class="pl-s1">dsize</span><span class="pl-c1">=</span>(<span class="pl-c1">1000</span>,<span class="pl-c1">1000</span>))
<span class="pl-en">print</span>(<span class="pl-s1">img2</span>.<span class="pl-en">min</span>(), <span class="pl-s1">img2</span>.<span class="pl-en">mean</span>(), <span class="pl-s1">img2</span>.<span class="pl-en">max</span>())</pre></div>
<p dir="auto">Output:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="25 124.046783447 245
26 123.920154 244
25.8968 124.044 243.705"><pre class="notranslate"><code class="notranslate">25 124.046783447 245
26 123.920154 244
25.8968 124.044 243.705
</code></pre></div>
<p dir="auto">scipy.version.full_version, scipy.version.git_revision, scipy.version.release is ('0.14.0', 'f2ec91c4908f9d67b5445fbfacce7f47518b35d1', True)</p>