如何使用python2.7中的cv2制作新的滤镜并将其应用于图像? [英] How to make a new filter and apply it on an image using cv2 in python2.7?

查看:175
本文介绍了如何使用python2.7中的cv2制作新的滤镜并将其应用于图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在python2.7中使用cv2制作新的滤镜并将其应用于图像?

例如:

kernel = np.array([[-1, -1, -1],
                   [-1,  4, -1],
                   [-1, -1, -1]])

我是opencv的新手,因此,如果您能解释一下,那就太好了.谢谢!

解决方案

只要将自定义内核应用于给定图像,您就可以简单地使用

输入图片:

输出图像:

编辑:根据 @Dolphin 建议的修改,请使用带有center的内核值为8时,在使用圆形二进制光盘的情况下将获得良好的结果.

How to make a new filter and apply it on an image using cv2 in python2.7?

For example:

kernel = np.array([[-1, -1, -1],
                   [-1,  4, -1],
                   [-1, -1, -1]])

i'm new to opencv so if you can explain that'd be great. thanks!

解决方案

As far as applying a custom kernel to a given image you may simply use filter2D method to feed in a custom filter. You may also copy the following code to get you going. But The results with current filter seem a bit weird:

import cv2
import numpy as np

# Create a dummy input image.
canvas = np.zeros((100, 100), dtype=np.uint8)
canvas = cv2.circle(canvas, (50, 50), 20, (255,), -1)

kernel = np.array([[-1, -1, -1],
                   [-1, 4, -1],
                   [-1, -1, -1]])

dst = cv2.filter2D(canvas, -1, kernel)
cv2.imwrite("./filtered.png", dst)

Input image:

Output Image:

EDIT: As per the edits suggested by @Dolphin, using a kernel with center value of 8, would get the good results in case of circular binary disc.

这篇关于如何使用python2.7中的cv2制作新的滤镜并将其应用于图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