介绍
本篇文章给大家分享的是有关Python图像处理二值化方法有哪些实例,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
在用Python进行图像处理时,二值化是非常重要的一步,现总结了自己遇到过的6种图像二值化的方法(当然这个绝对不是全部的二值化方法,若发现新的方法会继续新增)。
引用>
1。opencv简单阈值cv2.threshold
2。opencv自适应阈值cv2。adaptiveThreshold(自适应阈值中计算阈值的方法有两种:mean_c和guassian_c,可以尝试用下哪种效果好)
3。大津# 39;s二值化
例子:
进口cv2 进口numpy np 从matplotlib进口pyplot plt img=cv2.imread (& # 39; scratch.png& # 39;, 0) #全球阈值 ret1 th2=cv2。阈值(cv2.THRESH_BINARY img、127、255) #大津# 39;阈值 th3=cv2。cv2 adaptiveThreshold (img 255。ADAPTIVE_THRESH_MEAN_C cv2。THRESH_BINARY 11 2) #大津# 39;阈值 #阈值一定要设为0 ! ret3 th4=cv2。阈值(img 0 255, cv2。THRESH_BINARY + cv2.THRESH_OTSU) #所有的图像及其直方图 图像=[img, 0 th2 img 0 th3, img, 0, th4] 标题=[ & # 39;原始噪声图像# 39;& # 39;直方图# 39;& # 39;全局阈值(v=127) & # 39;, & # 39;原始噪声图像# 39;& # 39;直方图# 39;,“自适应Thresholding" & # 39;原始噪声图像# 39;& # 39;直方图# 39;,“大津# 39;s Thresholding" ] #这里使用了pyplot中画直方图的方法,plt。嘘,要注意的是它的参数是一维数组 #所以这里使用了(numpy)拉威尔方法,将多维数组转换成一维,也可以使用扁方法 # ndarray。平的一维迭代器在一个数组中。 # ndarray。平一维数组复制行数组元素的顺序。 因为我在范围(3): plt。次要情节(3 3 * 3 + 1),plt。imshow(图片(* 3),& # 39;灰色# 39;) plt。标题(标题(* 3)),plt.xticks ([]), plt.yticks ([]) plt。次要情节(3,3,我* 3 + 2),plt。嘘(图像(* 3).ravel (), 256) plt。标题(标题[我* 3 + 1]),plt.xticks ([]), plt.yticks ([]) plt。次要情节(3 3 * 3 + 3),plt。imshow(图片(我* 3 + 2)& # 39;灰色# 39;) plt。标题(标题[我* 3 + 2]),plt.xticks ([]), plt.yticks ([]) plt.show ()结果图:
4。skimage niblack阈值
5。skimage sauvola阈值(主要用于文本检测)
例子:https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_niblack_sauvola.html
进口matplotlib 进口matplotlib。pyplot作为plt 从skimage。数据导入页面 从skimage。过滤器进口(threshold_otsu threshold_niblack, threshold_sauvola) matplotlib.rcParams [& # 39; font.size& # 39;]=9 形象=(页) binary_global=图像比;threshold_otsu(图片) window_size=25 thresh_niblack=threshold_niblack(形象,window_size=window_size k=0.8) thresh_sauvola=threshold_sauvola(形象,window_size=window_size) binary_niblack=图像比;thresh_niblack binary_sauvola=图像比;thresh_sauvola plt。7)图(figsize=(8日) plt。次要情节(2、2、1) plt。imshow(图像,提出=plt.cm.gray) plt.title(& # 39;原始# 39;) plt.axis(& # 39;从# 39;) plt。次要情节(2 2 2) plt.title(& # 39;全局阈值# 39;) plt。imshow (binary_global提出=plt.cm.gray) plt.axis(& # 39;从# 39;) plt。次要情节(2,2,3) plt。imshow (binary_niblack提出=plt.cm.gray) plt.title (& # 39; Niblack阈值# 39;) plt.axis(& # 39;从# 39;) plt。次要情节(2,2,4) plt。imshow (binary_sauvola提出=plt.cm.gray) plt.title (& # 39; Sauvola阈值# 39;) plt.axis(& # 39;从# 39;) plt.show ()结果图:
6. integralthreshold(主要用于文本检测)
使用方法:运行下面网址的util.py文件https://github.com/Liang-yc/IntegralThreshold
结果图:
以上就是Python图像处理二值化方法有哪些实例,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
Python图像处理二值化方法有哪些实例