国产一区二区精品-国产一区二区精品久-国产一区二区精品久久-国产一区二区精品久久91-免费毛片播放-免费毛片基地

千鋒教育-做有情懷、有良心、有品質的職業教育機構

手機站
千鋒教育

千鋒學習站 | 隨時隨地免費學

千鋒教育

掃一掃進入千鋒手機站

領取全套視頻
千鋒教育

關注千鋒學習站小程序
隨時隨地免費學習課程

當前位置:首頁  >  技術干貨  > python圖像二值化處理

python圖像二值化處理

來源:千鋒教育
發布人:qyf
時間: 2022-08-17 17:38:00 1660729080

python圖像二值化處理

  一、圖像二值化

  圖像二值化是指將圖像上像素點的灰度值設定為0或255,即整個圖像呈現明顯的黑白效果的過程。

  二、python圖像二值化處理

  1.opencv簡單閾值cv2.threshold

  2.opencv自適應閾值cv2.adaptiveThreshold

  有兩種方法可用于計算自適應閾值: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

  # 閾值一定要設為 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培訓”的問題,歡迎咨詢千鋒教育在線名師。千鋒教育多年辦學,課程大綱緊跟企業需求,更科學更嚴謹,每年培養泛IT人才近2萬人。不論你是零基礎還是想提升,都可以找到適合的班型,千鋒教育隨時歡迎你來試聽。

tags:
聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。
10年以上業內強師集結,手把手帶你蛻變精英
請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
免費領取
今日已有369人領取成功
劉同學 138****2860 剛剛成功領取
王同學 131****2015 剛剛成功領取
張同學 133****4652 剛剛成功領取
李同學 135****8607 剛剛成功領取
楊同學 132****5667 剛剛成功領取
岳同學 134****6652 剛剛成功領取
梁同學 157****2950 剛剛成功領取
劉同學 189****1015 剛剛成功領取
張同學 155****4678 剛剛成功領取
鄒同學 139****2907 剛剛成功領取
董同學 138****2867 剛剛成功領取
周同學 136****3602 剛剛成功領取
相關推薦HOT