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

<p>For example:</p>

<pre><code>kernel = np.array([[-1, -1, -1],
                   [-1,  4, -1],
                   [-1, -1, -1]])
</code></pre>

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

<p>As far as applying a custom kernel to a given image you may simply use <a href="http://docs.opencv.org/3.1.0/d4/d13/tutorial_py_filtering.html" rel="nofollow">filter2D</a> 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:</p>

<pre><code>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)
</code></pre>

<p>Input image:</p>

<p><a href="http://i.stack.imgur.com/D03Bq.png" rel="nofollow"><img src="http://i.stack.imgur.com/D03Bq.png" alt="enter image description here"></a></p>

<p>Output Image:</p>

<p><a href="http://i.stack.imgur.com/wQ2u8.png" rel="nofollow"><img src="http://i.stack.imgur.com/wQ2u8.png" alt="enter image description here"></a></p>

python python-2.7 opencv