主頁 > 知識庫 > opencv函數(shù)threshold、adaptiveThreshold、Otsu二值化的實(shí)現(xiàn)

opencv函數(shù)threshold、adaptiveThreshold、Otsu二值化的實(shí)現(xiàn)

熱門標(biāo)簽:crm電銷機(jī)器人 電銷機(jī)器人 金倫通信 南京crm外呼系統(tǒng)排名 北京外呼電銷機(jī)器人招商 賓館能在百度地圖標(biāo)注嗎 鄭州智能外呼系統(tǒng)中心 400電話 申請 條件 云南地圖標(biāo)注 汕頭電商外呼系統(tǒng)供應(yīng)商

threshold:固定閾值二值化,

ret, dst = cv2.threshold(src, thresh, maxval, type)
  • src: 輸入圖,只能輸入單通道圖像,通常來說為灰度圖
  • dst: 輸出圖
  • thresh: 閾值
  • maxval: 當(dāng)像素值超過了閾值(或者小于閾值,根據(jù)type來決定),所賦予的值
  • type:二值化操作的類型,包含以下5種類型: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO;cv2.THRESH_TOZERO_INV

官方文檔的示例代碼:

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('gradient.png',0)
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in xrange(6):
  plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
  plt.title(titles[i])
  plt.xticks([]),plt.yticks([])
plt.show()

結(jié)果為:

 

adaptiveThreshold:自適應(yīng)閾值二值化

自適應(yīng)閾值二值化函數(shù)根據(jù)圖片一小塊區(qū)域的值來計(jì)算對應(yīng)區(qū)域的閾值,從而得到也許更為合適的圖片。

dst = cv2.adaptiveThreshold(src, maxval, thresh_type, type, Block Size, C)
  • src: 輸入圖,只能輸入單通道圖像,通常來說為灰度圖
  • dst: 輸出圖
  • maxval: 當(dāng)像素值超過了閾值(或者小于閾值,根據(jù)type來決定),所賦予的值
  • thresh_type: 閾值的計(jì)算方法,包含以下2種類型:cv2.ADAPTIVE_THRESH_MEAN_C; cv2.ADAPTIVE_THRESH_GAUSSIAN_C.
  • type:二值化操作的類型,與固定閾值函數(shù)相同,包含以下5種類型: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO;cv2.THRESH_TOZERO_INV.
  • Block Size: 圖片中分塊的大小
  • C :閾值計(jì)算方法中的常數(shù)項(xiàng)

官方文檔的示例代碼:

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('sudoku.png',0)
img = cv2.medianBlur(img,5)
ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\

      cv2.THRESH_BINARY,11,2)
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\

      cv2.THRESH_BINARY,11,2)
titles = ['Original Image', 'Global Thresholding (v = 127)',
      'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in xrange(4):
  plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
  plt.title(titles[i])
  plt.xticks([]),plt.yticks([])
plt.show()

結(jié)果為:

 

Otsu's Binarization: 基于直方圖的二值化

Otsu's Binarization是一種基于直方圖的二值化方法,它需要和threshold函數(shù)配合使用。

Otsu過程:
1. 計(jì)算圖像直方圖;
2. 設(shè)定一閾值,把直方圖強(qiáng)度大于閾值的像素分成一組,把小于閾值的像素分成另外一組;
3. 分別計(jì)算兩組內(nèi)的偏移數(shù),并把偏移數(shù)相加;
4. 把0~255依照順序多為閾值,重復(fù)1-3的步驟,直到得到最小偏移數(shù),其所對應(yīng)的值即為結(jié)果閾值。

官方文檔的示例代碼:

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('noisy2.png',0)
# global thresholding
ret1,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
# Otsu's thresholding
ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Otsu's thresholding after Gaussian filtering
blur = cv2.GaussianBlur(img,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# plot all the images and their histograms
images = [img, 0, th1,
     img, 0, th2,
     blur, 0, th3]
titles = ['Original Noisy Image','Histogram','Global Thresholding (v=127)',
     'Original Noisy Image','Histogram',"Otsu's Thresholding",
     'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]
for i in xrange(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é)果為:

 

參考文獻(xiàn):http://docs.opencv.org/3.2.0/d7/d4d/tutorial_py_thresholding.html

到此這篇關(guān)于opencv函數(shù)threshold、adaptiveThreshold、Otsu二值化的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)opencv threshold、adaptiveThreshold、Otsu內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • OpenCV 使用imread()函數(shù)讀取圖片的六種正確姿勢
  • python+opencv邊緣提取與各函數(shù)參數(shù)解析
  • 詳解opencv中畫圓circle函數(shù)和橢圓ellipse函數(shù)
  • 使用OpenCV circle函數(shù)圖像上畫圓的示例代碼
  • Python OpenCV 使用滑動條來調(diào)整函數(shù)參數(shù)的方法
  • Opencv2.4.9函數(shù)HoughLinesP分析
  • OpenCV中的cv::Mat函數(shù)將數(shù)據(jù)寫入txt文件

標(biāo)簽:懷化 昆明 西寧 梅州 文山 石家莊 浙江 錫林郭勒盟

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《opencv函數(shù)threshold、adaptiveThreshold、Otsu二值化的實(shí)現(xiàn)》,本文關(guān)鍵詞  opencv,函數(shù),threshold,adaptiveThreshold,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《opencv函數(shù)threshold、adaptiveThreshold、Otsu二值化的實(shí)現(xiàn)》相關(guān)的同類信息!
  • 本頁收集關(guān)于opencv函數(shù)threshold、adaptiveThreshold、Otsu二值化的實(shí)現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章