Python图像处理二值化方法实例汇总
作者:ttweixiao9999 发布时间:2021-04-10 21:41:33
标签:python,图像,处理,二值,化
在用python进行图像处理时,二值化是非常重要的一步,现总结了自己遇到过的6种 图像二值化的方法(当然这个绝对不是全部的二值化方法,若发现新的方法会继续新增)。
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
结果图:
来源:https://www.cnblogs.com/ttweixiao-IT-program/p/12091820.html


猜你喜欢
- views.py 视图文件 message = None all_message = UserMessage.obj
- 小程序中英文混合排序问题在开发一个手机联系人列表的功能时,遇到需求是需要将联系人列表按照拼音顺序排序。而联系人列表是会出现中英文混合的情况。
- 这些年来,我发现许多开发者对于何时使用数据操纵语言(DML)触发器与何时使用约束感到迷惑。许多时候,如果没有正确应用这两个对象,就会造成问题
- 可直接用这行命令!: pip install -U scikit-learn其他命令: (1)更新pippython -m pip inst
- 简介在日常开发中,我们的大部分时间都会花在阅读traceback模块信息以及调试代码上。本文我们将改进traceback模块,让其中的提示信
- 查看两个数据库的同名表的字段名差异问题描述开发过程中有多个测试环境,测试环境 A 加了字段,测试环境 B 忘了加,字段名对不上,同一项目就报
- 顽固的Select下拉列表,一般很难用css来控制样式下面使用了js来美化select<!DOCTYPE html PUBLIC &q
- 进程进程是操作系统分配资源的基本单元,是程序隔离的边界。进程和程序程序只是一组指令的集合,它本身没有任何运行的含义,它是静态的。进程程序的执
- 前言: 在项目开发中,一些业务表字段经常使用日期和时间类型,而且后续还会牵涉到这类字段的查询。关于日期及时间的查询等各类需求也很多,本篇文章
- 目录建表查看数据库文件:插入查询删除补充:Mysql自动按月表分区MySQL单表数据量,建议不要超过2000W行,否则会对性能有较大影响。最
- 产生batch数据输入data中每个样本可以有多个特征,和一个标签,最好都是numpy.array格式。datas = [data1, da
- 1 原理 2 检测步骤将参数空间(ρ,θ) 量化成m*n(m为ρ的等份数,n为θ的等份数)个单元,并设置累加器矩阵,初始值为0;对
- 本文研究的主要是Python多线程threading和multiprocessing模块的相关内容,具体介绍如下。线程是一个进程的实体,是由
- 注意:什么路径不可改就搜索该路径进行删除注册表记录,然后重启电脑按 win键盘+R输入 regedit 进去注册表,点击计算机按“编辑”--
- 什么是触发器?触发器是在对表进行插入、更新或删除操作时自动执行的存储过程。 触发器对表进行插入、更新、删除的时候会自动执行的特殊存储过程。触
- binascii模块用法binascii模块用于在二进制和ASCII之间转换>> import binascii# 将binar
- python的字符串编码识别模块(第三方库):官方地址: http://pypi.python.org/pypi/chardetimport
- 前言默认情况下SQL SERVER的安装路径与数据库的默认存放路径是在C盘的--这就很尴尬。平时又不注意,有天发现C盘的剩余空间比较吃紧了,
- 大家都遇到过验证码,随着灌水机的发展,验证码也是日新月异,验证码开始分了繁体简体,带着字母、符号,甚至开始了中文验证码,看到这样的验证码,估
- 引言在开发过程中,经常需要观察本地文件系统的更改。经过谷歌了几个小时后,到了一个简单的工具来做这件事。该工具就是fsnotify是一个Go跨