Image Compression in OpenCV

In this tutorial, we will explore image compression, understand the theory behind it, and learn how to perform it using OpenCV. So, let’s get started!

Updated March 25, 2023

Learn more about OpenCV and Computer Vision from this YouTube Channel!

Welcome to this comprehensive tutorial on image compression in OpenCV! As an expert in computer vision, you’ll often need to store and transmit images efficiently while maintaining their quality. In this tutorial, we will explore image compression, understand the theory behind it, and learn how to perform it using OpenCV. So, let’s get started!

What is Image Compression?

Image compression is the process of reducing the size of an image file without significantly affecting its visual quality. This is achieved by removing redundant data or approximating parts of the image, making it more storage and transmission-friendly.

There are two main types of image compression: lossless compression and lossy compression. Lossless compression retains the original image data, while lossy compression discards some information to achieve a smaller file size, potentially reducing the image quality.

Why is Image Compression Important?

Image compression is essential for many reasons:

  • Reduced storage space: Compressed images occupy less storage space, allowing you to store more images or use the saved space for other purposes.

  • Faster transmission: Smaller file sizes enable faster transmission of images over networks, which is particularly important in applications like video streaming and remote sensing.

  • Reduced bandwidth usage: Compressed images consume less bandwidth, which is crucial for data transfer on limited or metered connections.

The Theory Behind Image Compression

Image compression techniques typically exploit the spatial and color redundancies in images. Spatial redundancy arises from the correlation between neighboring pixels, while color redundancy stems from the similarity of color values in adjacent regions.

Lossless compression methods, such as PNG and GIF, use techniques like Run-Length Encoding and Huffman coding to eliminate these redundancies without losing any information.

Lossy compression methods, such as JPEG, use Discrete Cosine Transform (DCT) and quantization to approximate the image data, allowing for a smaller file size at the cost of some image quality loss.

Image Compression with OpenCV: A Step-by-Step Guide

Now that we have a basic understanding of image compression, let’s see how to perform it using OpenCV. We’ll be using Python for our examples, but you can also use the OpenCV C++ API.

Step 1: Install OpenCV

First, let’s install OpenCV. You can do this by running the following command:

pip install opencv-python opencv-python-headless

Step 2: Load an Image

Let’s start by loading an image using OpenCV:

import cv2

img = cv2.imread('path/to/image.jpg')

Step 3: Save the Image with Lossless Compression (PNG)

To save the image with lossless compression, we can use OpenCV’s imwrite() function with the PNG format:

cv2.imwrite('lossless_compressed_image.png', img)

This will save the image in the PNG format, which uses lossless compression.

Step 4: Save the Image with Lossy Compression (JPEG)

To save the image with lossy compression, we can use OpenCV’s imwrite() function with the JPEG format. Additionally, we can control the compression quality by setting the cv2.IMWRITE_JPEG_QUALITY flag:

jpeg_quality = 90  # A value between 0 and 100 (higher means better quality, but larger file size)
cv2.imwrite('lossy_compressed_image.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, jpeg_quality])

In this example, we save the image in the JPEG format with a quality setting of 90. The quality value can range from 0 to 100, with higher values representing better quality but larger file sizes.

Step 5: Compare the Compression Results

Now, let’s compare the original image with the compressed images to see the difference in file size and visual quality:

import os

original_size = os.path.getsize('path/to/image.jpg')
lossless_size = os.path.getsize('lossless_compressed_image.png')
lossy_size = os.path.getsize('lossy_compressed_image.jpg')

print(f'Original image size: {original_size} bytes')
print(f'Lossless compressed image size: {lossless_size} bytes')
print(f'Lossy compressed image size: {lossy_size} bytes')

You can also display the images side by side to visually compare their quality:

lossless_img = cv2.imread('lossless_compressed_image.png')
lossy_img = cv2.imread('lossy_compressed_image.jpg')

cv2.imshow('Original Image', img)
cv2.imshow('Lossless Compressed Image', lossless_img)
cv2.imshow('Lossy Compressed Image', lossy_img)

And that’s it! You now know how to perform image compression in OpenCV. By understanding the principles behind image compression and using OpenCV’s functions, you can efficiently store and transmit images in your computer vision projects.

Remember to keep experimenting with different compression formats and quality settings to find the best balance between file size and visual quality for your specific use case. Happy coding!