Skip to main content

VideoQuality Enhancing Using Python

Enhancing video quality using Python involves improving the visual appearance of a video by applying various image processing techniques and algorithms. This process can help reduce noise, increase sharpness, improve color balance, and generally make the video more visually appealing. Below is a more detailed description of the steps involved in enhancing video quality using Python:


1. Preparation:

   - Install the necessary Python libraries, such as OpenCV for video manipulation and NumPy for numerical operations.


2. Read the Video:

   - Load the video file you want to enhance using OpenCV's `VideoCapture` function. This step initializes a video capture object, making it possible to access the individual frames of the video.


3. Frame Processing:

   - Loop through the video frames, one frame at a time.

   - Apply various image processing techniques to enhance each frame. Common techniques include:

     - Denoising: Reducing noise in the video by applying filters like Gaussian blur, median blur, or bilateral filter.

     - Upscaling: Increasing the resolution and size of the video frames using techniques like interpolation or deep learning-based super-resolution models.

     - Color Correction: Adjusting brightness, contrast, saturation, and color balance to make the video more visually appealing.

     - Stabilization: Reducing shakiness in the video by applying video stabilization algorithms.

     - Deinterlacing: Converting interlaced video to progressive scan to improve video quality.

     - Contrast Enhancement: Increasing the contrast between different elements in the video to make it more vivid.

     - Sharpening: Enhancing the sharpness of the video to make details more distinct.


4. Frame Reconstruction:

   - After enhancing each frame, you can either overwrite the original frame with the improved version or store it in a new video file for later use.


5. Create the Enhanced Video:

   - Use OpenCV's `VideoWriter` to create a new video file where you will save the enhanced frames.

   - Specify the output video's format, frame rate, and resolution.


6. Display or Save:

   - Optionally, you can display the enhanced video within your Python application for real-time visual inspection.

   - Save the enhanced video to a file using OpenCV's `VideoWriter` object.


7. Cleanup:

   - Properly release resources by closing video capture and writer objects when you're done with the video enhancement process.


Video quality enhancement in Python can range from simple adjustments like noise reduction and color correction to more complex processes involving deep learning models for upscaling and denoising. The specific techniques used will depend on the quality of the input video and the desired level of enhancement. Additionally, you may want to consider user preferences and the intended use of the video when determining the extent of enhancement required.

Input Video:



Video details

Uploaded on April 24th, 2020

Dimensions:3840 x 2160

Aspect Ratio:16:9

Duration:30:20

FPS:23

Enhance Video Quality Code:

import cv2


# Input and output video file paths

input_video_path = 'input.mp4'

output_video_path = 'output_video.mp4'


# Open the input video file

input_video = cv2.VideoCapture(input_video_path)


# Get the current frame's width and height

width = int(input_video.get(3))

height = int(input_video.get(4))


# Define the desired output resolution

new_width = 7680  # New width in pixels

new_height = 4320  # New height in pixels


# Define the VideoWriter to save the upscaled video

fourcc = cv2.VideoWriter_fourcc(*'XVID')

output_video = cv2.VideoWriter(output_video_path, fourcc, 30.0, (new_width, new_height))


while True:

    ret, frame = input_video.read()

    if not ret:

        break


    # Resize the frame to the new resolution

    frame = cv2.resize(frame, (new_width, new_height))


    # Write the frame to the output video

    output_video.write(frame)


# Release the video objects

input_video.release()

output_video.release()


# Destroy any OpenCV windows

cv2.destroyAllWindows()

Output:



SharpenVideo Code:

import cv2

import numpy as np


def sharpen_frame(frame):

    # Define the sharpening kernel

    kernel = np.array([[-1, -1, -1],

                       [-1, 9, -1],

                       [-1, -1, -1]])

    

    # Apply the kernel to the frame

    sharpened_frame = cv2.filter2D(frame, -1, kernel)

    

    return sharpened_frame


# Input and output video file paths

input_video_path = 'input.mp4'

output_video_path = 'output_video(sharpen).mp4'


# Open the input video file

input_video = cv2.VideoCapture(input_video_path)


# Get the video's width, height, and FPS

width = int(input_video.get(3))

height = int(input_video.get(4))

fps = int(input_video.get(5))


# Define the VideoWriter to save the sharpened video

fourcc = cv2.VideoWriter_fourcc(*'XVID')

output_video = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))


while True:

    ret, frame = input_video.read()

    if not ret:

        break


    # Sharpen the frame

    sharpened_frame = sharpen_frame(frame)


    # Write the sharpened frame to the output video

    output_video.write(sharpened_frame)


# Release the video objects

input_video.release()

output_video.release()


# Destroy any OpenCV windows

cv2.destroyAllWindows()

Output:



Increasing the frames per second Code:

import cv2


# Input and output video file paths

