Skip to content

Instantly share code, notes, and snippets.

@palaashatri
Created August 12, 2020 06:00
Show Gist options
  • Save palaashatri/d34aa1f1607eb1263cfeff0977297119 to your computer and use it in GitHub Desktop.
Save palaashatri/d34aa1f1607eb1263cfeff0977297119 to your computer and use it in GitHub Desktop.
Demonstration of OpenCV Gaussian Blur, Dilation and Erosion filters on an image
import numpy as np
import cv2
image = cv2.imread("image.jpg")
cv2.imshow("Original",image)
#@ Gaussian Blur :
# Gaussian Blur filter smooths an image by averaging pixel values with its neighbors.
# It's called a Gaussian Blur because the average has a Gaussian falloff effect.
# In other words, pixels that are closer to the target pixel have a higher impact with the average than pixels that are far away.
blur = cv2.GaussianBlur(image,(5,55),0) # 2nd argument needs to be odd values
cv2.imshow("Blur",blur)
#@@ Dilation and Erosion :
# here are two operations that look to expand or contract the foreground pixels of an image to help remove or accentuate small pixel details, such as speckles.
# They work by sliding a kernel template, a small square, across an image
# The Dilation effect works to turn black pixels, or background pixels, into white pixels, while an erosion filter looks to turn white pixels into black pixels, essentially eating away at the foreground.
# The small structuring element that was moved across the image is called the kernel and defines where and how to mark a pixel changed by that filter.
kernel = np.ones((5,5),'uint8')
dilate = cv2.dilate(image,kernel,iterations=1)
erode = cv2.erode(image,kernel,iterations=1)
cv2.imshow("Dilate",dilate)
cv2.imshow("Erode",erode)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment