opencv python如何實現(xiàn)圖像二值化
更新時間:2020年02月03日 10:35:41 作者:wbytts
這篇文章主要介紹了opencv python如何實現(xiàn)圖像二值化,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
這篇文章主要介紹了opencv python如何實現(xiàn)圖像二值化,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
代碼如下
import cv2 as cv import numpy as np import matplotlib.pyplot as plt # 二值圖像就是將灰度圖轉(zhuǎn)化成黑白圖,沒有灰,在一個值之前為黑,之后為白 # 有全局和局部兩種 # 在使用全局閾值時,我們就是隨便給了一個數(shù)來做閾值,那我們怎么知道我們選取的這個數(shù)的好壞呢?答案就是不停的嘗試。 # 如果是一副雙峰圖像(簡 單來說雙峰圖像是指圖像直方圖中存在兩個峰)呢? # 我們豈不是應該在兩個峰之間的峰谷選一個值作為閾值?這就是 Otsu 二值化要做的。 # 簡單來說就是對 一副雙峰圖像自動根據(jù)其直方圖計算出一個閾值。 # (對于非雙峰圖像,這種方法 得到的結果可能會不理想)。 def threshold_demo(image): gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) # 這個函數(shù)的第一個參數(shù)就是原圖像,原圖像應該是灰度圖。 # 第二個參數(shù)就是用來對像素值進行分類的閾值。 # 第三個參數(shù)就是當像素值高于(有時是小于)閾值時應該被賦予的新的像素值 # 第四個參數(shù)來決定閾值方法,見threshold_simple() # ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY) ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) print("threshold value: %s"%ret) cv.imshow("threshold_demo", binary) def threshold_simple(image): img = cv.cvtColor(image, cv.COLOR_BGR2GRAY) ret, thresh1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY) ret, thresh2 = cv.threshold(img, 127, 255, cv.THRESH_BINARY_INV) ret, thresh3 = cv.threshold(img, 127, 255, cv.THRESH_TRUNC) ret, thresh4 = cv.threshold(img, 127, 255, cv.THRESH_TOZERO) ret, thresh5 = cv.threshold(img, 127, 255, cv.THRESH_TOZERO_INV) titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV'] images = [img, thresh1, thresh2, thresh3, thresh4, thresh5] for i in range(6): plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray') # 將圖像按2x3鋪開 plt.title(titles[i]) plt.xticks([]), plt.yticks([]) plt.show() # 在前面的部分我們使用是全局閾值,整幅圖像采用同一個數(shù)作為閾值。 # 當時這種方法并不適應與所有情況,尤其是當同一幅圖像上的不同部分的具有不同亮度時。 # 這種情況下我們需要采用自適應閾值。此時的閾值是根據(jù)圖像上的 每一個小區(qū)域計算與其對應的閾值。 # 因此在同一幅圖像上的不同區(qū)域采用的是不同的閾值,從而使我們能在亮度不同的情況下得到更好的結果。 # 這種方法需要我們指定三個參數(shù),返回值只有一個 # _MEAN_C:閾值取自相鄰區(qū)域的平均值,_GAUSSIAN_C:閾值取值相鄰區(qū)域 的加權和,權重為一個高斯窗口。 # Block Size - 鄰域大?。ㄓ脕碛嬎汩撝档膮^(qū)域大小)。 # C - 這就是是一個常數(shù),閾值就等于的平均值或者加權平均值減去這個常數(shù)。 def threshold_adaptive(image): img = cv.cvtColor(image, cv.COLOR_BGR2GRAY) # 中值濾波 img = cv.medianBlur(img,5) ret, th1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY) # 11 為 Block size, 2 為 C 值 th2 = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 11, 2) th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2) titles = ['Original Image', 'Global Threshold (v = 127)', 'Adaptive Mean Threshold', 'Adaptive Gaussian Threshold'] images = [img, th1, th2, th3] for i in range(4): plt.subplot(2, 2, i + 1), plt.imshow(images[i], 'gray') plt.title(titles[i]) plt.xticks([]), plt.yticks([]) plt.show() def threshold_custom(image): gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) h, w = gray.shape[:2] m = np.reshape(gray, [1, w*h]) mean = m.sum() / (w*h) # 求出整個灰度圖像的平均值 print("mean:", mean) ret, binary = cv.threshold(gray, mean, 255, cv.THRESH_BINARY) cv.imshow("threshold_custom", binary) # 將大圖片拆分成小圖片后再用自適應局部閾值比較好 def big_image_demo(image): print(image.shape) cw = 200 ch = 200 h, w = image.shape[:2] gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) cv.imshow("big_image_demo_gray", gray) # 將一張圖片每隔ch * cw分成一份 for row in range(0, h, ch): for col in range(0, w, cw): roi = gray[row:row+ch, col:col+cw] dst = cv.adaptiveThreshold(roi, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 127, 2) gray[row:row + ch, col:col + cw] = dst print(np.std(dst), np.mean(dst)) cv.imwrite("../images/result_big_image.png", gray) def main(): img = cv.imread("../images/02.jpg") # threshold_demo(img) # threshold_simple(img) # threshold_adaptive(img) # threshold_custom(img) src = cv.imread("../images/big_image.jpg") big_image_demo(src) cv.waitKey(0) # 等有鍵輸入或者1000ms后自動將窗口消除,0表示只用鍵輸入結束窗口 cv.destroyAllWindows() # 關閉所有窗口 if __name__ == '__main__': main()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python使用日志模塊快速調(diào)試代碼并記錄異常信息
本文詳細介紹了Python logging日志模塊的使用方法,包括如何在代碼中使用logging記錄調(diào)試信息、如何設置日志級別、如何記錄異常信息等。通過本文的指南,讀者可以快速學會如何使用logging模塊進行調(diào)試,并保留有用的日志信息,便于后續(xù)排查問題和優(yōu)化代碼2023-04-04詳解Django中 render() 函數(shù)的使用方法
這篇文章主要介紹了Django中 render() 函數(shù)的使用方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04Python爬取用戶觀影數(shù)據(jù)并分析用戶與電影之間的隱藏信息!
看電影前很多人都喜歡去 『豆瓣』 看影評,所以我爬取44130條 『豆瓣』 的用戶觀影數(shù)據(jù),分析用戶之間的關系,電影之間的聯(lián)系,以及用戶和電影之間的隱藏關系,需要的朋友可以參考下2021-06-06