Skip to content

Commit e47d2fd

Browse files
committed
Use image[~binary_mask]=0 to mask images
1 parent 15eabf9 commit e47d2fd

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

episodes/07-thresholding.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,11 @@ to create a mask. Here, we want to turn "on" all pixels which have
143143
values smaller than the threshold, so we use the less operator `<` to
144144
compare the `blurred_image` to the threshold `t`. The operator returns
145145
a mask, that we capture in the variable `binary_mask`. It has only one
146-
channel, and each of its values is either 0 or 1. The binary mask
147-
created by the thresholding operation can be shown with `plt.imshow`.
146+
channel, and each of its values is either `False` or `True`. The
147+
binary mask created by the thresholding operation can be shown with
148+
`plt.imshow`, where the `False` entries are shown as black pixels
149+
(0-valued) and the `True` entries are shown as white pixels
150+
(1-valued).
148151

149152
~~~
150153
# create a mask based on the threshold
@@ -193,8 +196,8 @@ colored shapes from the original.
193196

194197
~~~
195198
# use the binary_mask to select the "interesting" part of the image
196-
selection = np.zeros_like(image)
197-
selection[binary_mask] = image[binary_mask]
199+
selection = image.copy()
200+
selection[~binary_mask] = 0
198201
199202
fig, ax = plt.subplots()
200203
plt.imshow(selection)
@@ -268,8 +271,8 @@ plt.show()
268271
> > And here are the commands to apply the mask and view the thresholded image
269272
> > ~~~
270273
> > image = skimage.io.imread("data/shapes-02.jpg")
271-
> > selection = np.zeros_like(image)
272-
> > selection[binary_mask] = image[binary_mask]
274+
> > selection = image.copy()
275+
> > selection[~binary_mask] = 0
273276
> >
274277
> > fig, ax = plt.subplots()
275278
> > plt.imshow(selection)
@@ -384,8 +387,8 @@ Finally, we use the mask to select the foreground:
384387
385388
~~~
386389
# apply the binary mask to select the foreground
387-
selection = np.zeros_like(image)
388-
selection[binary_mask] = image[binary_mask]
390+
selection = image.copy()
391+
selection[~binary_mask] = 0
389392

390393
fig, ax = plt.subplots()
391394
plt.imshow(selection)
@@ -606,9 +609,9 @@ data/trial-293.jpg,0.13607895611702128
606609
> > blurred_image = skimage.filters.gaussian(image, sigma=sigma)
607610
> >
608611
> > # perform inverse binary thresholding to mask the white label and circle
609-
> > binary_mask = blurred_image > 0.95
612+
> > binary_mask = blurred_image < 0.95
610613
> > # use the mask to remove the circle and label from the blurred image
611-
> > blurred_image[binary_mask] = 0
614+
> > blurred_image[~binary_mask] = 0
612615
> >
613616
> > # perform automatic thresholding to produce a binary image
614617
> > t = skimage.filters.threshold_otsu(blurred_image)

0 commit comments

Comments
 (0)