Saving Images after calculating local entropy

Hi,
I am trying to save the images after calculation local entropy.
I followed https://scikit-image.org/docs/stable/auto_examples/filters/plot_entropy.html.
I used cv2.imwrite. I saved black images. I tried with io.imsave. It is saving the image in low contrast.
Below is my code.

for root, dirs, file in os.walk(image_path, topdown = False):
for name in dirs:
dir = root + os.sep + name
path = os.path.basename(dir)
for filename in os.listdir(dir):
file = dir + os.sep + filename
image = io.imread(file, plugin = ‘matplotlib’)

        im = (rgb2gray(image))
        print(type(im))
        ent_image = entropy(im, disk(10))
        img = resize(ent_image, (299,299))
        
        image_saved_path = saved_path + os.sep + path
        if not os.path.exists(image_saved_path ):
           os.makedirs(image_saved_path)
                     
        image_base = os.path.basename(file)
     
      
       # io.imsave(image_saved_path + os.sep + image_base, img.astype(np.uint8))
       # img /= 255.0
        cv2.imwrite(image_saved_path + os.sep + image_base,img_as_ubyte(img))

Please help me out to solve the issue.

1 Like

Hi, could you check the values in the img image before saving?
Maybe they are outside of the unsigned byte datatype in which you are trying to save the image?

Yes, as @Christian_Tischer says, it’s likely that the entropy image doesn’t have the expected range for a floating point image in scikit-image, or has a restricted range. For more on image intensity ranges in scikit-image, see this document:

https://scikit-image.org/docs/dev/user_guide/data_types.html

The solution is to rescale your image to whatever output range you want. The function skimage.exposure.rescale_intensity can make this convenient:

https://scikit-image.org/docs/dev/api/skimage.exposure.html#skimage.exposure.rescale_intensity

1 Like