Ask Your Question
0

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

asked 2020-01-14 19:17:59 -0600

updated 2020-01-17 05:30:27 -0600

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?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2020-01-17 04:49:40 -0600

berak gravatar image

updated 2020-01-17 04:50:48 -0600

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

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-01-14 19:17:59 -0600

Seen: 2,289 times

Last updated: Jan 17 '20