Image smoothing is an operation thats used to remove noise, sharpness and clutter in the image to give you much more smoother and blended effect. With power of opencv and python, you can achieve several smoothing effects with few lines of code.
Basically the way it works is first selecting a kernel (3x3, 5x6 etc) and then convolving with image. The intensity of center pixel is determined by pixel intensities of neighborhood pixel. Depending upon kernel values and the type of aggregation you get several smoothing effects
Here the kernel has uniform weights as shown below. Convolving with this filter simply yields average of pixel intensities in the neighborhood
We apply this filter on following image
Here is the output after applying average smoothing. As you can see noise( design details on building, trees) are removed giving you a much more smoother image
This can be achieved with just 1line of code in opencv
blurred = cv2.blur(image,(3,3))
where 3x3 is kernel size
Gaussian blur is similar to average blurring. The difference is instead of using uniform average, it uses weighted average. The weights are higher for pixels which are nearby and lower for pixels away from center pixel. The advantage is edge preservation is bit better than averaging blur. Here is an example. Notice how edged like eyebrows and lines around eyes are preserved but noise has been removed.
w1 > w2
gaussblurred = cv2.GaussianBlur(image,(x,y),0)
Where x,y is kernel size and 0 indicates that you would like opencv to guess value of standard deviation. You could override the value if you need to.
Gaussian blur usually appears more natural and good to start with first.
Here we used median values instead of mean to determine pixel intensity of center pixel. Because of median value, this technique is much more robust for outliers. This technique is very effective for removing salt and pepper noise in the image.
Here is code
mblurred = cv2.medianBlur(image,x)
where x is size of square kernel
This technique is more effective than simple Gaussian Blur in terms of preserving edges in images at the same time removing noise in the image. It uses 2 gaussian distribution. The first gaussian distribution is used for giving more weightage to nearby pixels. Second gaussian distribution distribution effect is used to consider only those pixel with similar intensity. So wherever there is edge, pixel intensity differ drastically and blur wont be applied there but it will be applied everywhere else. Here is the effect in action. As you can see , nose on hat, har has been removed but edges have been preserved nicely.
Here is the code
blurred = cv2.bilateralFilter(image, d, sC, sS)
image is image that need to be blurred
d is diameter of circle around center pixel. Larger values of d means more number of pixels around center pixel will be considered for blurring
sC is standard deviation for color. Larger values of SC means more color values around center pixel will be considered for blurring
sD is standard deviation for space. Larger is the value, more will be spread of gaussian distribution around center pixel.
Id you would like to learn step by step fundamentals of image smoothing an intuition behind it, please enroll into my online course here .
If you would like discounted coupons for the course please email me at email@example.com
About Author Evergreen Technologies:
Active in teaching online courses in Computer vision , Natural Language Processing and SaaS system developmentOver 20 years of experience in fortune 500 companies
•Linked in: @evergreenllc2020
•Udemy: Evergreen Technologies