一,图像阈值
1,cv2.cvtColor(p1,p2) 是颜色空间转换函数,p1是需要转换的图片,p2是转换成何种格式。
cv2.COLOR_BGR2RGB 将BGR格式转换成RGB格式
.0cv2.COLOR_BGR2GRAY 将BGR格式转换成灰度图片
#图像阈值
import cv2 #opencv读取的格式是BGR
import numpy as np
import matplotlib.pyplot as plt#Matplotlib是RGB
%matplotlib inline
img=cv2.imread('cat.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_gray.shape
cv2.imshow("img_gray", img_gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
"""
HSV
•H - 色调(主波长)。
•S - 饱和度(纯度/颜色的阴影)。
•V值(强度)
"""
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow("hsv", hsv)
cv2.waitKey(0)
cv2.destroyAllWindows()
2,ret, dst = cv2.threshold(src, thresh, maxval, type)
•src: 输入图,只能输入单通道图像,通常来说为灰度图
•dst: 输出图
•thresh: 阈值
•maxval: 当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值
•type:二值化操作的类型,包含以下5种类型
•cv2.THRESH_BINARY 超过阈值部分取maxval(最大值),小于阈值取0
•cv2.THRESH_BINARY_INV THRESH_BINARY的反转
•cv2.THRESH_TRUNC 大于阈值部分设为阈值,小于部分不变
•cv2.THRESH_TOZERO 大于阈值部分不改变,小于部分为0
•cv2.THRESH_TOZERO_INV THRESH_TOZERO的反转
ret, thresh1 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO_INV)
titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
二、图像平滑
img = cv2.imread('lenaNoise.png')
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 均值滤波
# 简单的平均卷积操作
blur = cv2.blur(img, (3, 3))
cv2.imshow('blur', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 方框滤波
#可以选择归一化,归一化后与和均值滤波一样
box = cv2.boxFilter(img,-1,(3,3), normalize=True)
cv2.imshow('box', box)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 方框滤波
# 不选择归一化,容易越界(像素值相加后大于255),越界后像素值取255,变成白色
box = cv2.boxFilter(img,-1,(3,3), normalize=False)
cv2.imshow('box', box)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 高斯滤波
# 高斯模糊的卷积核里的数值是满足高斯分布,相当于更重视中间的
aussian = cv2.GaussianBlur(img, (5, 5), 1)
cv2.imshow('aussian', aussian)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 中值滤波
# 从大到小或者从小到大取中值,相当于用中值代替
median = cv2.medianBlur(img, 5)
cv2.imshow('median', median)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 展示所有的
res = np.hstack((blur,aussian,median)) #横向展示
#res = np.vstack((blur,aussian,median)) #垂直展示
#print (res)
cv2.imshow('median vs average', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
三、形态变化
腐蚀与膨胀都是针对图像的高亮区域,而不是黑色部分;膨胀使高亮区域被扩展,邻域扩张,使效果图拥有更多的高亮区域;腐蚀使高亮部分被蚕食,邻域被蚕食,使效果图拥有更小的高亮区域
1,腐蚀
cv2.erode(src, kernel, iteration)
参数说明:src表示的是输入图片,kernel表示的是方框的大小,iteration表示迭代的次数
iteration越大,模糊程度越高,即腐蚀程度越高,正相关
#腐蚀操作原理:像水果腐蚀一样侵蚀了前景物体的边界,存在一个kernel,比如(3, 3),在图像中不断的平移,在这个9方框中,哪一种颜色所占的比重大,9个方格中将都是这种颜色
使用场景:图像腐蚀加上高斯模糊 就可以使得图像的色彩更加突出,可以是色彩追踪更加精准,少了很多的颜色干扰
#二值化图片进行腐蚀操作
img = cv2.imread('dige.png')
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#去掉毛刺
kernel = np.ones((3,3),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)
cv2.imshow('erosion', erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
#不同iterations迭代次数变化的影响
pie = cv2.imread('pie.png')
cv2.imshow('pie', pie)
cv2.waitKey(0)
cv2.destroyAllWindows()
kernel = np.ones((30,30),np.uint8)
erosion_1 = cv2.erode(pie,kernel,iterations = 1)
erosion_2 = cv2.erode(pie,kernel,iterations = 2)
erosion_3 = cv2.erode(pie,kernel,iterations = 3)
res = np.hstack((erosion_1,erosion_2,erosion_3))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
2,膨胀操作,取得局部最大值,效果是变胖
img = cv2.imread('dige.png')
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#膨胀字体变胖
kernel = np.ones((3,3),np.uint8)
dige_erosion = cv2.dilate(img,kernel,iterations = 1)
cv2.imshow('erosion', erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
#不同iterations的影响
pie = cv2.imread('pie.png')
kernel = np.ones((30,30),np.uint8)
dilate_1 = cv2.dilate(pie,kernel,iterations = 1)
dilate_2 = cv2.dilate(pie,kernel,iterations = 2)
dilate_3 = cv2.dilate(pie,kernel,iterations = 3)
res = np.hstack((dilate_1,dilate_2,dilate_3))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
3,开运算与闭运算
"""
cv2.morphologyEx(src, op, kernel) 进行各类形态学的变化
参数说明:
1,src传入的图片
2,op进行变化的方式
cv2.MORPH_OPEN 进行开运算,指的是先进行腐蚀操作,再进行膨胀操作
cv2.MORPH_CLOSE 进行闭运算, 指的是先进行膨胀操作,再进行腐蚀操作
开运算:表示的是先进行腐蚀,再进行膨胀操作
闭运算:表示先进行膨胀操作,再进行腐蚀操作
3,kernel表示方框的大小
"""
# 开:先腐蚀,再膨胀(去掉毛刺)
img = cv2.imread('dige.png')
kernel = np.ones((5,5),np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
cv2.imshow('opening', opening)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 闭:先膨胀,再腐蚀(毛刺字体都保留且变大)
img = cv2.imread('dige.png')
kernel = np.ones((5,5),np.uint8)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
cv2.imshow('closing', closing)
cv2.waitKey(0)
cv2.destroyAllWindows()
4,梯度运算
#梯度=膨胀-腐蚀
pie = cv2.imread('pie.png')
kernel = np.ones((7,7),np.uint8)
dilate = cv2.dilate(pie,kernel,iterations = 5)
erosion = cv2.erode(pie,kernel,iterations = 5)
res = np.hstack((dilate,erosion))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
#留下圆框
gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel)
cv2.imshow('gradient', gradient)
cv2.waitKey(0)
cv2.destroyAllWindows()
5,礼帽与黑帽
•礼帽 = 原始输入-开运算结果
•黑帽 = 闭运算-原始输入
#礼帽(只留下毛刺)
img = cv2.imread('dige.png')
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
cv2.imshow('tophat', tophat)
cv2.waitKey(0)
cv2.destroyAllWindows()
#黑帽(只留下框架)
img = cv2.imread('dige.png')
blackhat = cv2.morphologyEx(img,cv2.MORPH_BLACKHAT, kernel)
cv2.imshow('blackhat ', blackhat )
cv2.waitKey(0)
cv2.destroyAllWindows()