国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

opencv python如何實現(xiàn)圖像二值化

2020-02-15 21:27:24
字體:
供稿:網(wǎng)友

這篇文章主要介紹了opencv python如何實現(xiàn)圖像二值化,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

代碼如下

import cv2 as cvimport numpy as npimport matplotlib.pyplot as plt# 二值圖像就是將灰度圖轉(zhuǎn)化成黑白圖,沒有灰,在一個值之前為黑,之后為白# 有全局和局部兩種# 在使用全局閾值時,我們就是隨便給了一個數(shù)來做閾值,那我們怎么知道我們選取的這個數(shù)的好壞呢?答案就是不停的嘗試。# 如果是一副雙峰圖像(簡 單來說雙峰圖像是指圖像直方圖中存在兩個峰)呢?# 我們豈不是應該在兩個峰之間的峰谷選一個值作為閾值?這就是 Otsu 二值化要做的。# 簡單來說就是對 一副雙峰圖像自動根據(jù)其直方圖計算出一個閾值。# (對于非雙峰圖像,這種方法 得到的結(jié)果可能會不理想)。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ū)域采用的是不同的閾值,從而使我們能在亮度不同的情況下得到更好的結(jié)果。# 這種方法需要我們指定三個參數(shù),返回值只有一個# _MEAN_C:閾值取自相鄰區(qū)域的平均值,_GAUSSIAN_C:閾值取值相鄰區(qū)域 的加權(quán)和,權(quán)重為一個高斯窗口。# Block Size - 鄰域大小(用來計算閾值的區(qū)域大小)。# C - 這就是是一個常數(shù),閾值就等于的平均值或者加權(quán)平均值減去這個常數(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表示只用鍵輸入結(jié)束窗口  cv.destroyAllWindows() # 關(guān)閉所有窗口if __name__ == '__main__':  main()            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 绍兴县| 西昌市| 清镇市| 贵州省| 大英县| 塔河县| 西华县| 延津县| 邵阳市| 普定县| 安宁市| 来凤县| 庐江县| 白城市| 拜城县| 简阳市| 沁阳市| 宁夏| 张掖市| 天峨县| 汉源县| 剑川县| 安徽省| 定安县| 永平县| 基隆市| 咸宁市| 大埔县| 保山市| 芦山县| 巴塘县| 临清市| 弥勒县| 佛冈县| 新宁县| 前郭尔| 孝感市| 永定县| 肇源县| 新津县| 涟水县|