Skip to content

Commit 700b457

Browse files
committed
Use ndarray.size instead of multiplying
1 parent fe1d89d commit 700b457

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

episodes/07-thresholding.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,7 @@ def measure_root_mass(filename, sigma=1.0):
478478

479479
# determine root mass ratio
480480
root_pixels = np.count_nonzero(binary_mask)
481-
w = binary_mask.shape[1]
482-
h = binary_mask.shape[0]
483-
density = root_pixels / (w * h)
481+
density = root_pixels / binary_mask.size
484482

485483
return density
486484
```
@@ -499,11 +497,8 @@ Recall that in the `binary_mask`, every pixel has either a value of
499497
zero (black/background) or one (white/foreground).
500498
We want to count the number of white pixels,
501499
which can be accomplished with a call to the NumPy function `np.count_nonzero`.
502-
Then we determine the width and height of the image by using
503-
the elements of `binary_mask.shape`
504-
(that is, the dimensions of the NumPy array that stores the image).
505500
Finally, the density ratio is calculated by dividing the number of white pixels
506-
by the total number of pixels `w*h` in the image.
501+
by the total number of pixels `binary_mask.size` in the image.
507502
The function returns then root density of the image.
508503

509504
We can call this function with any filename and
@@ -650,9 +645,7 @@ def enhanced_root_mass(filename, sigma):
650645

651646
# determine root mass ratio
652647
root_pixels = np.count_nonzero(binary_mask)
653-
w = binary_mask.shape[1]
654-
h = binary_mask.shape[0]
655-
density = root_pixels / (w * h)
648+
density = root_pixels / binary_mask.size
656649

657650
return density
658651

0 commit comments

Comments
 (0)