亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python圖像處理二值化方法實例匯總

 更新時間:2020年07月24日 15:16:57   作者:ttweixiao9999  
這篇文章主要介紹了Python圖像處理二值化方法實例匯總,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

在用python進行圖像處理時,二值化是非常重要的一步,現(xiàn)總結(jié)了自己遇到過的6種 圖像二值化的方法(當然這個絕對不是全部的二值化方法,若發(fā)現(xiàn)新的方法會繼續(xù)新增)。

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, 要注意的是它的參數(shù)是一維數(shù)組
# 所以這里使用了( numpy ) ravel 方法,將多維數(shù)組轉(zhuǎn)換成一維,也可以使用 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()

結(jié)果圖:

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()

結(jié)果圖:

6.IntegralThreshold(主要用于文本檢測)

使用方法: 運行下面網(wǎng)址的util.py文件

https://github.com/Liang-yc/IntegralThreshold

結(jié)果圖:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python機器學習實戰(zhàn)之最近鄰kNN分類器

    python機器學習實戰(zhàn)之最近鄰kNN分類器

    這篇文章主要介紹了python機器學習實戰(zhàn)之最近鄰kNN分類器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 利用Python批量處理多個txt文本的示例代碼

    利用Python批量處理多個txt文本的示例代碼

    這篇文章主要給大家介紹了關(guān)于如何利用Python批量處理多個txt文本的方法,文中通過實例代碼介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2023-10-10
  • 基于scrapy的redis安裝和配置方法

    基于scrapy的redis安裝和配置方法

    今天小編就為大家分享一篇基于scrapy的redis安裝和配置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • django框架model orM使用字典作為參數(shù),保存數(shù)據(jù)的方法分析

    django框架model orM使用字典作為參數(shù),保存數(shù)據(jù)的方法分析

    這篇文章主要介紹了django框架model orM使用字典作為參數(shù),保存數(shù)據(jù)的方法,結(jié)合實例形式分析了字典參數(shù)結(jié)合django model實現(xiàn)保存數(shù)據(jù)相關(guān)操作技巧,需要的朋友可以參考下
    2019-06-06
  • TensorFlow使用Graph的基本操作的實現(xiàn)

    TensorFlow使用Graph的基本操作的實現(xiàn)

    這篇文章主要介紹了TensorFlow使用Graph的基本操作的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • Pandas庫中iloc[]函數(shù)的使用方法

    Pandas庫中iloc[]函數(shù)的使用方法

    在數(shù)據(jù)分析過程中,很多時候需要從數(shù)據(jù)表中提取出相應的數(shù)據(jù),而這么做的前提是需要先“索引”出這一部分數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Pandas庫中iloc[]函數(shù)的使用方法,需要的朋友可以參考下
    2023-01-01
  • python將.ppm格式圖片轉(zhuǎn)換成.jpg格式文件的方法

    python將.ppm格式圖片轉(zhuǎn)換成.jpg格式文件的方法

    今天小編就為大家分享一篇python將.ppm格式圖片轉(zhuǎn)換成.jpg格式文件的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Python實現(xiàn)圖片壓縮的案例詳解

    Python實現(xiàn)圖片壓縮的案例詳解

    這篇文章主要介紹了如何用最簡潔的Python代碼實現(xiàn)圖片壓縮效果,還可以保證照片不失真,感興趣的小伙伴可以跟隨小編一起動手試試
    2022-01-01
  • Numpy中np.vstack()?和?np.hstack()?的實現(xiàn)

    Numpy中np.vstack()?和?np.hstack()?的實現(xiàn)

    本文主要介紹了Numpy中np.vstack()和np.hstack()的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-04-04
  • Pytorch中的Broadcasting問題

    Pytorch中的Broadcasting問題

    這篇文章主要介紹了Pytorch中的Broadcasting問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01

最新評論