Instagram
youtube
Facebook
Twitter

Smooth/Blur Videos

  • Smoothing is a common image processing technique used to reduce noise from images.

  • We have already learned about it in our image processing tutorial. 

  • We can also use OpenCV to reduce the noise of videos.

  • In OpenCV, smoothing can be applied to videos using various filters, such as Gaussian blur, median blur, or bilateral filter.

In this tutorial, we’ll learn about how to smooth videos using OpenCV. We have already described all the filters in our image-processing tutorial. So, if you want to learn about each filter click here.  

 

Input:

 

Code:
 

import cv2
my_input = cv2.VideoCapture("video1.mp4")
if not my_input.isOpened():
    print("Error opening video file")
    exit()
output_file = "output_blur.mp4"
fourcc = cv2.VideoWriter_fourcc(*'mp4v') 
out = cv2.VideoWriter(output_file, fourcc, 30.0, (int(my_input.get(cv2.CAP_PROP_FRAME_WIDTH)),int(my_input.get(cv2.CAP_PROP_FRAME_HEIGHT))))
while my_input.isOpened():
    ret, frame = my_input.read() 
    if ret:
        blur = cv2.GaussianBlur(frame, (15, 15), 0)
        out.write(blur)
        cv2.imshow("Frame", blur)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
my_input.release()
out.release()
cv2.destroyAllWindows()

   

  • First we have imported OpenCV.

  • Then, we used the cv2.VideoCapture() function to read the video file. 

  • Then, we used cv2.isOpened() function to check if the video is opened or not. If not, then it prints an error message.

  • Then, we specified the name of the output file(output_blur.mp4) and fourcc(mp4v).

  • Then, we used cv2.videowriter() function with parameters including image, fourcc, fps, and resolution.

  • After this we used a while loop and performed a Gaussian blur operation on the frames using a kernel size of 15x15 with a sigma of 0, and wrote the blurred frames to an output video file (out). We have also used cv2.imshow function to display the final result.

  • At last we have used the release() function to release all the resources.   

 

result: