• <del id="a8uas"></del>
    • 千鋒教育-做有情懷、有良心、有品質的職業教育機構

      400-811-9990
      手機站
      千鋒教育

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

      千鋒教育

      掃一掃進入千鋒手機站

      領取全套視頻
      千鋒教育

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

      上海
      • 北京
      • 鄭州
      • 武漢
      • 成都
      • 西安
      • 沈陽
      • 廣州
      • 南京
      • 深圳
      • 大連
      • 青島
      • 杭州
      • 重慶
      當前位置:合肥千鋒IT培訓  >  技術干貨  >  python圖像二值化處理

      python圖像二值化處理

      來源:千鋒教育
      發布人:xqq
      時間: 2023-11-06 02:09:07

      一、圖像二值化

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

      二、python圖像二值化處理

      1.opencv簡單閾值cv2.threshold

      2.opencv自適應閾值cv2.adaptiveThreshold

      有兩種方法可用于計算自適應閾值:mean_c和guassian_c

      3.Otsu's二值化

      三、示例:

      importcv2

      importnumpyasnp

      frommatplotlibimportpyplotasplt

      img=cv2.imread('scratch.png',0)

      #globalthresholding

      ret1,th1=cv2.threshold(img,127,255,cv2.THRESH_BINARY)

      #Otsu'sthresholding

      th2=cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)

      #Otsu'sthresholding

      #閾值一定要設為0!

      ret3,th3=cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

      #plotalltheimagesandtheirhistograms

      images=[img,0,th1,img,0,th2,img,0,th3]

      titles=[

      'OriginalNoisyImage','Histogram','GlobalThresholding(v=127)',

      'OriginalNoisyImage','Histogram',"AdaptiveThresholding",

      'OriginalNoisyImage','Histogram',"Otsu'sThresholding"

      ]

      #這里使用了pyplot中畫直方圖的方法,plt.hist,要注意的是它的參數是一維數組

      #所以這里使用了(numpy)ravel方法,將多維數組轉換成一維,也可以使用flatten方法

      #ndarray.flat1-Diteratoroveranarray.

      #ndarray.flatten1-Darraycopyoftheelementsofanarrayinrow-majororder.

      foriinrange(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圖像二值化處理,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。

      聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。

      猜你喜歡LIKE

      pythonPython和php什么關系

      2023-11-06

      pythonint函數怎么用

      2023-11-06

      python怎么導包

      2023-11-06

      最新文章NEW

      python division如何取整

      2023-11-06

      pythonsort()報錯是什么原因

      2023-11-06

      python基礎知識

      2023-11-06

      相關推薦HOT

      更多>>

      快速通道 更多>>

      最新開班信息 更多>>

      網友熱搜 更多>>