Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3ac7392

Browse files
committedJun 3, 2024·
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 6c93b77 commit 3ac7392

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed
 

‎computer_vision/otsu_threshold.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
https://en.wikipedia.org/wiki/Otsu%27s_method
77
"""
88

9+
910
def otsu_threshold(image: Image) -> Image:
1011
"""
1112
image: is a grayscale PIL image object
@@ -40,7 +41,11 @@ def otsu_threshold(image: Image) -> Image:
4041
mean_background = sum_foreground / weight_background
4142
mean_foreground = (sum_total - sum_foreground) / weight_foreground
4243

43-
between_class_variance = weight_background * weight_foreground * (mean_background - mean_foreground) ** 2
44+
between_class_variance = (
45+
weight_background
46+
* weight_foreground
47+
* (mean_background - mean_foreground) ** 2
48+
)
4449

4550
if between_class_variance > current_max:
4651
current_max = between_class_variance
@@ -53,6 +58,7 @@ def otsu_threshold(image: Image) -> Image:
5358
# Convert numpy array back to PIL image
5459
return Image.fromarray(binary_image)
5560

61+
5662
if __name__ == "__main__":
5763
image = otsu_threshold(Image.open("path_to_image").convert("L"))
5864
image.save("output_image_path")

0 commit comments

Comments
 (0)
Please sign in to comment.