opencv 最大熵二值化 python
时间: 2023-08-17 21:08:26 浏览: 187
在Python中使用OpenCV进行最大熵二值化的示例代码如下:
```python
import numpy as np
import cv2 as cv
import sys
image = cv.imread('F:/material/images/2022/2022-06/img_300_320.jpg')
if image is None:
print('Error: Could not load image')
sys.exit()
img_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow('img_gray', img_gray)
blockSize = 5
constValue = 0
maxVal = 255
img_B_MEAN = cv.adaptiveThreshold(img_gray, maxVal, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, blockSize, constValue)
img_B_GAUSSIAN = cv.adaptiveThreshold(img_gray, maxVal, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, blockSize, constValue)
cv.imshow('img_B_MEAN', img_B_MEAN)
cv.imshow('img_B_GAUSSIAN', img_B_GAUSSIAN)
cv.waitKey(0)
cv.destroyAllWindows()
```
这段代码使用OpenCV的`adaptiveThreshold()`函数进行最大熵二值化。其中,`blockSize`表示局部区域的大小,`constValue`表示从平均值或加权平均值中减去的常数值,`maxVal`表示最大值。通过调整这些参数,可以得到不同的二值化结果。在示例代码中,分别使用了`ADAPTIVE_THRESH_MEAN_C`和`ADAPTIVE_THRESH_GAUSSIAN_C`两种算法进行二值化,并显示了结果图像。
希望对你有帮助!
#### 引用[.reference_title]
- *1* [学习笔记6(opencv+python阈值分割(最大熵))](https://blog.csdn.net/weixin_44911091/article/details/107973515)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [与二值化阈值处理相关的OpenCV函数、方法汇总,便于对比和拿来使用](https://blog.csdn.net/wenhao_ir/article/details/125592598)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文