#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How To?: Sobel Filter on image

edited December 2017

I searched a lot but didn't find anything about the sobel filter for Processing. My task is to use the sobel filter on a normal colored image. I think I understood the sobel filter method, but I have absolutely no clue how to set that in code.

I can use the 2 kernels for 2 direction and iterate them over the whole image, to detect the pixels, which are inbetween e.g. a black and a white pixel. I can calculate the gradient magnitude with these results. And it's also possible to calculate the gradient's direction (But I think that's unnecessary for me).

So now I have these kernels for the x- and the y-direction...

float filterx[][] = {
{-1, 0, 1},
{-2, 0, 2},
{-1, 0, 1}};

float filtery[][] = {
{-1, -2, -1},
{0, 0, 0},
{1, 2, 1}};

and now I should iterate them over the whole image, with 2 for-loops(?)

and with the following one, I can calculate the gradient magnitude.

float gradientengroeße = sqrt(pow(gx, 2), pow(gy, 2));

But I have to get gx and gy somehow. And do I have to do these steps 3 times (for red, green, blue)? Or how should I start with that? And how do I iterate a two-dimensional Array over the image? I'm super confused, can someone help me :/

Tagged:

Run Processing, then go to File>>Examples then go to Topics>>Image Processing>>Convolution or EdgeDetection and you can see an example of how to apply a kernel to the whole image. If you want to work with single colors as in the RGB case, you should use bit shifting. You need to check the reference to understand their definitions:

https://processing.org/reference/color_datatype.html

https://processing.org/reference/rightshift.html

I will suggest you focus only on one kernel before you implement the two together. A third step is to work with your formula:

float gradientengroeße = sqrt(pow(gx, 2), pow(gy, 2));

It is worth to ask, gx is the result of applying the kernel on the X direction? I prefer not to assume but to have clear from the beginning what both gx and gy are.

Give it a try and implement step 1 following the example above and if you run into problems, post your code.

Kf

• There is also a nice tutorial in image and pixels

• For high performance it is worth looking at GLSL filters eg https://github.com/SableRaf/Filters4Processing/tree/master/sketches/EdgeDetection then you can do video filtering.