1.高通滤波器是根据像素与邻近像素的亮度差值来提升该像素的亮度。
import cv2
import numpy as np
from scipy import ndimage
kernel_3_3 =np.array([[-1,-1,-1],
[-1,8,-1],
[-1,-1,-1]])
print(kernel_3_3)
kernel_5_5 =np.array([[-1,-1,-1,-1,-1],
[-1,1,2,1,-1],
[-1,2,4,2,-1],
[-1,1,2,1,-1],
[-1,-1,-1,-1,-1]])
img =cv2.imread("x.jpg",0)
k3=ndimage.convolve(img,kernel_3_3)
print(k3)
k5=ndimage.convolve(img,kernel_5_5)
blurred=cv2.GaussianBlur(img,(11,11),0)
g_hpf=img - blurred
cv2.imshow("3*3",k3)
cv2.imshow("5*5",k5)
cv2.imshow("g_hpf",g_hpf)
cv2.waitKey()
cv2.destroyAllWindows()
确实容易看出,第三种效果最好。
2.
import cv2
import numpy as np
from scipy import ndimage
blurKsize=7
edgeKsize=5
src=cv2.imread("x.jpg")
#模糊函数,对去除数字化的视频噪声很有效,尤其是彩色图像的噪声
blurredSrc=cv2.medianBlur(src,blurKsize)
cv2.imshow('blurredSrc',blurredSrc)
cv2.waitKey(0)
#彩色图转灰度图
graySrc=cv2.cvtColor(blurredSrc,cv2.COLOR_BGR2GRAY)
cv2.imshow('graySrc',graySrc)
cv2.waitKey(0)
#边缘检测函数,会产生明显的边缘线条
cv2.Laplacian(graySrc,cv2.CV_8U,graySrc,edgeKsize)
cv2.imshow('LapSrc',graySrc)
cv2.waitKey(0)
#黑转白,白转黑
normalizedInverseAlpha =(1.0/255)*(255 - graySrc)
cv2.imshow('normalizedSrc',normalizedInverseAlpha)
cv2.waitKey(0)
#重新恢复彩色,实现更清晰的轮廓图
channels=cv2.split(src)
for channel in channels:
channel[:]=channel*normalizedInverseAlpha
dst=src.copy()
cv2.merge(channels,dst)
cv2.imshow('dst',dst)
cv2.waitKey(0)
使用medianBlur()作为模糊函数,它对去除数字化的视频噪声非常有效。
从BGR色彩空间转灰度色彩空间
使用Laplacian()作为边缘检测函数,它会产生明显的边缘线条
转化为黑色边缘和白色背景的图像
归一化:
3.(1)锐化
import cv2
import numpy as np
src=cv2.imread("x.jpg")
kernel=np.array([[-1,-1,-1],
[-1,9,-1],
[-1,-1,-1]])
dst=src.copy()
cv2.filter2D(src,-1,kernel,dst)
cv2.imshow("pic",dst)
cv2.waitKey(0)
kernel=np.array([[-1,-1,-1],
[-1,9,-1],
[-1,-1,-1]])如果感兴趣的像素已经与其邻近的像素有一点差别,那么这个差别会增加。
这样会让图像锐化。
filter2D()运用由用户指定的任意核或卷积矩阵。
(2)边缘检测
kernel=np.array([[-1,-1,-1], [-1,8,-1], [-1,-1,-1]])此时为边缘检测核(权重加起来为0,把边缘转为白色,把非边缘区域转为黑色)
(3)模糊效果
kernel=np.array([[0.04,0.04,0.04,0.04,0.04],
[0.04,0.04,0.04,0.04,0.04],
[0.04,0.04,0.04,0.04,0.04],
[0.04,0.04,0.04,0.04,0.04],
[0.04,0.04,0.04,0.04,0.04]
])
通常权重为1,邻近像素的权重全为正。
(4)模糊加锐化(产生脊状或浮雕效果)
kernel=np.array([[-2,-1,0],
[-1,1,1],
[0,1,2]])