python otsu
时间: 2023-08-16 07:15:49 浏览: 99
Otsu's method is a popular image thresholding technique used in computer vision and image processing. It is used to automatically determine the optimal threshold value to separate the foreground and background of an image.
In Python, you can apply Otsu's method using the OpenCV library. Here's an example code snippet:
```python
import cv2
# Read the image in grayscale
image = cv2.imread('image.jpg', 0)
# Apply Otsu's thresholding
_, thresholded_image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Display the result
cv2.imshow('Thresholded Image', thresholded_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
In this code, we first read the image in grayscale using `cv2.imread` function. Then, we apply Otsu's thresholding using `cv2.threshold` function with `cv2.THRESH_BINARY + cv2.THRESH_OTSU` flag. Finally, we display the thresholded image using `cv2.imshow` and wait for a key press to close the window.
Make sure to replace `'image.jpg'` with the path to your actual image file.
阅读全文