OpenCV for python閾值處理

2021-10-01 20:51:24 字數 1606 閱讀 3918

閾值處理,主要用到函式cv2.threshhold()

示例**:

'''

this program is used to show how to use threshhold

functions in opencv for python

'''#coding:utf-8

import numpy as np

from cv2 import cv2

#此處不指定0,則按照三通道辦法讀取,造成自適應閾值無法完成

lena = cv2.imread(

'black.bmp',0

)#二值化閾值處理

t1,lena_1 = cv2.threshold(lena,

127,

255,cv2.thresh_binary)

#反向二值化閾值處理

t2,lena_2 = cv2.threshold(lena,

127,

255,cv2.thresh_binary_inv)

#截斷閾值處理

t3,lena_3 = cv2.threshold(lena,

127,

255,cv2.thresh_trunc)

#超閾值零處理

t4,lena_4 = cv2.threshold(lena,

127,

255,cv2.thresh_tozero_inv)

#低閾值零處理

t5,lena_5 = cv2.threshold(lena,

127,

255,cv2.thresh_tozero)

#自適應閾值處理

lena_6 = cv2.adaptivethreshold(lena,

255,cv2.adaptive_thresh_gaussian_c,cv2.thresh_binary,5,

3)#otus處理,dtype add the cv2.thresh_otsu item

t7,lena_7 = cv2.threshold(lena,0,

255,cv2.thresh_binary+cv2.thresh_otsu)

#show these photos

cv2.imshow(

'lena1'

,lena_1)

cv2.imshow(

'lena2'

,lena_2)

cv2.imshow(

'lena3'

,lena_3)

cv2.imshow(

'lena4'

,lena_4)

cv2.imshow(

'lena5'

,lena_5)

cv2.imshow(

'lena6'

,lena_6)

cv2.imshow(

'lena7'

,lena_7)

cv2.waitkey(

)cv2.destroyallwindows(

)

效果圖:

OpenCV for Python之色彩空間

python 3.7 opencv 4.2 所用 hsv中h,s,v的最小值 upper hsv np.array 50 255 255 hsv中的h,s,v最大值 提取指定範圍顏色,保留指定範圍顏色,其餘置為黑 0 mask cv.inrange hsv,lowerb lower hsv,uppe...

OpenCV 閾值處理 二 自適應閾值

因此在同一副影象上的不同區域採用的是不同的閾值,從而使我們能在亮度不同的情況下得到更好的結果。自適應閾值函式 dst cv.adaptivethreshold src,maxvalue,adaptivemethod,thresholdtype,blocksize,c dst 引數 src 8位單通道...

otsu閾值分割演算法原理 閾值分割 Otsu法

演算法實現 不呼叫函式 include include using namespace std using namespace cv 實現灰度直方圖的繪製 void drawpicture mat inpicture,mat outpicture ma hist const int bins 256...