一 图像阈值处理
1. THRESH_BINAR
# 像素值超过127的变成255,否则为0,亮的更亮
ret,threshold1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
2. THRESH_BINARY_INV
# 像素值超过127的变成0,否则为255,亮的更暗
ret,threshold2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
3. THRESH_TRUNC# 像素值超过127的变成127,否则不变,理解成图片整体变暗ret,threshold3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
4. THRESH_TOZERO# 像素值超过127的不变,否则为0,理解成加大图片的对比度ret,threshold4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
5. THRESH_TOZERO_INV[url=]
[/url]
# 像素值超过127的为0,否则不变ret,threshold5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV) [url=]
[/url]
二 . 滤波
1.均值滤波(通过求与单位矩阵做内积和的平均值做图像处理)
blur = cv2.blur(img,(3,3))
2. 高斯滤波 (根据正态分布处理图像,越靠近中心点,值越接近)
blur2 = cv2.GaussianBlur(img,(3,3),1)
3. 中位值滤波(取指定大小矩阵的所有元素值的中位值处理)
三. 腐蚀与膨胀 1. 腐蚀[url=]
[/url]
img = cv2.imread("test.png")img2 = cv2.erode(img,kernel=numpy.ones((9,9),numpy.uint8),iterations=9) 和单位矩阵做处理,迭代9次,意味这腐蚀的程度cv2.imshow("IMage",numpy.hstack((img,img2)))cv2.waitKey(0) cv2.destroyAllWindows()[url=]
[/url]
2. 膨胀 (嗯,和腐蚀操作刚好相反)[url=]
[/url]
img2 =cv2.dilate(img,kernel=numpy.ones((9,9),numpy.uint8),iterations=9)cv2.imshow("IMage",numpy.hstack((img,img2)))cv2.waitKey(0) cv2.destroyAllWindows()[url=]
[/url]
3. 梯度运算(膨胀-腐蚀)
img2=cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel=numpy.ones((5,5),numpy.uint8))
cv2.imshow("IMage",img2)
4. 礼帽与黑帽[url=]
[/url]
[url=]
[/url]
更多技术资讯可关注:gzitcast