@@ -616,9 +616,8 @@ and label from the image before applying Otsu's method.
616
616
## Solution
617
617
618
618
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.
622
621
623
622
``` python
624
623
def enhanced_root_mass (filename , sigma ):
@@ -631,21 +630,22 @@ def enhanced_root_mass(filename, sigma):
631
630
632
631
# perform binary thresholding to mask the white label and circle
633
632
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)
640
639
641
640
# determine root mass ratio
642
- rootPixels = np.count_nonzero(binary_mask)
641
+ root_pixels = np.count_nonzero(binary_mask)
643
642
w = binary_mask.shape[1 ]
644
643
h = binary_mask.shape[0 ]
645
- density = rootPixels / (w * h)
644
+ density = root_pixels / (w * h)
646
645
647
646
return density
648
647
648
+
649
649
all_files = glob.glob(" data/trial-*.jpg" )
650
650
for filename in all_files:
651
651
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
657
657
and labels were skewing our root mass ratios:
658
658
659
659
``` 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
664
664
```
665
665
666
666
Here are the binary images produced by the additional thresholding.
0 commit comments