Skip to content

Commit

Permalink
differences for PR #315
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Dec 11, 2023
1 parent 46cdacc commit 1a7455d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
40 changes: 22 additions & 18 deletions 07-thresholding.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,17 @@ plt.xlim(0, 1.0)
![](fig/maize-root-cluster-histogram.png){alt='Grayscale histogram of the maize root image'}

The histogram has a significant peak around 0.2, and a second,
smaller peak very near 1.0.
shallower peak near 0.6.
Thus, this image is a good candidate for thresholding with Otsu's method.
The mathematical details of how this works are complicated (see
[the scikit-image documentation](https://scikit-image.org/docs/dev/api/skimage.filters.html#threshold-otsu)
if you are interested),
but the outcome is that Otsu's method finds a threshold value between
the two peaks of a grayscale histogram.
Note there is a third peak very close to 1.0 which corresponds to the white label and disk on the
left hand side of the image. It would be a good idea to remove these white pixels
before calculating the Otsu threshold. We will discuss how to do this later in the episode but for
now we will continue without removing them.

The `ski.filters.threshold_otsu()` function can be used to determine
the threshold automatically via Otsu's method.
Expand Down Expand Up @@ -462,10 +466,10 @@ def measure_root_mass(filename, sigma=1.0):
binary_mask = blurred_image > t

# determine root mass ratio
rootPixels = np.count_nonzero(binary_mask)
root_pixels = np.count_nonzero(binary_mask)
w = binary_mask.shape[1]
h = binary_mask.shape[0]
density = rootPixels / (w * h)
density = root_pixels / (w * h)

return density
```
Expand Down Expand Up @@ -612,9 +616,8 @@ and label from the image before applying Otsu's method.
## Solution

We can apply a simple binary thresholding with a threshold
`t=0.95` to remove the label and circle from the image. We use the
binary mask to set the pixels in the blurred image to zero
(black).
`t=0.95` to remove the label and circle from the image. We can then use the
binary mask to calculate the Otsu threshold without the pixels from the label and circle.

```python
def enhanced_root_mass(filename, sigma):
Expand All @@ -627,21 +630,22 @@ def enhanced_root_mass(filename, sigma):

# perform binary thresholding to mask the white label and circle
binary_mask = blurred_image < 0.95
# use the mask to remove the circle and label from the blurred image
blurred_image[~binary_mask] = 0

# perform automatic thresholding to produce a binary image
t = ski.filters.threshold_otsu(blurred_image)
binary_mask = blurred_image > t

# perform automatic thresholding using only the pixels with value True in the binary mask
t = ski.filters.threshold_otsu(blurred_image[binary_mask])

# update binary mask to identify pixels which are both less than 0.95 and greater than t
binary_mask = np.logical_and(binary_mask, blurred_image > t)

# determine root mass ratio
rootPixels = np.count_nonzero(binary_mask)
root_pixels = np.count_nonzero(binary_mask)
w = binary_mask.shape[1]
h = binary_mask.shape[0]
density = rootPixels / (w * h)
density = root_pixels / (w * h)

return density


all_files = glob.glob("data/trial-*.jpg")
for filename in all_files:
density = enhanced_root_mass(filename=filename, sigma=1.5)
Expand All @@ -653,10 +657,10 @@ The output of the improved program does illustrate that the white circles
and labels were skewing our root mass ratios:

```output
data/trial-016.jpg,0.045935837765957444
data/trial-020.jpg,0.058800033244680854
data/trial-216.jpg,0.13705003324468085
data/trial-293.jpg,0.13164461436170213
data/trial-016.jpg,0.046250166223404256
data/trial-020.jpg,0.05886968085106383
data/trial-216.jpg,0.13712117686170214
data/trial-293.jpg,0.13190342420212767
```

Here are the binary images produced by the additional thresholding.
Expand Down
2 changes: 1 addition & 1 deletion md5sum.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"episodes/04-drawing.md" "9d78a765f5e9747ffc2aa43a4a5a414d" "site/built/04-drawing.md" "2023-09-05"
"episodes/05-creating-histograms.md" "59c07192c0a6217e8a42d3e7365025f9" "site/built/05-creating-histograms.md" "2023-12-08"
"episodes/06-blurring.md" "8d109bb4c49f27f54857f6d35b4c6b9a" "site/built/06-blurring.md" "2023-12-08"
"episodes/07-thresholding.md" "7ae5260f90e1df8e20a6226cce8ec6b6" "site/built/07-thresholding.md" "2023-09-05"
"episodes/07-thresholding.md" "f0e0f3b873b02661f14f8dd4945c6d97" "site/built/07-thresholding.md" "2023-12-11"
"episodes/08-connected-components.md" "59d42797208c5bf569da2fa2e4dd05df" "site/built/08-connected-components.md" "2023-09-09"
"episodes/09-challenges.md" "a3ace24af8f5cb0bda1e9379a688ad4c" "site/built/09-challenges.md" "2023-09-05"
"instructors/instructor-notes.md" "b1c166a544eb4b9b91f3ac8f2dafd549" "site/built/instructor-notes.md" "2023-05-12"
Expand Down

0 comments on commit 1a7455d

Please sign in to comment.