一.阈值处理
自适应阈值运算
athdMEAN = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,5,3)
athdGAUS = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_FAUSSIAN_C,cv.THRESH_BINARY,5,3)
cv.imshow("athMEAN",athdMEAN)
cv.imshow("athGAUS",athdGAUS)
Otsu阈值运算
t,otsu=cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
cv.imshow("ost",otsu)
cv.waitKey(0)
cv.destroyAllWindows()
二.使用滑块实现阈值类型与大小调整
Type=0
Value=0
def onType(a):
Type=cv2.getTrackbarPos(tType,windowName)
Value = cv2.getTrackbarPos(tValue,windowName)
ret,dst = cv2.threshold(img,Value,255,Type)
def onValue(a):
Type=cv2.getTrackbarPos(tType,windowName)
Value=cv2.getTrackbarPos(tValue,windowName)
ret,dst = cv2.threshold(img,Value,255,Type)
cv2.immshow(windowName,dst)
img = cv2.imread("peppa.jpg",0)
windowName = "Pappa"
cv2.nameWindow(windowName)
cv2.imshow(windowName,img)
tType = "Type"
tType = "Value"\
cv2.createTrackbar(tType,windowName,0,4,onType)
cv2.createTrackbar(tValue,windowName,0,255,onValue)
cv2.waitKey()
cv2.destroyAllWindows()
三.平滑处理
img = cv2.imread("peppa_gaussian.jpg")
blur = cv2.blur(img,(7,7))
box = cv2.boxFilter(img,-1,(7,7),normalize=True)
gaussion = cv2.GaussianBlur(img,(7,7),10)
median = cv2.medianBlur(img,7)
bilater = cv2.bilateralFilter(img,9,75,75)
kernel = np.array((
[-2,-1,0],
[-1,1,1],
[0,1,2]),dtype=""loat32)
filter2D=cv2.filter2D(img,-1,kernel)
cv2.imshow('img',img)
cv2.imshow('blur',blur)
cv2.imshow('box',box)
cv2.imshow('gaussian',gaussian)
cv2.imshow('median',median)
cv2.imshow('bilater',bilater)
cv2.imshow('filter2D',filter2D)
cv2.waitKey()
cv2.destroyAllWindows()