In Python OpenCV Tutorial, Explained How to Blur image using cv2.blur() and cv2.boxFilter() opencv function.
Get the answers of below questions:
- How do I blur an image in OpenCV?
- How do you blur an image in Python?
- Why do we blur image?
- How do you blur part of a picture?
Syntax:
cv2.blur(src, ksize[, dst[, anchor[, borderType]]]) -> dst @brief Blurs an image using the normalized box filter. Parameters: @param src input image; it 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 blurring kernel size. . @param anchor anchor point; default value Point(-1,-1) means that the anchor is at the kernel . center. . @param borderType border mode used to extrapolate pixels outside of the image, see #BorderTypes @sa boxFilter, bilateralFilter, GaussianBlur, medianBlur
cv2.boxFilter(src, ddepth, ksize[, dst[, anchor[, normalize[, borderType]]]]) -> dst The function smooths an image using the kernel: Parameters: @param src input image. . @param dst output image of the same size and type as src. . @param ddepth the output image depth (-1 to use src.depth()). . @param ksize blurring kernel size. . @param anchor anchor point; default value Point(-1,-1) means that the anchor is at the kernel . center. . @param normalize flag, specifying whether the kernel is normalized by its area or not. . @param borderType border mode used to extrapolate pixels outside of the image, see #BorderTypes @sa blur, bilateralFilter, GaussianBlur, medianBlur, integral
Read & Show Image
#### OpenCV Video Tutorial Playlist: https://www.youtube.com/watch?v=-rm0P7A4Jbc&list=PLfP3JxW-T70G5FB9vcmT6T3xnmvFvqV7w ####
## Show Image
import cv2
img_path = r"D:\Indian AI Production\OpenCV\01 Opencv Tutorial\020 Blure Image\vivek_bindra.jpg"
img = cv2.imread(img_path)
cv2.imshow("Model", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Blur Image using cv2.blur() with Kernel 3×3
img_blur_3 = cv2.blur(img, (3,3))
cv2.imshow("Model Blur", img_blur_3)
cv2.imshow("Model ", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Blur Image using cv2.blur() with Kernel 50×50
img_blur_50 = cv2.blur(img, (50,50))
cv2.imshow("Model Blur", img_blur_50)
cv2.imshow("Model ", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Blur Image using cv2.boxfilter() with Kernel 3×3
img_b_blur_3 = cv2.boxFilter(img, -1, (3,3))
cv2.imshow("Model Blur", img_b_blur_3)
cv2.imshow("Model ", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Blur Image using cv2.boxfilter() with Kernel 3×3 without Normalization
img_b_blur_3 = cv2.boxFilter(img, -1, (3,3), normalize=False)
cv2.imshow("Model Blur normalize", img_b_blur_3)
cv2.imshow("Model ", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Blur Image using cv2.boxfilter() with Kernel 50×50
img_b_blur_50 = cv2.boxFilter(img, -1, (50,50))
cv2.imshow("Model Blur", img_b_blur_50)
cv2.imshow("Model ", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Show All Blur Image
import numpy as np
img_blur_3 = cv2.resize(img_blur_3, (650, 400))
img_blur_50 = cv2.resize(img_blur_50, (650, 400))
img_b_blur_3 = cv2.resize(img_b_blur_3, (650, 400))
img_b_blur_50 = cv2.resize(img_b_blur_50, (650, 400))
#putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
cv2.putText(img_blur_3, "blur 3x3", (10,50), cv2.FONT_HERSHEY_COMPLEX, 2, (255,0,55), 3)
cv2.putText(img_blur_50, "blur 50x50", (10,50), cv2.FONT_HERSHEY_COMPLEX, 2, (255,0,55), 3)
cv2.putText(img_b_blur_3, "box blur 3x3", (10,50), cv2.FONT_HERSHEY_COMPLEX, 2, (255,255, 0), 3)
cv2.putText(img_b_blur_50, "box blur 50x50", (10,50), cv2.FONT_HERSHEY_COMPLEX, 2, (255,255, 0), 3)
blurr_2_img = np.hstack((img_blur_3, img_blur_50))
filter_blur_2_img = np.hstack((img_b_blur_3, img_b_blur_50))
img_4 = np.vstack((blurr_2_img, filter_blur_2_img))
cv2.imshow("Show 4 Blur Image", img_4)
cv2.waitKey(0)
cv2.destroyAllWindows()
Blur Image in 2 Line
import cv2
cv2.imshow("Blur Image", cv2.blur(cv2.imread(r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\girl-eye.jpg"), (50,50)))
cv2.waitKey(0)
cv2.destroyAllWindows()