Fu Xianjun. All Rights Reserved.所有素材来自于小傅老师。
如何用像素级操作,将peppa.jpg中像素值小于180的设置为黑色,大于等于180的的像素值设置为白色?
import cv2
img = cv2.imread("peppa.jpg",0)
h,w = img.shape
for i in range(h):
for j in range(w):
if img[i,j]<180:
img[i,j]=0
else:
img[i,j]=255
cv2.imshow("binary",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
我们开始今天的学习!阈值与平滑处理
目标:
能够使用相关函数实现图像阈值处理
能够根据图像选择适合的方法进行阈值处理
能够使用相关函数实现图像平滑处理
能使用滑块进行平滑处理
Otsu大津
定义:根据当前图像给出最佳的类间分割阈值。
操作:遍历所有可能阈值,从而找到最佳的阈值。
ret, dst = cv2.threshold(src, thresh, maxval, type)
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的反转
提问:如何用像素级操作,将peppa.jpg中像素值小于180的设置为黑色,大于等于180的的像素值设置为白色
import cv2
img = cv2.imread("peppa.jpg",0)
h,w = img.shape
for i in range(h):
for j in range(w):
if img[i,j]<180:
img[i,j]=0
else:
img[i,j]=255
cv2.imshow("binary",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
任务一:阈值处理
-
值运算(threshold):二值化、反二值化、截断、超阈值零处理、低阈值零处理
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)
-
自适应阈值运算(adaptiveThreshold):
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)
-
Otsu阈值运算:
-
ret,otsu=cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imshow("otsu",otsu)
cv2.waitKey(0)
cv2.destroyAllWindows()
任务二:使用滑块调整阈值大小
import cv2
Value=0 #使用的阈值
def onValue(a):
Value= cv2.getTrackbarPos(tValue, windowName)
median = cv2.medianBlur(img, 2*Value+1)
cv2.imshow(windowName,median)
img = cv2.imread("peppa.jpg",0)
windowName = "Peppa"
cv2.namedWindow(windowName)
cv2.imshow(windowName,img)
tValue = "Value"
v=cv2.createTrackbar(tValue, windowName,1, 100, 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)
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()
任务四:使用滑块调整平滑的效果
参照任务二 完成中值滤波的滑块调整
import cv2
Value=0 #使用的阈值
def onValue(a):
Value= cv2.getTrackbarPos(tValue, windowName)
median = cv2.medianBlur(img, 2*Value+1)
cv2.imshow(windowName,median)
img = cv2.imread("peppa.jpg",0)
windowName = "Peppa"
cv2.namedWindow(windowName)
cv2.imshow(windowName,img)
tValue = "Value"
v=cv2.createTrackbar(tValue, windowName,1, 100, onValue)
cv2.waitKey()
cv2.destroyAllWindows()
- 今天的学习到此结束了,今天木有任务噢!
- 下次见!拜拜!