高斯模糊的簡單演算法
阿新 • • 發佈:2019-01-30
來自http://www.swageroo.com/wordpress/how-to-program-a-gaussian-blur-without-using-3rd-party-libraries/
This is where the Gaussian equation comes in, using it we can find out how much weight we want each pixel to receive and pixels receive less weight depending on its distance to the center pixel.
Let’s explain what everything in this equation means:
σ (lowercase sigma) – This is the blurring factor, the larger this number is, the smoother/blurrier the image becomes.
e - This is simply euler’s number, a constant, 2.71828182846
x – This is the distance from the origin — The horizontal distance to the center pixel.
y – This is the distance from the origin — The vertical distance to the center pixel.
This means that x and y in this equation will be zero for the center pixel (the current pixel we want to blur), and x^2 + y^2 increases as we get farther away from the center, causing lower weights for pixels farther away.
Let’s say we wanted to find out how we would weigh neighboring pixels if we wanted a ‘window’ or ‘kernel size’ of 3 for our Gaussian blur. Of course the center pixel (the pixel we are actually blurring) will receive the most weight. Lets choose a σ of 1.5 for how blurry we want our image.
Here’s what our weight window would look like:
With each weighting evaluated it looks like this: (Notice that the weighting for the center pixel is greatest)
If you’re pretty observant you’ll notice that this matrix doesn’t add up 1. For this to represent a weights, all the weights when summed together will have to add up to 1. We can multiply each number by 1/sum to ensure this is true. The sum of this matrix is 0.4787147. This means we need to multiply the matrix by 1/0.4787147 so that all elements end up adding up to 1. We finally get the following matrix which represents how we will weight each pixel during a blur.