You are on page 1of 6

Unit - 5

Image compression

Image compression is a technique used to reduce the size of an image file, making it
easier to store and transmit while minimizing the impact on visual quality. There are two
main types of image compression: lossless and lossy.

1. Lossless Image Compression:

 Lossless compression retains all the original data, and the decompressed image is
identical to the original.
 Examples of lossless compression algorithms include Run-Length Encoding
(RLE), Huffman coding, and the Lempel-Ziv-Welch (LZW) algorithm.
 Lossless compression is suitable for images where preserving every detail is
critical, such as medical images or text documents.

2. Lossy Image Compression:

 Lossy compression reduces file size by discarding some image information. The
decompressed image is not identical to the original, and some details may be lost.
 Popular lossy compression algorithms include JPEG (Joint Photographic Experts
Group), WebP, and JPEG2000.
 Lossy compression is commonly used for photographs and other images where
some loss of detail is acceptable.

Basic Compression Methods:

1. Run-Length Encoding (RLE):


Principle: Replaces sequences of identical pixels with a single value and a count of
how many times that value should be repeated.
Use Case: Effective for images with long runs of identical pixels, such as simple
graphics or line drawings.
2. Huffman Coding:
Principle: Assigns variable-length codes to different symbols based on their
frequencies. Frequent symbols have shorter codes.
Use Case: Suitable for lossless compression and often used in combination with
other techniques. Huffman coding is used in image file formats like GIF.
3. Differential Coding:
Principle: Encodes the difference between consecutive pixel values rather than the
absolute values.
Unit - 5

Use Case: Effective for images with smooth gradients or slowly changing pixel
values. It reduces the dynamic range of the data.
4. Arithmetic Coding:
Principle: Maps entire messages to single values in the unit interval [0, 1), using
probabilities of symbols.
Use Case: Provides higher compression efficiency compared to Huffman coding but
is computationally more complex.
5. Transform Coding (e.g., Discrete Cosine Transform - DCT):
Principle: Transforms image data into a different domain, where most energy is
concentrated in fewer coefficients. Quantizes and codes these coefficients.
Use Case: Commonly used in lossy compression algorithms like JPEG. DCT
separates the image into frequency components, allowing more efficient compression
of high-frequency details.
6. Quantization:
Principle: Reduces the precision of pixel values, often by dividing them into
quantization intervals.
Use Case: Frequently used in combination with other compression methods. In lossy
compression, higher quantization levels result in smaller file sizes but also reduce
image quality.
7. Entropy Coding:
Principle: Takes advantage of the statistical properties of the image data to assign
shorter codes to more probable symbols.
Use Case: Used in combination with other coding methods. Arithmetic coding and
Huffman coding are examples of entropy coding techniques.
8. Predictive Coding:
Principle: Predicts pixel values based on neighboring pixels and encodes the
difference between the predicted and actual values.
Use Case: Effective for images with smooth transitions. Used in lossless and lossy
compression algorithms.
Unit - 5

JPEG Compression Example (Lossy)

The JPEG compression algorithm is widely used for lossy image compression.

Image.open: Open input image file.

img.save: Saves the image using the PNG format

make sure to install pillow library (pip install Pillow)

from PIL import Image # PIL for import Python Imaging Library

def compress_image(input_path, jpeg_output_path, quality=85):

def compress_lossless(input_path, png_output_path):

# Open the image

img = Image.open(input_path)

# Save the compressed image using JPEG format(Lossy)

img.save(jpeg_output_path, 'JPEG', quality=quality)

# Save the compressed image using PNG format(lossless)

img.save(png_output_path, 'PNG', quality=quality)

if __name__ == "__main__":

input_image_path = "input_image.jpg"

jpeg_output_image_path = "compressed_image.jpeg"

png_output_image_path = "compressed_image.png"

# Specify the compression quality (0-100)

compression_quality = 85

# Compress the image

print("JPG Image compression start.")


Unit - 5

compress_image(input_image_path, jpeg_output_image_path, compression_quality)

print("JPEG Image compression complete.")

print("PNG image compression Start.")

compress_image(input_image_path, png_output_image_path, compression_quality)

print("PNG image compression complete.")

Image segmentation

Image segmentation is a fundamental task in computer vision that involves partitioning


an image into meaningful regions or segments based on certain criteria. The goal of
segmentation is to group together pixels or regions that share similar visual properties.
There are various methods for image segmentation, and the choice of a particular
technique depends on the characteristics of the images and the application requirements.

1. Thresholding:

Method: Assign pixels to different segments based on a threshold applied to pixel


intensity or color.
Use Case: Simple and effective for images with distinct intensity or color differences.
Often used in binary segmentation.

2. Region-based Segmentation:

Method: Group pixels into segments based on their spatial connectivity and similarity in
color, intensity, or texture.
Use Case: Suitable for images with regions of similar characteristics. Watershed
segmentation is an example.

3. Edge-based Segmentation:

Method: Identify boundaries or edges between different regions in the image.


Use Case: Useful for images with well-defined edges. Common edge detection methods
include the Canny edge detector.

4. Clustering Methods:

Method: Group pixels based on their similarity in feature space using clustering
algorithms like k-means.
Unit - 5

Use Case: Effective for images with distinct clusters or groups of similar pixels.

5. Graph-based Segmentation:

Method: Represent pixels as nodes in a graph and use graph algorithms to find
meaningful segments.
Use Case: Suitable for images with complex structures. The Felzenszwalb algorithm is
an example.

6. Contour-based Segmentation:

Method: Identify contours or boundaries of objects in the image.


Use Case: Useful for object detection and extraction. The Active Contour Model (Snake)
is an example.

7. Deep Learning-based Segmentation:

Method: Utilize deep neural networks, such as U-Net, Mask R-CNN, or DeepLab, for
semantic segmentation.
Use Case: Suitable for complex tasks like object recognition and scene understanding.

Implementation

import cv2
import numpy as np

# Read the image


image = cv2.imread('example_image.jpg')

# Convert the image to grayscale


gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply Gaussian blur to reduce noise and enhance segmentation


blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)

# Apply Canny edge detector to find edges


edges = cv2.Canny(blurred_image, 50, 150)

# Find contours in the edges


Unit - 5

contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL,


cv2.CHAIN_APPROX_SIMPLE)

# Draw the contours on the original image


result_image = image.copy()
cv2.drawContours(result_image, contours, -1, (0, 255, 0), 2)

# Display the results


cv2.imshow('Original Image', image)
cv2.imshow('Segmented Image with Contours', result_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

You might also like