成人免费观看网欧美片-成人免费观看视频-成人免费观看男女羞羞视频-成人免费观看的视频黄页-成人免费高清视频-成人免费福利片在线观看

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

手機站
千鋒教育

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

千鋒教育

掃一掃進入千鋒手機站

領取全套視頻
千鋒教育

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

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

python圖像二值化處理

來源:千鋒教育
發布人:xqq
時間: 2023-11-08 05:53:13 1699393993

一、圖像二值化

圖像二值化是指將圖像上像素點的灰度值設定為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培訓機構:千鋒教育。

tags: python培訓
聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。
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