input_video_path = 'input.mp4'

output_video_path = 'output_video.mp4'


# Open the input video file

input_video = cv2.VideoCapture(input_video_path)


# Get the current frame rate

current_fps = int(input_video.get(cv2.CAP_PROP_FPS))


# Define the desired output frame rate

new_fps = 90  # New frame rate


# Define the VideoWriter to save the video with the new frame rate

fourcc = cv2.VideoWriter_fourcc(*'XVID')

output_video = cv2.VideoWriter(output_video_path, fourcc, new_fps,(int(input_video.get(3)), int(input_video.get(4))))


while True:

    ret, frame = input_video.read()

    if not ret:

        break


    # Write each frame multiple times to increase the frame rate

    for _ in range(new_fps // current_fps):

        output_video.write(frame)


# Release the video objects

input_video.release()

output_video.release()


# Destroy any OpenCV windows

cv2.destroyAllWindows()

Output:




ColorGradingVideo Code:
import cv2
import numpy as np

def apply_color_grading(frame, brightness=1.0, contrast=1.0, saturation=1.0):
    # Adjust brightness, contrast, and saturation
    frame = cv2.convertScaleAbs(frame, alpha=contrast, beta=brightness * 255)
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    frame = np.array(frame, dtype=np.float32)
    frame[:, :, 1] *= saturation
    frame = np.clip(frame, 0, 255)
    frame = np.array(frame, dtype=np.uint8)
    frame = cv2.cvtColor(frame, cv2.COLOR_HSV2BGR)
    return frame

# Input and output video file paths
input_video_path = 'input.mp4'
output_video_path = 'output_video(ColorGraded).mp4'

# Open the input video file
input_video = cv2.VideoCapture(input_video_path)

# Get the video's width, height, and FPS
width = int(input_video.get(3))
height = int(input_video.get(4))
fps = int(input_video.get(5))

# Define color grading parameters
brightness = 1.0  # Adjust as needed (1.0 is neutral)
contrast = 1.0   # Adjust as needed (1.0 is neutral)
saturation = 1.0  # Adjust as needed (1.0 is neutral)

# Define the VideoWriter to save the color-graded video
fourcc = cv2.VideoWriter_fourcc(*'XVID')
output_video = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))

while True:
    ret, frame = input_video.read()
    if not ret:
        break

    # Apply color grading to the frame
    graded_frame = apply_color_grading(frame, brightness, contrast, saturation)

    # Write the graded frame to the output video
    output_video.write(graded_frame)

# Release the video objects
input_video.release()
output_video.release()

# Destroy any OpenCV windows
cv2.destroyAllWindows()

Thank You For Visiting Our Blog.

Comments

Popular posts from this blog

Stable Diffusion WebUI 1.10.1 Full Installation Guide | AUTOMATIC1111 | Windows 11

Stable Diffusion WebUI 1.10.1 Full Installation Guide | AUTOMATIC1111 | Windows 11  Welcome to this step-by-step Stable Diffusion WebUI 1.10.1 installation guide! In this tutorial, we will walk you through the complete setup process on Windows 11 , including downloading and installing Git , setting up Python 3.10.6 , cloning the AUTOMATIC1111 repository , and configuring .gitignore for a clean and efficient installation. By following this guide, you’ll be able to generate AI-generated images using Stable Diffusion with ease. Whether you're new to AI image generation or an experienced user, this guide ensures that your setup is optimized for performance and stability. 🔗 Required Downloads: Before we begin, make sure to download the following tools: ✅ Git for Windows – Download Here ✅ Stable Diffusion WebUI (AUTOMATIC1111) – Download Here ✅ Python 3.10.6 – Download Here 🛠️ Step-by-Step Installation Process 1️⃣ Install Git for Windows Git is required to clone the ...

Unreal Engine Product Showcase: Mesmerizing Video Sequence Render

  4k Image:

Install TensorFlow on Windows 11: Step-by-Step Guide for CPU & GPU

 --- Installing **TensorFlow on Windows 11** requires setting up system dependencies, configuring Python, and ensuring compatibility with CPU or GPU acceleration. This step-by-step guide provides everything needed to install **TensorFlow 2.10 or lower** on **Windows Native**, including software prerequisites, Microsoft Visual C++ Redistributable installation, Miniconda setup, GPU driver configuration, and verification steps.   ### **System Requirements:**   Before installing TensorFlow, ensure your system meets these requirements:   - **Operating System:** Windows 7 or higher (64-bit)   - **Python Version:** 3.9–3.12   - **pip Version:** 19.0 or higher for Linux and Windows, 20.3 or higher for macOS   - **Microsoft Visual C++ Redistributable:** Required for Windows Native   - **Long Paths Enabled:** Ensure long paths are enabled in Windows settings   For **GPU support**, install:   - **NVIDIA ...