新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python圖像二值化處理
一、圖像二值化

圖像二值化是指將圖像上像素點(diǎn)的灰度值設(shè)定為0或255,即整個(gè)圖像呈現(xiàn)明顯的黑白效果的過程。
二、python圖像二值化處理
1.opencv簡單閾值cv2.threshold
2.opencv自適應(yīng)閾值cv2.adaptiveThreshold
有兩種方法可用于計(jì)算自適應(yīng)閾值: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
# 閾值一定要設(shè)為 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()關(guān)于圖像二值化相關(guān)知識(shí)點(diǎn)推薦查看:
python全局圖像二值化
如何二值化圖像
更多python學(xué)習(xí)歡迎進(jìn)入python學(xué)習(xí)網(wǎng),免費(fèi)分享python學(xué)習(xí)教程和視頻!
新聞名稱:創(chuàng)新互聯(lián)Python教程:python圖像二值化處理
網(wǎng)頁網(wǎng)址:http://www.dlmjj.cn/article/dpchsdh.html


咨詢
建站咨詢
