Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary image (np.int dtype) become all zero after resizing #5977

Closed
Guodonggogo opened this issue Oct 21, 2021 · 7 comments
Closed

Binary image (np.int dtype) become all zero after resizing #5977

Guodonggogo opened this issue Oct 21, 2021 · 7 comments
Labels
🫂 Support User help and QA

Comments

@Guodonggogo
Copy link

Guodonggogo commented Oct 21, 2021

Description

I would like resize a binary image (dtype='np.int') using transform.resize, but its output becomes all zeros. But if I change the input image type to 'np.bool_', the output is correct.

Way to reproduce

import numpy as np
import matplotlib.pyplot as plt
from skimage import io, transform

path_image = './mask.png'
label = io.imread(path_image)
label0 = np.array(label>0, np.int)
label1 = np.array(label>0, np.bool_)

fig = plt.figure()
ax1 = fig.add_subplot(131)  
ax2 = fig.add_subplot(132)  
ax3 = fig.add_subplot(133)  
result0 = transform.resize(label0,(288,288), mode='constant', order=0, preserve_range=False)
result1 = transform.resize(label1,(288,288), mode='constant', order=0, preserve_range=False)

ax1.imshow(label*255, vmin=0, vmax=255)
ax2.imshow(result0*255, vmin=0, vmax=255)
ax3.imshow(result1*255, vmin=0, vmax=255)
plt.show()

print('integer', np.max(result0))
print('bool', np.max(result1))

mask

Place the full code we need to recreate your issue here

upload all necessary images to github too!

Version information

# Paste the output of the following python commands
from __future__ import print_function
import sys; print(sys.version)
import platform; print(platform.platform())
import skimage; print(f'scikit-image version: {skimage.__version__}')
import numpy; print(f'numpy version: {numpy.__version__}')

3.7.4 (default, Aug 13 2019, 20:35:49)
[GCC 7.3.0]
Linux-5.4.0-77-generic-x86_64-with-debian-buster-sid
scikit-image version: 0.17.2
numpy version: 1.16.1

Screenshot from 2021-10-22 00-15-06

# your output here

integer 5.421010862427522e-20
bool 1.0

"preserve_range = True" has same issue
``

`
@mkcor mkcor changed the title Binaray image (np.int dtype) become all zero after resizing Binary image (np.int dtype) become all zero after resizing Oct 21, 2021
@mkcor
Copy link
Member

mkcor commented Oct 21, 2021

Hi @Guodonggogo,

Thanks for reaching out!

It looks like the image didn't upload properly in your post. Would you mind re-uploading it?

Thanks

@mkcor
Copy link
Member

mkcor commented Oct 21, 2021

Ok, this one I can see, I guess it's the figure with 3 subplots:
screenshot

@grlee77
Copy link
Contributor

grlee77 commented Oct 21, 2021

does it work for you with the np.int image if you set anti_aliasing=False when calling resize? (possibly related to #4292 which was fixed by automatically disabling anti-aliasing when the input has bool type. The same automatic disabling will not occur in your case since the type is int rather than bool)

@Guodonggogo
Copy link
Author

@grlee77 Thanks a lot for replying. I tried result0 = transform.resize(label0,(288,288), mode='constant', order=0, preserve_range=False, anti_aliasing=False)
But it doesn't help.

@rfezzani
Copy link
Member

OK, with current dev version @grlee77 answer does the trick:

import imageio
import numpy as np
import matplotlib.pyplot as plt
from skimage.transform import resize

label = imageio.imread("/tmp/mask.png")

label0 = np.array(label > 0, np.int)
label1 = np.array(label > 0, np.bool_)

result0 = resize(label0, (288, 288), mode='constant', order=0,
                 preserve_range=False, anti_aliasing=False)
result1 = resize(label1, (288, 288), mode='constant', order=0,
                 preserve_range=False, anti_aliasing=False)

fig, (ax0, ax1, ax2) = plt.subplots(1, 3, figsize=(8, 4))

ax0.imshow(label)
ax1.imshow(result0)
ax2.imshow(result1)
fig.tight_layout()
plt.show()

Figure_1

@Guodonggogo, I edited your report message to correctly display your images 😉

@rfezzani rfezzani added the 🫂 Support User help and QA label Oct 22, 2021
@Guodonggogo
Copy link
Author

@rfezzani Thank you a lot. So, the result above is from 0.19.0.dev0, is it?

@rfezzani
Copy link
Member

@rfezzani Thank you a lot. So, the result above is from 0.19.0.dev0, is it?

Yes, exactly! I hope you don't mind @Guodonggogo if I convert this issue to discussion. Thank you again for your interest.

@scikit-image scikit-image locked and limited conversation to collaborators Oct 25, 2021
@scikit-image scikit-image unlocked this conversation Apr 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🫂 Support User help and QA
Projects
None yet
Development

No branches or pull requests

4 participants