Enhance & Restore Images with OpenCV and Python

Image enhancement and restoration are important tasks in image processing, especially for old or damaged photos. In this article, we will explore how to use OpenCV and Python to improve image quality and breathe new life into old or damaged photos.

Table of Contents

  1. Introduction to OpenCV
  2. Setting up the Environment
  3. Image Enhancement Techniques
  4. Image Restoration Techniques
  5. Conclusion

Introduction to OpenCV

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. It has C++, Python, and Java interfaces, and supports Windows, Linux, Mac OS, iOS, and Android. OpenCV is designed for computational efficiency and focuses on real-time applications, making it perfect for image enhancement and restoration tasks.

Setting up the Environment

To begin, you'll need to install the required libraries. You can install OpenCV using the following command:

pip install opencv-python

Next, we'll import the necessary libraries in our Python script:

import cv2
import numpy as np
from matplotlib import pyplot as plt

Image Enhancement Techniques

Histogram Equalization

Histogram equalization is a technique that improves the contrast in an image by redistributing the intensity values. This is particularly useful for images with poor lighting conditions.

def histogram_equalization(image):
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    equalized_image = cv2.equalizeHist(gray_image)
    return equalized_image

image = cv2.imread('input_image.jpg')
equalized_image = histogram_equalization(image)

cv2.imshow('Equalized Image', equalized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Contrast Stretching

Contrast stretching is another technique that improves the contrast in an image by adjusting the intensity values. It rescales the image's pixel values to cover the full range of intensities.

def contrast_stretching(image):
    image_min = np.min(image)
    image_max = np.max(image)
    return (image - image_min) * (255 / (image_max - image_min))

image = cv2.imread('input_image.jpg')
stretched_image = contrast_stretching(image)

cv2.imshow('Contrast Stretched Image', stretched_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Image Smoothing

Image smoothing is a technique that reduces noise and blurring in an image. It can be achieved with various filters, such as Gaussian, Median, or Bilateral filters.

def smooth_image(image, method='gaussian', kernel_size=(5, 5), sigma_x=0):
    if method == 'gaussian':
        return cv2.GaussianBlur(image, kernel_size, sigma_x)
    elif method == 'median':
        return cv2.medianBlur(image, kernel_size[0])
    elif method == 'bilateral':
        return cv2.bilateralFilter(image, kernel_size[0], sigma_x, sigma_x)

image = cv2.imread('input_image.jpg')
smoothed_image = smooth_image(image, method='gaussian')

cv2.imshow('Smoothed Image', smoothed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Image Restoration Techniques

Removing Noise with Median Filtering

Median filtering is a technique that reduces noise in an image by replacing each pixel with the median value of its neighboring pixels.

def median_filter(image, kernel_size=5):
    return cv2.medianBlur(image, kernel_size)

image = cv2.imread('input_image.jpg')
denoised_image = median_filter(image)

cv2.imshow('Denoised Image', denoised_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Inpainting

Inpainting is a technique that restores damaged or missing parts of an image by filling them with the surrounding information. OpenCV provides two inpainting algorithms: Navier-Stokes and Telea.

def inpaint_image(image, mask, method='telea', radius=3):
    if method == 'navier_stokes':
        return cv2.inpaint(image, mask, radius, cv2.INPAINT_NS)
    else:
        return cv2.inpaint(image, mask, radius, cv2.INPAINT_TELEA)

image = cv2.imread('input_image.jpg')
mask = cv2.imread('mask_image.jpg', 0)
restored_image = inpaint_image(image, mask)

cv2.imshow('Restored Image', restored_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Conclusion

In this article, we've explored various techniques to enhance and restore images using OpenCV and Python. These techniques can be used to improve image quality, remove noise, and restore damaged photos. With the power of OpenCV and Python, you can bring new life to old or damaged images and create better visualizations for your projects.

An AI coworker, not just a copilot

View VelocityAI