Blur Image using Gaussian Filter OpenCV Python | OpenCV Tutorial

In Python OpenCV Tutorial, Explained How to Blur image using cv2.GaussianBlur() opencv function.

Get the answers of below questions:

  1. How do I blur an image in OpenCV?
  2. How do you blur an image in Python?
  3. Why do we blur image?
  4. How do you blur part of a picture?

Syntax:

cv2.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType]]]) -> dst

.   @brief Blurs an image using a Gaussian filter.
 .   
 .   The function convolves the source image with the specified Gaussian kernel. In-place filtering is
 .   supported.

.   @param src input image; the image can have any number of channels, which are processed
.   independently, but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
.   @param dst output image of the same size and type as src.
.   @param ksize Gaussian kernel size. ksize.width and ksize.height can differ but they both must be
.   positive and odd. Or, they can be zero's and then they are computed from sigma.
.   @param sigmaX Gaussian kernel standard deviation in X direction.
.   @param sigmaY Gaussian kernel standard deviation in Y direction; if sigmaY is zero, it is set to be
.   equal to sigmaX, if both sigmas are zeros, they are computed from ksize.width and ksize.height,
.   respectively (see #getGaussianKernel for details); to fully control the result regardless of
.   possible future modifications of all this semantics, it is recommended to specify all of ksize,
.   sigmaX, and sigmaY.
.   @param borderType pixel extrapolation method, see #BorderTypes

Show Image

# Show Image
################## Video Tutorial: https://youtu.be/W05qMagCaR4
import cv2

img_path = r"D:\Indian AI Production\OpenCV\01 Opencv Tutorial\020 Blure Image\sandeep_maheshwari.jpg"

img = cv2.imread(img_path)

cv2.imshow("Model Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Blur Image using Gaussian Blur

# Normal Blur

blur_img = cv2.GaussianBlur(img, (3,3), sigmaX=34, sigmaY=36)

cv2.imshow("Blur Image", blur_img)
cv2.imshow("Model Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()
# Heavy Blur

blur_img = cv2.GaussianBlur(img, (101,101), sigmaX=40, sigmaY=30)

cv2.imshow("Blur Image", blur_img)
cv2.imshow("Model Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Stretch image Vertically

blur_img = cv2.GaussianBlur(img, (1,99), sigmaX=10, sigmaY=10)

cv2.imshow("Blur Image", blur_img)
cv2.imshow("Model Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Stretch image Horizontally

blur_img = cv2.GaussianBlur(img, (99,1), sigmaX=10, sigmaY=20)

cv2.imshow("Blur Image", blur_img)
cv2.imshow("Model Image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Save Blur Image

cv2.imwrite("blur_img.jpg", blur_img)

Leave a Reply