Skip to content

Commit

Permalink
Improve example
Browse files Browse the repository at this point in the history
  • Loading branch information
sciunto committed May 21, 2019
1 parent fb90d0e commit 54cdfbe
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions doc/examples/segmentation/plot_backprojection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
feature to find the object in other images. So it takes two images as input,
one is your object image and the next is the image where you want to find the
object. The function then computes the histograms of these two images and
return a **backprojected image**. A backprojected image is a grayscale image
return a **backprojected image** [1]_. A backprojected image is a grayscale image
where each pixel denotes the probability of that pixel being part of the
object. By thresholding this backprojected image with a suitable value gives
the objects' mask.
Expand All @@ -21,9 +21,13 @@
thresholding to get the mask. Then a bitwise_and operation with input image
and mask gives the segmented object.
References
----------
.. [1] Swain, Michael J., and Dana H. Ballard. "Indexing via color histograms."
Active Perception and Robot Vision. Springer Berlin Heidelberg,
1992. 261-273. DOI:`10.1109/ICCV.1990.139558`
1992. 261-273. :DOI:`10.1109/ICCV.1990.139558`
"""
import numpy as np
Expand All @@ -35,30 +39,36 @@
from skimage import img_as_ubyte


img1 = data.immunohistochemistry()
img1 = img_as_ubyte(img1)
img2 = img1[:200, :200]
image = data.immunohistochemistry()
image = img_as_ubyte(image)
template = image[50:200, :150]

# apply histogram backprojection
bc = histogram_backprojection(img1, img2)
backprojection = histogram_backprojection(image, template)

# threshold the image with otsu's thresholding
thresh = threshold_otsu(bc)
mask = np.where(bc >= thresh, 255, 0).astype('uint8')
res = np.bitwise_and(img1, mask[..., np.newaxis])
thresh = threshold_otsu(backprojection)
mask = backprojection >= thresh
# Set zero values where the mask is False
res = np.where(mask[..., None], image, 0)

fig, ax = plt.subplots(nrows=2, ncols=2)
ax = ax.ravel()

fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3)
ax[0].imshow(image)
ax[0].axis('off')
ax[0].set_title('Input image')

ax1.imshow(img1)
ax1.axis('off')
ax1.set_title('input image')
ax[1].imshow(template)
ax[1].axis('off')
ax[1].set_title('Template')

ax2.imshow(mask, cmap=plt.cm.gray)
ax2.axis('off')
ax2.set_title('backprojected image')
ax[2].imshow(backprojection, cmap=plt.cm.gray)
ax[2].axis('off')
ax[2].set_title('Backprojected image')

ax3.imshow(res)
ax3.axis('off')
ax3.set_title('segmented image')
ax[3].imshow(res)
ax[3].axis('off')
ax[3].set_title('Segmented image')

plt.show()

0 comments on commit 54cdfbe

Please sign in to comment.