© Fu Xianjun. All Rights Reserved.
阈值处理指挑剔图像内像素值高于一定或者低于一定值的像素点。
1.阈值处理
语法格式:
src: 输入图,只能输入单通道图像,通常来说为灰度图
dst: 输出图
thresh: 阈值
maxval: 当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值
type:二值化操作的类型,包含以下5种类型: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO;cv2.THRESH_TOZERO_INV
cv2.THRESH_BINARY 超过阈值部分取maxval(最大值),否则取0
cv2.THRESH_BINARY_INV THRESH_BINARY的反转
cv2.THRESH_TRUNC 大于阈值部分设为阈值,否则不变
cv2.THRESH_TOZERO 大于阈值部分不改变,否则设为0
cv2.THRESH_TOZERO_INV THRESH_TOZERO的反转
代码如下:
import cv2
import numpy as np
#from matplotlib import pyplot as plt
peppa = cv2.imread('peppa.jpg')
img=cv2.cvtColor(peppa,cv2.COLOR_BGR2GRAY)
cv2.imshow('Peppa',img)
ret,thresh1 = cv2.threshold(img,200,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(img,200,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img,200,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img,200,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img,200,255,cv2.THRESH_TOZERO_INV)
cv2.imshow('BINARY',thresh1)
cv2.imshow('BINARY_INV',thresh2)
# cv2.imshow('TRUNC',thresh3)
# cv2.imshow('TOZERO',thresh4)
# cv2.imshow('TOZERO_INV',thresh5)
peppa_body=cv2.bitwise_and(peppa,peppa,mask=thresh2)
cv2.imshow('peppa_body',peppa_body)
cv2.waitKey()
cv2.destroyAllWindows()
2.中值滤波的滑块处理
代码如下:
import cv2
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)
cv2.imshow(windowName,dst)
def onValue(a):
Type= cv2.getTrackbarPos(tType, windowName)
Value= cv2.getTrackbarPos(tValue, windowName)
ret, dst = cv2.threshold(img, Value, 255, Type)
cv2.imshow(windowName,dst)
img = cv2.imread("peppa.jpg",0)
windowName = "Peppa" #窗体名
cv2.namedWindow(windowName)
cv2.imshow(windowName,img)
#创建两个滑动条
tType = "Type" #用来选取阈值处理类型的滚动条
tValue = "Value" #用来选取阈值的滚动条
cv2.createTrackbar(tType, windowName, 0, 4, onType)
cv2.createTrackbar(tValue, windowName,0, 255, onValue)
cv2.waitKey()
cv2.destroyAllWindows()
语法格式:
cv2.adaptiveThreshold()
代码如下(示例):
img=cv2.imread('peppa.jpg',0)
athdMEAN=cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,7,5)
athdGAUS=cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,5,3)
cv2.imshow("athMEAN",athdMEAN)
cv2.imshow("athGAUS",athdGAUS)
cv2.waitKey(0)
cv2.destroyAllWindows()
1.模糊处理
代码如下(示例):
import cv2
import numpy as np
img = cv2.imread("peppa_gaussian.jpg")
blur = cv2.blur(img, (7, 7))
box = cv2.boxFilter(img,-1,(7,7), normalize=True)
gaussian = 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="float32")
filter2D=cv2.filter2D(img,-1,kernel)#https://my.oschina.net/u/4306156/blog/3598055
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()
阈值处理主要利用图像中要提取的目标物与其背景在灰度特性上的差异,确定图像中每个像素点属于背景还是目标区域,从而产生相应的二度图像。