OpenCV中几种常见的滤镜有如下几种:
灰度滤镜 cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
怀旧滤镜 通过调整色彩通道的权重,模拟老照片效果。
浮雕滤镜 使用卷积核 [[-2, -1, 0], [-1, 1, 1], [0, 1, 2]] 进行卷积操作。
模糊滤镜 cv2.GaussianBlur(image, (15, 15), 0)
锐化滤镜 使用卷积核 [[0, -1, 0], [-1, 5, -1], [0, -1, 0]] 进行卷积操作。
边缘检测滤镜 cv2.Canny(gray_image, 100, 200)
接下来小编将用代码实例来展示。
1、灰度滤镜
#灰度滤镜
import cv2
from opencv_jupyter_ui import cv2_imshow
# 读取图像
image = cv2.imread('./images/car.png')
# 转换为灰度图像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 保存灰度图像
# cv2.imwrite('./images/gray_output.jpg', gray_image)
# 显示灰度图像
cv2.imshow('Gray Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# cv2_imshow('Gray Image', gray_image)

2、怀旧滤镜
#怀旧滤镜
import cv2
import numpy as np
from opencv_jupyter_ui import cv2_imshow
# 读取图像
image = cv2.imread('./images/car.png')
# 分离 BGR 通道
b, g, r = cv2.split(image)
# 调整通道强度
r = np.clip(r * 0.393 + g * 0.769 + b * 0.189, 0, 255).astype(np.uint8)
g = np.clip(r * 0.349 + g * 0.686 + b * 0.168, 0, 255).astype(np.uint8)
b = np.clip(r * 0.272 + g * 0.534 + b * 0.131, 0, 255).astype(np.uint8)
# 合并通道
vintage_image = cv2.merge((b, g, r))
# 保存怀旧图像
# cv2.imwrite('vintage_output.jpg', vintage_image)
# 显示怀旧图像
# cv2.imshow('Vintage Image', vintage_image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
cv2_imshow('Vintage Image', vintage_image)

3、浮雕滤镜
#浮雕滤镜
import cv2
import numpy as np
from opencv_jupyter_ui import cv2_imshow
# 读取图像
image = cv2.imread('./images/car.png')
# 转换为灰度图像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 定义卷积核
kernel = np.array([[-2, -1, 0],
[-1, 1, 1],
[ 0, 1, 2]])
# 应用卷积核
emboss_image = cv2.filter2D(gray_image, -1, kernel)
# 保存浮雕图像
# cv2.imwrite('emboss_output.jpg', emboss_image)
# 显示浮雕图像
cv2.imshow('Emboss Image', emboss_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# cv2_imshow('Emboss Image', emboss_image)

4、模糊滤镜
#模糊滤镜
import cv2
from opencv_jupyter_ui import cv2_imshow
# 读取图像
image = cv2.imread('./images/car.png')
# 模糊滤镜
blurred_image = cv2.GaussianBlur(image, (15, 15), 0)
# 显示结果
# cv2.imshow("Blur Filter", blurred_image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
cv2_imshow("Blur Filter", blurred_image)

5、锐化滤镜
#锐化滤镜
import cv2
import numpy as np
from opencv_jupyter_ui import cv2_imshow
# 读取图像
image = cv2.imread('./images/car.png')
# 锐化滤镜
sharpen_kernel = np.array([[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]])
sharpened_image = cv2.filter2D(image, -1, sharpen_kernel)
# 显示结果
# cv2.imshow("Sharpen Filter", sharpened_image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
cv2_imshow("Sharpen Filter", sharpened_image)


全部评论