Skip to content

Commit ff0d2c3

Browse files
committed
better solution
1 parent d3ddff6 commit ff0d2c3

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

episodes/07-thresholding.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,8 @@ and label from the image before applying Otsu's method.
616616
## Solution
617617

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

623622
```python
624623
def enhanced_root_mass(filename, sigma):
@@ -631,21 +630,22 @@ def enhanced_root_mass(filename, sigma):
631630

632631
# perform binary thresholding to mask the white label and circle
633632
binary_mask = blurred_image < 0.95
634-
# use the mask to remove the circle and label from the blurred image
635-
blurred_image[~binary_mask] = 0
636-
637-
# perform automatic thresholding to produce a binary image
638-
t = ski.filters.threshold_otsu(blurred_image)
639-
binary_mask = blurred_image > t
633+
634+
# perform automatic thresholding using only the pixels with value True in the binary mask
635+
t = ski.filters.threshold_otsu(blurred_image[binary_mask])
636+
637+
# update binary mask to identify pixels which are both less than 0.95 and greater than t
638+
binary_mask = np.logical_and(binary_mask, blurred_image > t)
640639

641640
# determine root mass ratio
642-
rootPixels = np.count_nonzero(binary_mask)
641+
root_pixels = np.count_nonzero(binary_mask)
643642
w = binary_mask.shape[1]
644643
h = binary_mask.shape[0]
645-
density = rootPixels / (w * h)
644+
density = root_pixels / (w * h)
646645

647646
return density
648647

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

659659
```output
660-
data/trial-016.jpg,0.045935837765957444
661-
data/trial-020.jpg,0.058800033244680854
662-
data/trial-216.jpg,0.13705003324468085
663-
data/trial-293.jpg,0.13164461436170213
660+
data\trial-016.jpg,0.046250166223404256
661+
data\trial-020.jpg,0.05886968085106383
662+
data\trial-216.jpg,0.13712117686170214
663+
data\trial-293.jpg,0.13190342420212767
664664
```
665665

666666
Here are the binary images produced by the additional thresholding.

0 commit comments

Comments
 (0)