
Image Kernels explained visually - phodo
http://setosa.io/ev/image-kernels/
======
kragen
This is super cool but it's unfortunate that it clamps negative result values
to black.

It's probably worth mentioning that there are a lot of ways to implement
convolution with a kernel, and the kernel can be of any size, not just 3×3.
The explanation here shows how to implement the output-side algorithm
nonrecursively;
[http://www.dspguide.com/ch6/3.htm](http://www.dspguide.com/ch6/3.htm) gives
this for the one-dimensional case. But you can implement it on the input side
instead (iterating over the input samples instead of the output samples),
there are kernels that have a much more efficient recursive implementation
(including zero-phase kernels using time-reversal), you can implement very
large kernels if you can afford to do the convolution in the frequency domain,
and there's a whole class of kernels that have efficient sparse filter cascade
representations, including large Gaussians.

(To say nothing of convolutions over other rings.)

~~~
colejohnson66
> This is super cool but it's unfortunate that it clamps negative result
> values to black.

Curious: What would a negative pixel look like? What use would that have?

~~~
tnone
In fluid dynamic sims for example. Water level can be positive or negative.
Grids of data can represent more than colors in images.

~~~
colejohnson66
Well yeah. That has use, but when one mentions claiming negative results to
black, I assumed that was for pictures. Obviously negative values mean
something in other places, but I assumed we were talking about pictures.

------
guelo
How are the kernels derived? Is it just an art where people play with the
matrices to see their effects or is there well-understood math behind them?

~~~
zild3d
A little art and plenty of science. The kernel matrices can be broken down
logically once you know what the numbers are operating on. Considering a 3x3
kernel, the center of the kernel matrix is the origin pixel, and the kernel
elements around the origin are the neighboring pixels in their respective
directions and distances.

The identity kernel is [0 0 0; 0 1 0; 0 0 0]. For every input pixel, the
output is the original pixel value. Don't change the pixel value based on what
is around it.

A simple blur kernel would be 1/9 * [1 1 1; 1 1 1; 1 1 1]. The output for each
input pixel is the average of the origin's pixel value and all of its
neighboring values, with an even weighting. A less dramatic blur can weigh the
origin pixel higher than the neighbors, such as a gaussian blur. This will
result in the output pixel being more similar to the origin pixel, than to
it's neighbors.

Edge detection like [-1 -1 -1; -1 8 -1; -1 -1 -1] can be understood by
multiplying the origin pixel value by 8, and subtracting off all of its 8
neighboring values. If the values are all fairly similar, lets say all gray,
your output will be black. 8 _A - 8_ A = 0. So it "punishes" pixels that are
similar to its neighbors. When a pixel is different than some or all of its
neighbors, you will be left with some value > 0 at the output, which detects a
change from its neighbors: an edge. Horizontal edge detection: [1 0 -1; 2 0
-2; 1 0 -1]. Ignores the pixels above, below, and center, but accentuates the
differences between what is on the left from the right.

[https://en.wikipedia.org/wiki/Kernel_(image_processing)](https://en.wikipedia.org/wiki/Kernel_\(image_processing\))

~~~
userbinator
To put it succinctly, convolution just is a fancy term for a weighted average,
and a kernel is the name for a set of weights.

~~~
dfox
In general terms, convolution is defined for two functions which both can be
continuous and then it's essentially an "weighted integral". In 1D case
(sound), when both functions have discrete domain and domain of filter is
finite, then it is the same thing as FIR filter. Convolution with continuous
domain kernel (eg. sinc()) is useful for example for resampling/scaling.

------
Isamu
I don't see image kernels compared to cellular automata, but that's what they
are. We just don't iterate more than once or twice with a kernel, and the
long-term evolution (stability, chaos, or more interesting dynamics) is not
the concern here.

That is to say, there is more to cellular automata than the GOL, and one bit
per cell.

~~~
cmrx64
I'd be interested to learn more about this! I'm completely outside image
processing/machine learning/whatever this is, but articles about it really
fascinate me.

~~~
nealabq
Golly is a free game-of-life program you can play with:

[http://golly.sourceforge.net/](http://golly.sourceforge.net/)

There's an active community searching-for/categorizing/discussing patterns in
Conway's game-of-life (GOL) (and other cellular automata). Examples:

[http://conwaylife.com/forums/](http://conwaylife.com/forums/)
[http://pentadecathlon.com/lifenews/](http://pentadecathlon.com/lifenews/)

~~~
cmrx64
I know about GOL and other simple automata, I'm mostly curious about
applications in image processing.

~~~
corysama
Here's a classic article that blurs the lines between image processing and
automata. Very sad that it seems offline today.

[https://web.archive.org/web/20160325174539/http://freespace....](https://web.archive.org/web/20160325174539/http://freespace.virgin.net/hugo.elias/graphics/x_water.htm)

~~~
avyfain
Thanks for this. I have been playing with CAs recently[1], and doing a lot of
GIF rendering based on the Game of Life rules. This water rendering algorithm
seems like a great next step for me to experiment with.

[1] [https://twitter.com/tweetgameoflife](https://twitter.com/tweetgameoflife)

------
besselheim
The previous discussion has some good comments and links:
[https://news.ycombinator.com/item?id=8966785](https://news.ycombinator.com/item?id=8966785)

------
anjc
Nice site. This would've been very helpful to me in college when I was trying
to get an intuitive grasp of Gaussian blurs and so on via the formulas.

~~~
Kinnard
In what context did you learn about Gaussian Blurs?

~~~
anjc
In Computer Vision, for image manipulation and so on. Using this book if
you're interested: [https://www.amazon.com/Image-Processing-Analysis-Machine-
Vis...](https://www.amazon.com/Image-Processing-Analysis-Machine-
Vision/dp/049508252X)

~~~
Kinnard
I was interested. Thank you very much.

------
jomamaxx
Man we need more articles like this.

So nice and clear.

I've been working with images for 12 years and I was never sure exactly what
'sharpen' actually did ...

------
jscardella
This was an interesting article though I am not an image-o-phile. However,
what I really like was the base site! I am a part time instructor for business
students and I am teaching them about the power of visualization. This is an
incredibly illustrative source that explains points well, and I'll be able to
use it as a teaching tool! Thanks for the post!

------
oyvkva
Really cool way to see how image kernels work.

------
kixpanganiban
Wish we had this when I was taking Discrete Math and Numerical Methods back in
college. Really neat visualization!

------
zk00006
Bug: 3x3 blur kernel, e.g. [1 1 1; 1 1 1; 1 1 1], outputs white image.

