pycharm图像二值化
时间: 2023-09-18 16:14:43 浏览: 211
你可以按照以下步骤在PyCharm中进行图像二值化:
1. 导入cv2库
```python
import cv2
```
2. 读取图像
```python
img = cv2.imread('image.jpg', 0) # 0表示以灰度模式读取图像
```
3. 对图像进行二值化处理
```python
ret, binary_img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
```
其中,`ret`表示阈值,`binary_img`表示二值化后的图像。
4. 显示二值化后的图像
```python
cv2.imshow('binary image', binary_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
`cv2.imshow()` 用于显示图像,`cv2.waitKey(0)` 用于等待键盘输入,`cv2.destroyAllWindows()` 用于关闭所有窗口。
完整代码如下:
```python
import cv2
img = cv2.imread('image.jpg', 0)
ret, binary_img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
cv2.imshow('binary image', binary_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文
相关推荐


















