在medium上看到一篇介绍opencv滤镜效果的博客,这里做一下记录和分享.
卷积操作
高斯模糊
pip3 install opencv-python
pip3 install scipy
def gaussianBlur(image):
return cv2.GaussianBlur(image, (35, 35), 0)
initialImage = cv2.imread("image1.jpg")
blurredImage = gaussianBlur(copy.deepcopy(initialImage))
cv2.imwrite("blurred.jpg", blurredImage)
锐化
def sharpen(image):
kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
return cv2.filter2D(image, -1, kernel)
Sepia
def sepia(image):
kernel = np.array([[0.272, 0.534, 0.131],
[0.349, 0.686, 0.168],
[0.393, 0.769, 0.189]])
return cv2.filter2D(image, -1, kernel)
emboss
def emboss(image):
kernel = np.array([[0,-1,-1],
[1,0,-1],
[1,1,0]])
return cv2.filter2D(image, -1, kernel)
像素操作
def brightnessControl(image, level):
return cv2.convertScaleAbs(image, beta=level)
查找表操作
def spreadLookupTable(x, y):
spline = UnivariateSpline(x, y)
return spline(range(256))
def warmImage(image):
increaseLookupTable = spreadLookupTable([0, 64, 128, 256], [0, 80, 160, 256])
decreaseLookupTable = spreadLookupTable([0, 64, 128, 256], [0, 50, 100, 256])
red_channel, green_channel, blue_channel = cv2.split(image)
red_channel = cv2.LUT(red_channel, increaseLookupTable).astype(np.uint8)
blue_channel = cv2.LUT(blue_channel, decreaseLookupTable).astype(np.uint8)
return cv2.merge((red_channel, green_channel, blue_channel))
def coldImage(image):
increaseLookupTable = spreadLookupTable([0, 64, 128, 256], [0, 80, 160, 256])
decreaseLookupTable = spreadLookupTable([0, 64, 128, 256], [0, 50, 100, 256])
red_channel, green_channel, blue_channel = cv2.split(image)
red_channel = cv2.LUT(red_channel, decreaseLookupTable).astype(np.uint8)
blue_channel = cv2.LUT(blue_channel, increaseLookupTable).astype(np.uint8)
return cv2.merge((red_channel, green_channel, blue_channel))
暖色调效果
冷色调