Professional Documents
Culture Documents
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.
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.
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.
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
The JPEG compression algorithm is widely used for lossy image compression.
from PIL import Image # PIL for import Python Imaging Library
img = Image.open(input_path)
if __name__ == "__main__":
input_image_path = "input_image.jpg"
jpeg_output_image_path = "compressed_image.jpeg"
png_output_image_path = "compressed_image.png"
compression_quality = 85
Image segmentation
1. Thresholding:
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:
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: 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