Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 758
red5pider Avatar asked Sep 25 '16 10:09

red5pider


1 Answers

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:

enter image description here

Output Image:

enter image description here

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.

like image 145
ZdaR Avatar answered Nov 14 '22 21:11

ZdaR