opencv-python图像二值化
时间: 2023-11-20 17:07:57 浏览: 110
OpenCV图像二值化
4星 · 用户满意度95%
以下是使用OpenCV进行图像二值化的方法:
1. 全局阈值二值化
```python
import cv2
img = cv2.imread('image.jpg', 0) # 读取灰度图像
ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # 全局阈值二值化
cv2.imshow('image', thresh1)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
2. 自适应阈值二值化
```python
import cv2
img = cv2.imread('image.jpg', 0) # 读取灰度图像
thresh2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) # 自适应阈值二值化
cv2.imshow('image', thresh2)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
3. TRIANGLE法自适应阈值二值化
```python
import cv2
img = cv2.imread('image.jpg', 0) # 读取灰度图像
ret, thresh3 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_TRIANGLE) # TRIANGLE法自适应阈值二值化
cv2.imshow('image', thresh3)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文