News for hackers. Stuff that matters.
Re:Image Kernels explained visually by guelo on Monday August 08, 2016 @02:56PM (#12250042)
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?
Re:Image Kernels explained visually by zild3d on Monday August 08, 2016 @03:45PM (#12250376)
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. 8A - 8A = 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)

Re:Image Kernels explained visually by userbinator on Monday August 08, 2016 @11:11PM (#12252346)
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.
Re:Image Kernels explained visually by dfox on Tuesday August 09, 2016 @06:49AM (#12253689)
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.
Re:Image Kernels explained visually by salehenrahman on Monday August 08, 2016 @03:17PM (#12250200)
There is a well-understood math behind it.

For blur, you can either do two things:

- generate a matrix such that each element in the matrix is equal to 1 (e.g. averaging)

- generate a matrix such that it represents the Gaussian distribution (you can use a 2D Gauss function)

For edge detection, you essentially have "derivative", e.g. rate of change; the more abrupt the change, the brighter the resulting pixel, hence why edges are highlighted. A good convolution kernel for edge detection would be the Laplacian.

For sharpness, it's pretty much the Laplacian Kernel + identity kernel.

Re:Image Kernels explained visually by echelon on Monday August 08, 2016 @08:25PM (#12251738)
I never realized sharpness was identity + Laplacian, but that absolutely makes sense. Thanks!
Re:Image Kernels explained visually by GregBuchholz on Monday August 08, 2016 @06:08PM (#12251204)
You might be interested in "The Scientist and Engineer's Guide to Digital Signal Processing":

http://www.dspguide.com/

...you might want to start by skimming chapters 23 and 24.

Re:Image Kernels explained visually by sforzando on Monday August 08, 2016 @03:29PM (#12250289)
An example of a kernel that has a mathematical explanation for its values is the (discrete) Gaussian kernel, used to blur/smooth images: http://dev.theomader.com/gaussian-kernel-calculator/

The values of the gaussian kernel matrix are determined by doing a discrete sampling of the gaussian function. You get to choose sigma (gaussian's standard deviation) and kernel size (spatial neighborhood of the kernel, ie how much of the surroundings that the kernel will examine).

Another example is the Sobel operator, used to extract edges from images: https://en.wikipedia.org/wiki/Sobel_operator

The kernel matrix is the result of composing a gaussian smoothing with a spatial-differencing operation. Thus, the Sobel estimates edges from smoothed images.

As for the sharpen kernel described in the post -- an intuitive explanation is that you want to accentuate differences in pixel intensities.

Re:Image Kernels explained visually by BenFrantzDale on Monday August 08, 2016 @04:23PM (#12250615)
Both, but lots of science. In particular, convolution is linear so if you know what you want a bright dot (a single pixel—a delta function) to look like, then that is your kernel. Want a dot to look like a fuzzy blob? Then your kernel should be a fuzzy blob (Gaussian blur). Want a dot to turn into a positive blip on the left and a negative one on the right (an x-derivative), then (-1, 0, 1) is your buddy (I lied in the symmetric case: it needs to be flipped).
Re:Image Kernels explained visually by mistercow on Monday August 08, 2016 @03:41PM (#12250347)
If you understand how things work in the frequency domain, you can design them there and convert them back to the time domain (or leave them in the frequency domain, because if they're of any significant size, convolution is faster in the frequency domain anyway).
Re:Image Kernels explained visually by hatsunearu on Monday August 08, 2016 @04:48PM (#12250760)
image kernels are 2D convolutions, and you can think of them as extensions from 1D convolutions. In 1D, understanding the frequency domain behavior is much easier since you don't have to worry about spacial frequency and 1D frequency is something most people can intuitively understand.

look at FIR filters, they are essentially 1D image kernels.

Re:Image Kernels explained visually by amelius on Monday August 08, 2016 @06:35PM (#12251294)
Also an interesting question: given a kernel K, how do you derive the inverse of that kernel?
Re:Image Kernels explained visually by dpark on Monday August 08, 2016 @11:15PM (#12252361)
Would that not just be the inverse matrix?

http://mathworld.wolfram.com/MatrixInverse.html

Re:Image Kernels explained visually by tel on Tuesday August 09, 2016 @01:40AM (#12252823)
Nope, and in general it doesn't exist. That said, there are techniques for "deconvolution" which partially reverse (or in some, special cases completely reverse) a kernel convolution.
Re:Image Kernels explained visually by dpark on Tuesday August 09, 2016 @10:46AM (#12254813)
Ah, thanks. I guess that makes sense since this isn't straightforward matrix multiplication.
Re:Image Kernels explained visually by amelius on Tuesday August 09, 2016 @11:15AM (#12255114)
I would suggest to try a 1-dimensional example first. In that case, the kernel does not look like a matrix, but more like a vector.