Home > Software design >  How to calculate corect way the entropy on image?
How to calculate corect way the entropy on image?

Time:11-02

When I want to calculate the entropy on an image I got only blank white image. How can I do it in the correct way?

def entropy_of_image(image):
    image_gray = rgb2gray(image)
    f_size = 20
    radi = list(range(1,10))
    entropy_tmp = entropy(image_gray, disk(radi[8])) 
    plt.savefig('images/tmpentropy.jpg')  
    return entropy_tmp  

and I call it like

    default_file = 'images/myimage.jpg'
    
    filename = os.path.join(default_file)
    img = io.imread(filename)
    img = img_as_float(img)
    gray_entropy = entropy_of_image(img)

doing with thesese:

from skimage import data
from skimage import data_dir
from skimage.util import img_as_ubyte
from skimage.filters.rank import entropy
from skimage.morphology import disk
from skimage import io
import os
from skimage.transform import rescale, resize, downscale_local_mean
import matplotlib.pyplot as plt
from skimage.io import imread, imshow
from skimage import data
from skimage.util import img_as_ubyte
from skimage.filters.rank import entropy
from skimage.morphology import disk
from skimage import data, color
from skimage.color import rgb2hsv, rgb2gray, rgb2yuv
from skimage.feature import canny
from skimage.transform import hough_circle, hough_circle_peaks
from skimage.draw import circle_perimeter

CodePudding user response:

When you write plt.savefig('images/tmpentropy.jpg'), what do you think pyplot will write to that JPEG file? You didn't give peplos any array to write. savefig writes the current figure window to file. Because you didn't create a figure window, or write anything to that figure window, there is nothing to save to the file, pyplot in this case apparently prefers to write a blank image over giving an error message.

If you first display your image to the figure window, then savefig will have something to write to the file. However, it will be what's in the figure window: the image with axes around it, and lots of white space. It is just like making a screen shot of the pyplot display:

plt.imshow(entropy_tmp)
plt.savefig('images/tmpentropy.jpg')

A better way to save an image to file is using an appropriate image file writing function, such as imageio.imwrite, matplotlib.pyplot.imsave, skimage.io.imsave, etc.

Since you're using scikit-image to read your image file, let's use that to write the output image file as well:

default_file = 'images/myimage.jpg'
filename = os.path.join(default_file)
img = io.imread(filename)
img = img_as_float(img)
gray_entropy = entropy_of_image(img)
skimage.io.imsave('images/tmpentropy.jpg', gray_entropy)  

(don't have your computation function entropy_of_image() write image files, have each function do only one thing!)

  • Related