Python图像处理二值化方法实例汇总

 更新时间:2020年07月24日 15:16:57   作者:ttweixiao9999  
这篇文章主要介绍了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

结果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python编写生成验证码的脚本的教程

    Python编写生成验证码的脚本的教程

    这篇文章主要介绍了Python编写生成验证码的脚本的教程,验证码在web编程中几乎是必备的功能,需要的朋友可以参考下
    2015-05-05
  • 使用python求解迷宫问题的三种实现方法

    使用python求解迷宫问题的三种实现方法

    关于迷宫问题,常见会问能不能到达某点,以及打印到达的最短路径,下面这篇文章主要给大家介绍了关于如何使用python求解迷宫问题的三种实现方法,需要的朋友可以参考下
    2022-03-03
  • Pycharm使用matplotlib警告\不能显示的问题及解决

    Pycharm使用matplotlib警告\不能显示的问题及解决

    在PyCharm中使用matplotlib画图时遇到警告和图像显示问题,通过在代码中添加`mpl.use('TkAgg')`或`plt.switch_backend('TkAgg')`,可以解决警告并弹出显示完整的图像窗口,同时,文章还列举了matplotlib的其他backend选项,如Qt4Agg、Qt5Agg、WXAgg等
    2025-02-02
  • PySide6精简教程(附图文!)

    PySide6精简教程(附图文!)

    PySide6是Qt的Python封装,是Qt for Python的组成之一,另一部分是Shiboken,下面这篇文章主要给大家介绍了关于PySide6精简教程的相关资料,需要的朋友可以参考下
    2023-02-02
  • Django点赞的实现示例

    Django点赞的实现示例

    本文主要介绍了Django点赞的实现示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • 浅析python 动态库m.so.1.0错误问题

    浅析python 动态库m.so.1.0错误问题

    这篇文章主要介绍了python 动态库m.so.1.0错误问题,文中给大家提到了python中使用动态库的方法,通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • 使用Python实现简单的学生成绩管理系统

    使用Python实现简单的学生成绩管理系统

    这篇文章主要为大家详细介绍了Python实现学生成绩管理系统,使用数据库,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • python3使用logging包,如何把日志写到系统的rsyslog中

    python3使用logging包,如何把日志写到系统的rsyslog中

    这篇文章主要介绍了python3使用logging包,如何把日志写到系统的rsyslog中的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • python深度学习借助多标签分类器进行对抗训练

    python深度学习借助多标签分类器进行对抗训练

    这篇文章主要为大家介绍了python深度学习中如何借助多标签分类器进行对抗训练,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-01-01
  • opencv python在视屏上截图功能的实现

    opencv python在视屏上截图功能的实现

    OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows、Android和Mac OS操作系统上。这篇文章主要介绍了opencv python在视屏上截图,需要的朋友可以参考下
    2020-03-03

最新评论