国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python圖像處理二值化方法實例匯總

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

2020-07-25 00:07ttweixiao9999 Python

這篇文章主要介紹了Python圖像處理二值化方法實例匯總,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

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

1. opencv 簡單閾值 cv2.threshold

2. opencv 自適應閾值 cv2.adaptiveThreshold (自適應閾值中計算閾值的方法有兩種:mean_c 和 guassian_c ,可以嘗試用下哪種效果好)

3. Otsu's 二值化

例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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()

結果圖:

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

4. skimage niblack閾值

5. skimage sauvola閾值 (主要用于文本檢測)

例子:

https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_niblack_sauvola.html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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()

結果圖:

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

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

使用方法: 運行下面網址的util.py文件

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

結果圖:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/ttweixiao-IT-program/p/12091820.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲情视频| 亚洲精品一区久久久久久 | 中文字幕在线观看av | 韩日中文字幕 | 透逼视频 | 日韩精品一区二区三区中文在线 | 久久av网 | 国产精品欧美日韩在线观看 | 久久久精品蜜桃 | 亚洲久久 | 亚洲精品乱码8久久久久久日本 | 免费在线观看黄色 | 国产一级在线免费观看 | 久久精品2019中文字幕 | 欧美男人的天堂 | 亚洲综合一区二区三区 | 国产精品大片 | 天天干天天搞天天射 | 日韩欧美在线播放 | 亚洲精品视频在线免费 | 欧美性一区 | 日韩福利在线 | 激情毛片| 欧美一级黄色片 | 午夜视频| 中文字幕 国产精品 | 九九综合| 日韩高清一区二区 | 午夜寂寞影视在线观看 | 一级大片免费观看 | 久久九九99| 91精品国产乱码久久久久久 | 国产精品免费精品自在线观看 | 日韩毛片视频 | 国产亚洲精品美女久久久久久久久久 | 免费久草 | 国产一级片| 久久精品电影 | 欧美一级欧美三级在线观看 | 国产一区二区三区不卡在线观看 | 精品国产91乱码一区二区三区 |