1、(inRange)色彩空间转换:
2、模糊(卷积操作):
2.1、均值模糊:
# 均值模糊(可以去住随机噪声)
def blur_demo(image):
dst = cv.blur(image, (5, 5)) # 第一个值是x方向,第二个值是y方向
cv.imshow("blur_demo", dst)
2.2、中值模糊:
# 中值模糊(可以去除椒盐噪声)
def median_blur(image):
dst = cv.medianBlur(image, 5)
cv.imshow("median_blur", dst)
2.3、自定义卷积核产生的模糊:
# 自定义卷积核产生的模糊
def custom_blur_demo(image):
kernal = np.ones([5, 5], np.float32) / 25
dst = cv.filter2D(image, -1, kernel=kernal)
cv.imshow("custom_blur_demo", dst)
2.4.1、高斯噪声(使图片产生古老的效果):
# 高斯模糊(图片产生古老的效果)
def gaussian_noise(image):
h, w, c = image.shape
for row in range(h):
for col in range(w):
s = np.random.normal(0, 20, 3)
b = image[row, col, 0] # blue
g = image[row, col, 1] # green
r = image[row, col, 2] # red
image[row, col, 0] = clamp(b + s[0])
image[row, col, 1] = clamp(g + s[1])
image[row, col, 2] = clamp(r + s[2])
cv.imshow("gaussian_noise", image)
2.4.2、高斯模糊(高斯模糊对高斯噪声有一定的抑制作用):
dst = cv.GaussianBlur(src, (0, 0), 5)
cv.imshow("gaussion_noise", dst)
通过对原图进行高斯噪声后产生的图像:
通过对高斯噪声图像进行高斯模糊后形成的图像:
3、边缘保留滤波(EPF):
3.1、高斯双边模糊:
def bi_demo(image):
dst = cv.bilateralFilter(image, 0, 100, 15)
cv.imshow("bi_demo", dst)
3.2、均值迁移模糊:
def shift_demo(image):
dst = cv.pyrMeanShiftFiltering(image, 10, 50)
cv.imshow("shift_demo", dst)
4、直方图(hist):
4.1、用matplotlib.pyplot:
def plot_demo(image):
plt.hist(image.ravel(), 256, [0, 256])# ravel表示统计的频率数
plt.show("直方图")
4.2、用Opencv:
def image_hist(image):
color = ('blue', 'green', 'red')
for i, color in enumerate(color):
hist = cv.calcHist([image], [i], None, [256], [0, 256])
plt.plot(hist, color=color)
plt.xlim([0, 256])
plt.show()
5、直方图应用:
5.1、全局直方图均衡化(可以图像增强,提高图像对比度):
def equal_Hist(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)#直方图均衡化只能用于灰度图
dst = cv.equalizeHist(gray)
cv.imshow("equal_Hist", dst
5.2、自适应(局部)直方图均衡化(相比全局,可以防止图像过度增强);
def clahe_Hist(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
clahe = cv.createCLAHE(clipLimit=5.0, tileGridSize=(8, 8))
dst = clahe.apply(gray)
cv.imshow("clahe_Hist", dst)
6、直方图反向投影技术(HSV色彩空间图像):
6.1、使用cv.imshow显示2d直方图代码:
def hist2d_demo(image):
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
hist = cv.calcHist([image], [0, 1], None, [180, 256], [0, 180, 0, 256])
cv.imshow("hist2d", hist)
**显示效果为黑白色。
6.2、使用matplotlib.pyplot显示的2d直方图代码:
def hist2d_demo(image):
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
hist = cv.calcHist([image], [0, 1], None, [180, 256], [0, 180, 0, 256])
# cv.imshow("hist2d", hist)
plt.imshow(hist, interpolation="nearest")
plt.title("2D Histogram")
plt.show()
6.3、反向投影:
**效果: