From c991403c86a2102b4937a26f731831d27859b95d Mon Sep 17 00:00:00 2001 From: odebeir Date: Sat, 29 Aug 2015 10:41:10 +0200 Subject: [PATCH 1/6] add entropy example --- doc/examples/plot_entropy2.py | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 doc/examples/plot_entropy2.py diff --git a/doc/examples/plot_entropy2.py b/doc/examples/plot_entropy2.py new file mode 100644 index 00000000..10364089 --- /dev/null +++ b/doc/examples/plot_entropy2.py @@ -0,0 +1,38 @@ +""" +======= +Entropy +======= + +The following example applies the local entropy measure to a noised image with a variable noise. + +""" +import matplotlib.pyplot as plt +import numpy as np + +from skimage.filters.rank import entropy +from skimage.morphology import disk + +noise_mask = 28*np.ones((128,128),dtype=np.uint8) +noise_mask[32:-32,32:-32] = 30 + +noise = (noise_mask*np.random.random(noise_mask.shape)-.5*noise_mask).astype(np.uint8) +img = noise + 128 + +radius = 10 +e = entropy(img,disk(radius)) + +plt.figure(figsize=[15,5]) +plt.subplot(1,3,1) +plt.imshow(noise_mask,cmap=plt.cm.gray) +plt.xlabel('noise mask') +plt.colorbar() +plt.subplot(1,3,2) +plt.imshow(img,cmap=plt.cm.gray) +plt.xlabel('noised image') +plt.colorbar() +plt.subplot(1,3,3) +plt.imshow(e) +plt.xlabel('image local entropy ($r=%d$)'%radius) +plt.colorbar() + +plt.show() From f8afc07d80b0e7c214c8d8449cd2ade3ae97af9a Mon Sep 17 00:00:00 2001 From: odebeir Date: Sat, 29 Aug 2015 12:55:24 +0200 Subject: [PATCH 2/6] pep8 --- doc/examples/plot_entropy2.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/doc/examples/plot_entropy2.py b/doc/examples/plot_entropy2.py index 10364089..1c596e19 100644 --- a/doc/examples/plot_entropy2.py +++ b/doc/examples/plot_entropy2.py @@ -12,27 +12,27 @@ import numpy as np from skimage.filters.rank import entropy from skimage.morphology import disk -noise_mask = 28*np.ones((128,128),dtype=np.uint8) -noise_mask[32:-32,32:-32] = 30 +noise_mask = 28*np.ones((128, 128), dtype=np.uint8) +noise_mask[32:-32, 32:-32] = 30 noise = (noise_mask*np.random.random(noise_mask.shape)-.5*noise_mask).astype(np.uint8) img = noise + 128 radius = 10 -e = entropy(img,disk(radius)) +e = entropy(img, disk(radius)) -plt.figure(figsize=[15,5]) -plt.subplot(1,3,1) -plt.imshow(noise_mask,cmap=plt.cm.gray) +plt.figure(figsize=[15, 5]) +plt.subplot(1, 3, 1) +plt.imshow(noise_mask, cmap=plt.cm.gray) plt.xlabel('noise mask') plt.colorbar() -plt.subplot(1,3,2) -plt.imshow(img,cmap=plt.cm.gray) +plt.subplot(1, 3, 2) +plt.imshow(img, cmap=plt.cm.gray) plt.xlabel('noised image') plt.colorbar() -plt.subplot(1,3,3) +plt.subplot(1, 3, 3) plt.imshow(e) -plt.xlabel('image local entropy ($r=%d$)'%radius) +plt.xlabel('image local entropy ($r=%d$)' % radius) plt.colorbar() plt.show() From 35341bed139a0d17afe545cd7df355230dda86a2 Mon Sep 17 00:00:00 2001 From: odebeir Date: Sat, 29 Aug 2015 13:27:16 +0200 Subject: [PATCH 3/6] merge entropy examples+add description --- doc/examples/plot_entropy.py | 45 ++++++++++++++++++++++++++++++++--- doc/examples/plot_entropy2.py | 38 ----------------------------- 2 files changed, 42 insertions(+), 41 deletions(-) delete mode 100644 doc/examples/plot_entropy2.py diff --git a/doc/examples/plot_entropy.py b/doc/examples/plot_entropy.py index f33f26ff..f140b116 100644 --- a/doc/examples/plot_entropy.py +++ b/doc/examples/plot_entropy.py @@ -3,17 +3,56 @@ Entropy ======= -Image entropy is a quantity which is used to describe the amount of information -coded in an image. +In information theory, information entropy is the log-base-2 of the number of possible outcomes +for a message. + +For an image, local entropy is related to the complexity contained in a given neighborhood, typically defined by a +structuring element. A large number of various gray levels has a higher entropy than an homogeneous neighborhood. + +Entropy filter can detect subtle variations of local gray level distribution, in the example, the +image is composed of two surfaces with two slightly different distributions. + +Image center has a random distribution in the range [-14,+14] centered on 128, while the borders has a +random distribution in the range [-15,+15] centered on 128. + +We apply the local entropy measure using a circular structuring element of radius 10. As a result, one can +detect the central square. Radius should be big enough to efficiently sample the local gray level distribution. + +In the second example, the local entropy is used to detect image texture. """ import matplotlib.pyplot as plt +import numpy as np from skimage import data +from skimage.util import img_as_ubyte from skimage.filters.rank import entropy from skimage.morphology import disk -from skimage.util import img_as_ubyte +noise_mask = 28*np.ones((128, 128), dtype=np.uint8) +noise_mask[32:-32, 32:-32] = 30 + +noise = (noise_mask*np.random.random(noise_mask.shape)-.5*noise_mask).astype(np.uint8) +img = noise + 128 + +radius = 10 +e = entropy(img, disk(radius)) + +plt.figure(figsize=[15, 5]) +plt.subplot(1, 3, 1) +plt.imshow(noise_mask, cmap=plt.cm.gray) +plt.xlabel('noise mask') +plt.colorbar() +plt.subplot(1, 3, 2) +plt.imshow(img, cmap=plt.cm.gray) +plt.xlabel('noised image') +plt.colorbar() +plt.subplot(1, 3, 3) +plt.imshow(e) +plt.xlabel('image local entropy ($r=%d$)' % radius) +plt.colorbar() + +#second example: texture detection image = img_as_ubyte(data.camera()) diff --git a/doc/examples/plot_entropy2.py b/doc/examples/plot_entropy2.py deleted file mode 100644 index 1c596e19..00000000 --- a/doc/examples/plot_entropy2.py +++ /dev/null @@ -1,38 +0,0 @@ -""" -======= -Entropy -======= - -The following example applies the local entropy measure to a noised image with a variable noise. - -""" -import matplotlib.pyplot as plt -import numpy as np - -from skimage.filters.rank import entropy -from skimage.morphology import disk - -noise_mask = 28*np.ones((128, 128), dtype=np.uint8) -noise_mask[32:-32, 32:-32] = 30 - -noise = (noise_mask*np.random.random(noise_mask.shape)-.5*noise_mask).astype(np.uint8) -img = noise + 128 - -radius = 10 -e = entropy(img, disk(radius)) - -plt.figure(figsize=[15, 5]) -plt.subplot(1, 3, 1) -plt.imshow(noise_mask, cmap=plt.cm.gray) -plt.xlabel('noise mask') -plt.colorbar() -plt.subplot(1, 3, 2) -plt.imshow(img, cmap=plt.cm.gray) -plt.xlabel('noised image') -plt.colorbar() -plt.subplot(1, 3, 3) -plt.imshow(e) -plt.xlabel('image local entropy ($r=%d$)' % radius) -plt.colorbar() - -plt.show() From f0d201599377c62a140089ac1085e5114e47c3b5 Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Wed, 2 Sep 2015 16:11:10 +0200 Subject: [PATCH 4/6] fix >80 linelength --- doc/examples/plot_entropy.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/doc/examples/plot_entropy.py b/doc/examples/plot_entropy.py index f140b116..12c6777a 100644 --- a/doc/examples/plot_entropy.py +++ b/doc/examples/plot_entropy.py @@ -3,20 +3,24 @@ Entropy ======= -In information theory, information entropy is the log-base-2 of the number of possible outcomes -for a message. +In information theory, information entropy is the log-base-2 of the number of +possible outcomes for a message. -For an image, local entropy is related to the complexity contained in a given neighborhood, typically defined by a -structuring element. A large number of various gray levels has a higher entropy than an homogeneous neighborhood. +For an image, local entropy is related to the complexity contained in a given +neighborhood, typically defined by a structuring element. A large number of +various gray levels has a higher entropy than an homogeneous neighborhood. -Entropy filter can detect subtle variations of local gray level distribution, in the example, the -image is composed of two surfaces with two slightly different distributions. +Entropy filter can detect subtle variations of local gray level distribution, +in the example, the image is composed of two surfaces with two slightly +different distributions. -Image center has a random distribution in the range [-14,+14] centered on 128, while the borders has a -random distribution in the range [-15,+15] centered on 128. +Image center has a random distribution in the range [-14,+14] centered on 128, +while the borders has a random distribution in the range [-15,+15] centered +on 128. -We apply the local entropy measure using a circular structuring element of radius 10. As a result, one can -detect the central square. Radius should be big enough to efficiently sample the local gray level distribution. +We apply the local entropy measure using a circular structuring element of +radius 10. As a result, one can detect the central square. Radius should be big +enough to efficiently sample the local gray level distribution. In the second example, the local entropy is used to detect image texture. @@ -29,10 +33,11 @@ from skimage.util import img_as_ubyte from skimage.filters.rank import entropy from skimage.morphology import disk -noise_mask = 28*np.ones((128, 128), dtype=np.uint8) +noise_mask = 28 * np.ones((128, 128), dtype=np.uint8) noise_mask[32:-32, 32:-32] = 30 -noise = (noise_mask*np.random.random(noise_mask.shape)-.5*noise_mask).astype(np.uint8) +noise = (noise_mask * np.random.random(noise_mask.shape) - .5 * + noise_mask).astype(np.uint8) img = noise + 128 radius = 10 @@ -52,7 +57,7 @@ plt.imshow(e) plt.xlabel('image local entropy ($r=%d$)' % radius) plt.colorbar() -#second example: texture detection +# second example: texture detection image = img_as_ubyte(data.camera()) From 0fed2195601aa5e4b9e9ef28733b48550e654389 Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Fri, 4 Sep 2015 16:28:14 +0200 Subject: [PATCH 5/6] fix entropy example doc, fix figure syntax --- doc/examples/plot_entropy.py | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/doc/examples/plot_entropy.py b/doc/examples/plot_entropy.py index 12c6777a..8bcfdc52 100644 --- a/doc/examples/plot_entropy.py +++ b/doc/examples/plot_entropy.py @@ -10,17 +10,17 @@ For an image, local entropy is related to the complexity contained in a given neighborhood, typically defined by a structuring element. A large number of various gray levels has a higher entropy than an homogeneous neighborhood. -Entropy filter can detect subtle variations of local gray level distribution, -in the example, the image is composed of two surfaces with two slightly +The entropy filter can detect subtle variations of local gray level distribution. +In the example, the image is composed of two surfaces with two slightly different distributions. -Image center has a random distribution in the range [-14,+14] centered on 128, -while the borders has a random distribution in the range [-15,+15] centered -on 128. +Image has a uniform random distribution in the range [-14, +14] in the middle of the +image and a uniform random distribution in the range [-15, 15] at +the image borders, both centered at a gray value of 128. We apply the local entropy measure using a circular structuring element of -radius 10. As a result, one can detect the central square. Radius should be big -enough to efficiently sample the local gray level distribution. +radius 10. As a result, one can detect the central square. The radius is +big enough to efficiently sample the local gray level distribution. In the second example, the local entropy is used to detect image texture. @@ -43,19 +43,15 @@ img = noise + 128 radius = 10 e = entropy(img, disk(radius)) -plt.figure(figsize=[15, 5]) -plt.subplot(1, 3, 1) -plt.imshow(noise_mask, cmap=plt.cm.gray) -plt.xlabel('noise mask') -plt.colorbar() -plt.subplot(1, 3, 2) -plt.imshow(img, cmap=plt.cm.gray) -plt.xlabel('noised image') -plt.colorbar() -plt.subplot(1, 3, 3) -plt.imshow(e) -plt.xlabel('image local entropy ($r=%d$)' % radius) -plt.colorbar() +fig, ax = plt.subplots(1, 3, figsize=(8, 5)) +ax1, ax2, ax3 = ax.ravel() + +ax1.imshow(noise_mask, cmap=plt.cm.gray) +ax1.set_xlabel('Noise mask') +ax2.imshow(img, cmap=plt.cm.gray) +ax2.set_xlabel('Noised image') +ax3.imshow(e) +ax3.set_xlabel('Local entropy ($r=%d$)' % radius) # second example: texture detection From 86542c5916cfa35bd7300b68d2befb2dbb384974 Mon Sep 17 00:00:00 2001 From: Olivier Debeir Date: Mon, 7 Sep 2015 09:44:47 +0200 Subject: [PATCH 6/6] English corrections in entropy example --- doc/examples/plot_entropy.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/examples/plot_entropy.py b/doc/examples/plot_entropy.py index 8bcfdc52..29c70c20 100644 --- a/doc/examples/plot_entropy.py +++ b/doc/examples/plot_entropy.py @@ -7,16 +7,16 @@ In information theory, information entropy is the log-base-2 of the number of possible outcomes for a message. For an image, local entropy is related to the complexity contained in a given -neighborhood, typically defined by a structuring element. A large number of -various gray levels has a higher entropy than an homogeneous neighborhood. +neighborhood, typically defined by a structuring element. -The entropy filter can detect subtle variations of local gray level distribution. +The entropy filter can detect subtle variations in the local gray level +distribution. In the example, the image is composed of two surfaces with two slightly different distributions. -Image has a uniform random distribution in the range [-14, +14] in the middle of the -image and a uniform random distribution in the range [-15, 15] at -the image borders, both centered at a gray value of 128. +The image has a uniform random distribution in the range [-14, +14] in the +middle of the image and a uniform random distribution in the range [-15, 15] +at the image borders, both centered at a gray value of 128. We apply the local entropy measure using a circular structuring element of radius 10. As a result, one can detect the central square. The radius is