Skip to content

Commit

Permalink
Use ndarray.size instead of multiplying
Browse files Browse the repository at this point in the history
  • Loading branch information
nanosec committed Jan 29, 2025
1 parent fe1d89d commit 700b457
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions episodes/07-thresholding.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,7 @@ def measure_root_mass(filename, sigma=1.0):

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

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

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

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

return density

Expand Down

0 comments on commit 700b457

Please sign in to comment.