Ask Your Question
0

How do i create and use a custom kernel with cv.filter2D

asked Jan 15 '0

updated Jan 17 '0

berak gravatar image

I'm developing a website for a university project.

I need to apply custom kernel to a image, i can do this very easily with python but I am struggling to do it in javascript

gray = cv2 . cvtColor(image, cv2 . COLOR_RGB2GRAY)
kernel = np.array ([
    [ -1 ,0 ,1] ,
    [ -2 ,0 ,2] ,
    [ -1 ,0 ,1] ])

cv2 . filter2D (gray, -1,   kernel_vertical)

I want to do something like this but in javascript.

I started by taking a look at these exemples: https://docs.opencv.org/master/dd/d6a...

but they don't show to to use a custom kernel, can anyone help?

Preview: (hide)

1 answer

Sort by » oldest newest most voted
0

answered Jan 17 '0

berak gravatar image

updated Jan 17 '0

please take another look at the tutorials, you can use cv.matFromArray() to create your kernel, and then:

let src = cv.imread('canvasInput'); // lena
let gray = new cv.Mat();
let dst = new cv.Mat();

cv.cvtColor(src, gray, cv.COLOR_RGB2GRAY);

let kernel = cv.matFromArray(3,3,cv.CV_32FC1, [-1, 0, 1, -2 , 0, 2, -1 ,0 ,1]);
cv.filter2D(gray, dst, -1, kernel);

cv.imshow('canvasOutput', dst);

src.delete();
gray.delete();
dst.delete();

image description

Preview: (hide)

Question Tools

1 follower

Stats

Asked: Jan 15 '0

Seen: 3,107 times

Last updated: Jan 17 '20