在用python进行图像处理时,二值化是非常重要的一步,现总结了自己遇到过的6种 图像二值化的方法(当然这个绝对不是全部的二值化方法,若发现新的方法会继续新增)。
相关学习推荐:python视频教程
1. opencv 简单阈值 cv2.threshold
2. opencv 自适应阈值 cv2.adaptiveThreshold (自适应阈值中计算阈值的方法有两种:mean_c 和 guassian_c ,可以尝试用下哪种效果好)
3. Otsu's 二值化
例子:
import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('scratch.png', 0) # global thresholding ret1, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # Otsu's thresholding th2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) # Otsu's thresholding # 阈值一定要设为 0 ! ret3, th3 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # plot all the images and their histograms images = [img, 0, th1, img, 0, th2, img, 0, th3] titles = [ 'Original Noisy Image', 'Histogram', 'Global Thresholding (v=127)', 'Original Noisy Image', 'Histogram', "Adaptive Thresholding", 'Original Noisy Image', 'Histogram', "Otsu's Thresholding" ] # 这里使用了 pyplot 中画直方图的方法, plt.hist, 要注意的是它的参数是一维数组 # 所以这里使用了( numpy ) ravel 方法,将多维数组转换成一维,也可以使用 flatten 方法 # ndarray.flat 1-D iterator over an array. # ndarray.flatten 1-D array copy of the elements of an array in row-major order. for i in range(3): plt.subplot(3, 3, i * 3 + 1), plt.imshow(images[i * 3], 'gray') plt.title(titles[i * 3]), plt.xticks([]), plt.yticks([]) plt.subplot(3, 3, i * 3 + 2), plt.hist(images[i * 3].ravel(), 256) plt.title(titles[i * 3 + 1]), plt.xticks([]), plt.yticks([]) plt.subplot(3, 3, i * 3 + 3), plt.imshow(images[i * 3 + 2], 'gray') plt.title(titles[i * 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
import matplotlib import matplotlib.pyplot as plt from skimage.data import page from skimage.filters import (threshold_otsu, threshold_niblack, threshold_sauvola) matplotlib.rcParams['font.size'] = 9 image = page() binary_global = image > threshold_otsu(image) window_size = 25 thresh_niblack = threshold_niblack(image, window_size=window_size, k=0.8) thresh_sauvola = threshold_sauvola(image, window_size=window_size) binary_niblack = image > thresh_niblack binary_sauvola = image > thresh_sauvola plt.figure(figsize=(8, 7)) plt.subplot(2, 2, 1) plt.imshow(image, cmap=plt.cm.gray) plt.title('Original') plt.axis('off') plt.subplot(2, 2, 2) plt.title('Global Threshold') plt.imshow(binary_global, cmap=plt.cm.gray) plt.axis('off') plt.subplot(2, 2, 3) plt.imshow(binary_niblack, cmap=plt.cm.gray) plt.title('Niblack Threshold') plt.axis('off') plt.subplot(2, 2, 4) plt.imshow(binary_sauvola, cmap=plt.cm.gray) plt.title('Sauvola Threshold') plt.axis('off') plt.show()
结果图:
6.IntegralThreshold(主要用于文本检测)
使用方法: 运行下面网址的util.py文件
https://github.com/Liang-yc/IntegralThreshold
结果图:
以上是Python图像处理二值化方法实例云集的详细内容。更多信息请关注PHP中文网其他相关文章!

Python在自动化、脚本编写和任务管理中表现出色。1)自动化:通过标准库如os、shutil实现文件备份。2)脚本编写:使用psutil库监控系统资源。3)任务管理:利用schedule库调度任务。Python的易用性和丰富库支持使其在这些领域中成为首选工具。

要在有限的时间内最大化学习Python的效率,可以使用Python的datetime、time和schedule模块。1.datetime模块用于记录和规划学习时间。2.time模块帮助设置学习和休息时间。3.schedule模块自动化安排每周学习任务。

Python在游戏和GUI开发中表现出色。1)游戏开发使用Pygame,提供绘图、音频等功能,适合创建2D游戏。2)GUI开发可选择Tkinter或PyQt,Tkinter简单易用,PyQt功能丰富,适合专业开发。

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。 Python以简洁和强大的生态系统着称,C 则以高性能和底层控制能力闻名。

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

记事本++7.3.1
好用且免费的代码编辑器