import numpy as np
import cv2
from scipy import misc, ndimage, signal
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
def plot_two(left, right):
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax1.imshow(left)
ax2.imshow(right)
plt.show()
def cv_2d_conv_lower_pass_filer(img_src):
# prepare the 5x5 shaped filter
kernel = np.array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]])
kernel = kernel/sum(kernel)
# filter the source image
img_rst = cv2.filter2D(img_src, -1, kernel)
return img_rst
def cv_2d_conv_high_pass_filter(img_src):
# edge detection filter
kernel = np.array([[0.0, -1.0, 0.0],
[-1.0, 4.0, -1.0],
[0.0, -1.0, 0.0]])
kernel = kernel//(np.sum(kernel) if np.sum(kernel) != 0 else 1)
# filter the source image
img_rst = cv2.filter2D(img_src, -1, kernel)
return img_rst
def cv_2d_conv_customer_filter(img_src):
# edge detection filter
kernel = np.array([[-1.0, -1.0],
[2.0, 2.0],
[-1.0, -1.0]])
kernel = kernel/(np.sum(kernel) if np.sum(kernel) != 0 else 1)
# filter the source image
img_rst = cv2.filter2D(img_src, -1, kernel)
return img_rst
def select_filter(image):
print(image.shape)
filters = [
{
'option': 'lcov',
'name': '2维卷积低通滤镜( filter2D )',
'filter': lambda: cv_2d_conv_lower_pass_filer(image)
},
{
'option': 'hcov',
'name': '2维卷积高通滤镜( filter2D )',
'filter': lambda: cv_2d_conv_high_pass_filter(image)
},
{
'option': 'ccov',
'name': '2维卷积自定义滤镜( filter2D )',
'filter': lambda: cv_2d_conv_customer_filter(image)
},
]
print("本迷你PS软件支持以下滤镜:")
filter_dict = {}
for filter in filters:
filter_dict[filter['option']] = filter
print("* {} : {}".format(filter['option'], filter['name']))
options = '/'.join(list(map(lambda f: f['option'], filters)))
while True:
ret = input(f"请选择滤镜[ {options}]:")
filter = filter_dict.get(ret)
if filter is None:
print("不支持的选项,请重新选择。")
else:
return filter['filter']()
if __name__ == '__main__':
image = misc.face()
blurred = select_filter(image)
plot_two(image, blurred)
不清楚这个为何不成功
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- ShowMeAI 2022-12-12 16:22关注
我测试代码是能运行和用filter处理的啊
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 ansys fluent计算闪退
- ¥15 有关wireshark抓包的问题
- ¥15 Ubuntu20.04无法连接GitHub
- ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
- ¥15 向数据表用newid方式插入GUID问题
- ¥15 multisim电路设计
- ¥20 用keil,写代码解决两个问题,用库函数
- ¥50 ID中开关量采样信号通道、以及程序流程的设计
- ¥15 U-Mamba/nnunetv2固定随机数种子
- ¥30 C++行情软件的tick数据如何高效的合成K线