Skip to content

Commit

Permalink
remove max_val param from threshold.mean()
Browse files Browse the repository at this point in the history
  • Loading branch information
afinit committed Jun 29, 2023
1 parent 0150bb2 commit 45c3607
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
13 changes: 6 additions & 7 deletions docs/mean_threshold.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This local threshold depends on the local average, computed in a squared portion

In the mean adaptive threshold, the local average is the average of the pixel values in the block.

**plantcv.threshold.mean**(*gray_img, block_size, offset, object_type="light", max_value=255*)
**plantcv.threshold.mean**(*gray_img, block_size, offset, object_type="light"*)

**returns** thresholded/binary image

Expand All @@ -19,10 +19,9 @@ In the mean adaptive threshold, the local average is the average of the pixel va
A negative offset sets the local threshold above the local average.
- object_type - "light" or "dark" (default: "light").
- "light" for objects brighter than the background, sets the pixels above
the local threshold to max_value and the pixels below to 0.
the local threshold to 255 and the pixels below to 0.
- "dark" for objects darker than the background, sets the pixels below the
local threshold to max_value and the pixels above to 0.
- max_value - Value to apply above threshold (default: 255 = white)
local threshold to 255 and the pixels above to 0.
- **Context:**
- Useful for unevenly illuminated images

Expand All @@ -41,13 +40,13 @@ pcv.params.debug = "plot"

# Adaptive threshold with different parameters
threshold_mean1 = pcv.threshold.mean(gray_img=gray_img, block_size=250, offset=25,
object_type='dark', max_value=255)
object_type='dark')

threshold_mean2 = pcv.threshold.mean(gray_img=gray_img, block_size=15, offset=5,
object_type='dark', max_value=255)
object_type='dark')

threshold_mean3 = pcv.threshold.mean(gray_img=gray_img, block_size=2000, offset=25,
object_type='dark', max_value=255)
object_type='dark')
```

**Thresholded image**
Expand Down
2 changes: 1 addition & 1 deletion docs/updating.md
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ pages for more details on the input and output variable types.

* pre v3.0dev2: NA
* post v3.0dev2: bin_img = **plantcv.threshold.mean**(*gray_img, max_value, object_type="light"*)
* post v4.0: bin_img = **plantcv.threshold.mean**(*gray_img, block_size, offset, object_type="light", max_value=255*)
* post v4.0: bin_img = **plantcv.threshold.mean**(*gray_img, block_size, offset, object_type="light"*)

#### plantcv.threshold.otsu

Expand Down
10 changes: 4 additions & 6 deletions plantcv/plantcv/threshold/threshold_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def gaussian(gray_img, block_size, offset, object_type="light", max_value=255):


# Mean adaptive threshold
def mean(gray_img, block_size, offset, object_type="light", max_value=255):
def mean(gray_img, block_size, offset, object_type="light"):
"""Creates a binary image from a grayscale image based on the mean adaptive threshold method.
Adaptive thresholds use a threshold value that varies across the image.
Expand All @@ -120,10 +120,9 @@ def mean(gray_img, block_size, offset, object_type="light", max_value=255):
A negative offset sets the local threshold above the local average.
object_type = "light" or "dark" (default: "light")
- "light" (for objects brighter than the background) sets the pixels above
the local threshold to max_value and the pixels below to 0.
the local threshold to 255 and the pixels below to 0.
- "dark" (for objects darker than the background) sets the pixels below the
local threshold to max_value and the pixels above to 0.
max_value = Value to apply above threshold (default: 255 = white)
local threshold to 255 and the pixels above to 0.
Returns:
bin_img = Thresholded, binary image
Expand All @@ -132,7 +131,6 @@ def mean(gray_img, block_size, offset, object_type="light", max_value=255):
:param block_size: int
:param offset: float
:param object_type: str
:param max_value: int
:return bin_img: numpy.ndarray
"""
# Set the threshold method
Expand All @@ -146,7 +144,7 @@ def mean(gray_img, block_size, offset, object_type="light", max_value=255):

params.device += 1

bin_img = _call_adaptive_threshold(gray_img, block_size, offset, max_value, cv2.ADAPTIVE_THRESH_MEAN_C,
bin_img = _call_adaptive_threshold(gray_img, block_size, offset, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
threshold_method, "_mean_threshold_")

return bin_img
Expand Down
6 changes: 3 additions & 3 deletions tests/plantcv/threshold/test_threshold_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_mean(objtype, size, threshold_test_data):
"""Test for PlantCV."""
# Read in test data
gray_img = cv2.imread(threshold_test_data.small_gray_img, -1)
binary_img = mean(gray_img=gray_img, block_size=size, offset=2, object_type=objtype, max_value=255)
binary_img = mean(gray_img=gray_img, block_size=size, offset=2, object_type=objtype)
# Assert that the output image has the dimensions of the input image and is binary
assert gray_img.shape == binary_img.shape and np.array_equal(np.unique(binary_img), np.array([0, 255]))

Expand All @@ -57,15 +57,15 @@ def test_mean_incorrect_object_type(threshold_test_data):
# Read in test data
gray_img = cv2.imread(threshold_test_data.small_gray_img, -1)
with pytest.raises(RuntimeError):
_ = mean(gray_img=gray_img, block_size=11, offset=2, object_type="lite", max_value=255)
_ = mean(gray_img=gray_img, block_size=11, offset=2, object_type="lite")


def test_mean_incorrect_block_size(threshold_test_data):
"""Test for PlantCV."""
# Read in test data
gray_img = cv2.imread(threshold_test_data.small_gray_img, -1)
with pytest.raises(RuntimeError):
_ = mean(gray_img=gray_img, block_size=1, offset=2, object_type="dark", max_value=255)
_ = mean(gray_img=gray_img, block_size=1, offset=2, object_type="dark")


@pytest.mark.parametrize("objtype", ["dark", "light"])
Expand Down

0 comments on commit 45c3607

Please sign in to comment.