Skip to content

Commit 4127f59

Browse files
authored
Merge pull request #201 from datacarpentry/200-call-attention-to-very-different-implications-for-similar-looking-syntax
200 call attention to very different implications for similar looking syntax
2 parents ebcb2a4 + 3183a89 commit 4127f59

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

episodes/07-thresholding.md

Lines changed: 14 additions & 11 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)
@@ -605,10 +608,10 @@ data/trial-293.jpg,0.13607895611702128
605608
> > # blur before thresholding
606609
> > blurred_image = skimage.filters.gaussian(image, sigma=sigma)
607610
> >
608-
> > # perform inverse binary thresholding to mask the white label and circle
609-
> > binary_mask = blurred_image > 0.95
611+
> > # perform binary thresholding to mask the white label and circle
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)

episodes/08-connected-components.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Connected Component Analysis"
33
teaching: 70
4-
exercises: 45
4+
exercises: 55
55
questions:
66
- "How to extract separate objects from an image and describe these objects quantitatively."
77
objectives:
@@ -682,7 +682,7 @@ This will produce the output
682682
> {: .solution}
683683
{: .challenge}
684684
685-
> ## Color objects by area (10 min)
685+
> ## Color objects by area (optional, not included in timing)
686686
>
687687
> Finally, we would like to display the image with the objects colored
688688
> according to the magnitude of their area. In practice, this can be
@@ -715,5 +715,22 @@ This will produce the output
715715
> > {: .language-python}
716716
> >
717717
> > ![Objects colored by area](../fig/shapes-01-objects-coloured-by-area.png)
718+
> >
719+
> > > You may have noticed that in the solution, we have used the
720+
> > > `labeled_image` to index the array `object_areas`. This is an
721+
> > > example of [advanced indexing in
722+
> > > Numpy](https://numpy.org/doc/stable/user/basics.indexing.html#advanced-indexing)
723+
> > > The result is an array of the same shape as the `labeled_image`
724+
> > > whose pixel values are selected from `object_areas` according to
725+
> > > the object label. Hence the objects will be colored by area when
726+
> > > the result is displayed. Note that advanced indexing with an
727+
> > > integer array works slightly different than the indexing with a
728+
> > > Boolean array that we have used for masking. While Boolean array
729+
> > > indexing returns only the entries corresponding to the `True`
730+
> > > values of the index, integer array indexing returns an array
731+
> > > with the same shape as the index. You can read more about advanced
732+
> > > indexing in the [Numpy
733+
> > > documentation](https://numpy.org/doc/stable/user/basics.indexing.html#advanced-indexing).
734+
> > {: .callout}
718735
> {: .solution}
719736
{: .challenge}

0 commit comments

Comments
 (0)