0% found this document useful (0 votes)
10 views88 pages

Lec - 1 (Introduction to Images and Computing using Python)

Digital Image Processing

Uploaded by

Mahdi Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views88 pages

Lec - 1 (Introduction to Images and Computing using Python)

Digital Image Processing

Uploaded by

Mahdi Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 88

Shyamoli Textile Engineering College

Department of Computer Science and Engineering


CSE-4267: Digital Image Processing Lab

Lec-1: Introduction to Images and Computing using Python


Ref: Image Processing and Acquisition using Python by Ravishankar Chityala

Md. Ariful Islam


Assistant Professor
Dept. of Robotics and Mechatronics Engineering
University of Dhaka

1
Outlines
❑ Introduction to Python

❑ Computing using Python Modules

❑ Image Types

❑ Reading, Writing and Displaying Images

❑ Conversion of Image Modes

❑ Image Manipulation

❑ Sampling and Quantization


2
1. Introduction to Python

Python is a popular high-level programming language. It can handle various


programming tasks such as numerical computation, web development, database
programming, network programming, parallel processing, etc.

Python is popular mainly because of:

❑ Ease of Learning and Use: Its simple and readable syntax makes it beginner-
friendly.
❑ Versatility: It supports various applications, including web development, AI,
data analysis, and automation.
❑ Rich Libraries: Python offers extensive libraries like NumPy, pandas, and
TensorFlow for diverse tasks.
3
1.1 Python Environments

4
5
6
7
2. Python Modules A library is a collection of pre-written code that
2.1 numpy: you can use to perform specific tasks.
A powerful library for manipulating arrays and matrices

8
9
10
11
12
2.2 scipy:

Provides functions for performing higher-order mathematical operations such as


filtering, statistical analysis, image processing, etc.

13
14
15
16
17
2.3 matplotlib:

Provides functions for plotting and other forms of visualization.

18
19
20
21
22
23
2.4 Python Imaging Library:

Provides functions for basic


image reading, writing and
processing.
The imageio.v3 module is part
of the ImageIO library, which
is a modern and efficient
library for reading, writing,
and manipulating image and
video data in Python.

It supports multiple image


formats (e.g., PNG, JPEG, GIF,
TIFF). 24
25
26
27
28
29
30
31
32
33
34
35
36
37
2.5 Open Source Computer Vision Library (OpenCV)

The Open Source Computer Vision Library (OpenCV) is an image processing,


computer vision and machine learning software library. It has more than 2000
algorithms for processing image data.

It has a large user base and is used extensively in academic institutions,


commercial organizations, and government agencies.

38
39
40
3. Image Types

There are more than 100 image formats.

Some of these formats, such as JPEG, GIF, PNG, etc., are used for photographic
images.

Formats such as DICOM, NIFTI, and Analyze AVW are used in medical imaging.

Formats such as TIFF, ICS, IMS, etc., are used in microscope imaging.

41
3.1 JPEG (Joint Photographic Experts Group)

File Extension: .jpg or .jpeg

Best For: Photographs and complex images with many colors

Compression: Lossy
❑ JPEG uses lossy compression, meaning that some image data is discarded to
reduce file size. This results in a loss of quality, especially at higher
compression levels.

Animation: Not supported

File Size: Small to moderate, depending on compression


42
3.2 GIF (Graphics Interchange Format)

File Extension: .gif

Best For: Simple graphics, logos, and animations

Compression: Lossless (but limited color palette)


❑ GIF uses lossless compression, meaning the image is compressed without
losing any data.

Animation: Supported
❑ GIFs can display multiple frames and create short animations, making them
popular for animated graphics and memes.

File Size: Small to moderate, depending on the number of colors and frames.
43
3.3 PNG (Portable Network Graphics)

File Extension: .png

Best For: Website logos, graphics with transparent backgrounds, and


illustrations.

Compression: Lossless
❑ PNG uses lossless compression, meaning no image data is lost during
compression. This results in a larger file size compared to JPEG, but it
preserves the original quality of the image.

File Size: Larger than JPEG, but smaller than BMP or TIFF for high-quality images.

44
3.4 DICOM (Digital Imaging and Communications in Medicine)

File Extension: .dcm

Best For: Storing and transmitting medical images, including MRI, CT scans, X-
rays, and ultrasound images.

Compression: Lossless or lossy (optional)

Example: A DICOM file could represent a 3D MRI scan of a patient's brain, with
metadata containing patient details, date of the scan, and information about the
scanning device.

45
3.5 NIFTI (Neuroimaging Informatics Technology Initiative)

File Extension: .nii or .nii.gz (compressed)

Best For: Storing and processing 3D brain imaging data, such as MRI, fMRI, and
PET scans.

Compression: Lossless (compressed version: .nii.gz)

Example: A NIFTI file could store a 3D MRI scan of a brain, with the image data
representing the structural brain tissue, and the metadata providing details
about the scanning parameters and the experiment.

46
3.6 Analyze AVW (Analyze 7.5)

File Extension: .img (image data), .hdr (header)

Best For: Storing 3D medical imaging data, often used in neuroimaging.

Compression: Lossless (no native compression)

Key Features: The Analyze AVW format is one of the oldest formats used in
neuroimaging and medical image analysis.

It stores image data in a separate .img file and the corresponding metadata
(including dimensions, data type, and orientation) in the .hdr header file.

47
3.7 TIFF (Tagged Image File Format)

File Extension: .tiff or .tif

Best For: High-quality images for photography, scanning, and desktop publishing.

Compression: Lossless (e.g., LZW) or lossy (e.g., JPEG)

Key Features: TIFF is a flexible image format often used for high-quality images in
photography and printing.

48
3.8 ICS (Image Cytometry Standard) Cytometry is the measurement of the properties of
cells such as concentration, size, morphology,
File Extension: .ics surface proteins, and nucleic acid content amongst
others.
Best For: Storing image data from cytometry (e.g., flow cytometry or image-
based cytometry).

Compression: Typically lossless, depending on the software.

Key Features: The format is used for storing fluorescent microscopy images that
contain cellular data, such as cell counts, fluorescence intensity, and other data
collected during cytometry experiments.ICS files store not only the image data
but also the metadata and experimental parameters that help interpret the
image data (e.g., cell size, shape, and fluorescence properties).
49
3.9 IMS (Image Management System)

File Extension: .ims (or other proprietary extensions)

Best For: Storing image data for microscopy and scientific imaging.

Compression: Lossless or variable, depending on the software used.

Key Features: IMS files are often used in scientific microscopy and image analysis,
particularly for storing high-dimensional images such as those captured from
microscopes or confocal imaging systems.

50
Example: Reading, saving, and displaying an image
import matplotlib.pyplot as plt
import imageio.v3 as iio

# Step 1: Read the image


image_path = 'saint.jpg' # Replace with your image path
image = iio.imread(image_path)

# Step 2: Display the image


plt.imshow(image)
plt.axis('off') # Hide axes for better viewing
plt.title("Displayed Image")
plt.show()

51
# Step 3: Save the image
output_path = 'saved_image.png' # Replace with desired save path
plt.imsave(output_path, image)

print(f"Image saved successfully at {output_path}")

The f allows us to directly include


variables or expressions within a
string by enclosing them in curly
braces {}.

Image saved successfully at saved_image.png

52
Example: Converting from one image mode into another
L (Grayscale):

Represents a grayscale color mode. The L


stands for Luminance, which is the intensity
or brightness of a pixel.

Each pixel has a single intensity value, ranging


from 0 (black) to 255 (white) in an 8-bit
image.

Use Case: Black-and-white photographs,


scientific imaging, or when color information
is unnecessary.
53
RGBA (Red, Green, Blue, Alpha):

A color mode with four components:

R: Red
G: Green
B: Blue
A: Alpha (transparency)

Use Case: Digital images where transparency is required


(e.g., PNGs for web graphics).

54
CMYK (Cyan, Magenta, Yellow, Black):

A color model used for printing.

C: Cyan
M: Magenta
Y: Yellow
K: Key (Black)

Use Case: Printing (e.g., books, brochures) since printers mix inks in the
CMYK model.

55
from PIL import Image
import matplotlib.pyplot as plt

# Step 1: Open an image


image_path = 'rip current.jpg' # Replace with your image path
image = Image.open(image_path)

# Step 2: Convert the image to another mode


# Convert from RGB to grayscale (L)
grayscale_image = image.convert('L')

# Convert from RGB to RGBA (adding transparency)


rgba_image = image.convert('RGBA')

# Convert from RGB to CMYK


cmyk_image = image.convert('CMYK')
56
# Step 3: Save the converted images
grayscale_image.save('grayscale_image.jpg')
rgba_image.save('rgba_image.png')
cmyk_image.save('cmyk_image.tiff')

# Step 4: Display the converted images using matplotlib

# Display grayscale image (converted to RGB for proper


visualization)
plt.figure(figsize=(5, 5))
plt.imshow(grayscale_image.convert('RGB')) # Convert to RGB
for better visualization
plt.axis('off')
plt.title('Grayscale Image')
plt.show()

57
# Display RGBA image (converted to RGB for proper visualization)
plt.figure(figsize=(5, 5))
plt.imshow(rgba_image.convert('RGB')) # Convert to RGB for
better visualization
plt.axis('off')
plt.title('RGBA Image')
plt.show()

# Display CMYK image (converted to RGB for proper visualization)


plt.figure(figsize=(5, 5))
plt.imshow(cmyk_image.convert('RGB')) # Convert to RGB for
better visualization
plt.axis('off')
plt.title('CMYK Image')
plt.show()

print("Images converted and saved successfully.")


58
59
Example: Image Manipulation

60
3(a) Print image size, mode and format

from PIL import Image # Import the Pillow library


import imageio.v3 as iio # Optional: Use imageio for reading
images if needed

# Step 1: Read the image using Pillow


image_path = 'sunamganj.png' # Replace with your image path
im = Image.open(image_path)

# Step 2: Print image properties


print(f"Width: {im.width}, Height: {im.height}, Mode:{im.mode},
Format: {im.format}")

Width: 4080, Height: 1884, Mode: RGBA, Format: PNG


61
3(b) Cropping image
from PIL import Image
# Step 1: Open the image
image_path = 'sunamganj.png' # Replace with your image path
im = Image.open(image_path)
# Step 2: Define the cropping box
# (left, upper, right, lower) - these are pixel coordinates
crop_box = (50, 50, 300, 300) # Example: Crop a rectangle
starting from (50,50) to (300,300)
# Step 3: Perform the cropping
cropped_image = im.crop(crop_box)
# Step 4: Save or display the cropped image
import matplotlib.pyplot as plt
# Convert the cropped image to displayable format
plt.imshow(cropped_image)
plt.axis('off') # Hide axes
plt.show() 62
3(c) Resizing image
from PIL import Image

# Step 1: Open the image


image_path = 'sunamganj.png' # Replace with your image path
im = Image.open(image_path)

# Step 2: Resize the image


# You can resize by specifying the new size (width, height) or a scaling
factor
new_size = (300, 300) # Resize to 300x300 pixels

resized_image = im.resize(new_size)

# Step 3: Save or display the resized image


resized_image.save('sunamganj_resized.png') # Save the resized image
plt.imshow(resized_image) # Display the resized image
plt.axis('off') # Hide axes
plt.show()
63
64
Example: Sampling and Quantization
4.1 Reading an Image
import matplotlib.pyplot as plt
import imageio.v3 as iio

# Step 1: Read the image


image_path = 'brain.jpg' # Replace with your image path
image = iio.imread(image_path)

# Step 2: Display the image


plt.imshow(image)
plt.axis('off') # Hide axes for better viewing
plt.title("Brain MRI Image")
plt.show()

65
4.2 Color (RGB) image to Gray Image Conversion

The original image is in three channels: Blue (B), Green (G), and Red (R). Each
pixel has three values: one for each channel.

Example of one pixel in BGR:

Conversion Formula:

OpenCV converts BGR to Grayscale using a weighted formula:

So, the pixel in the grayscale image will have a value of 162.
66
Output Image (Grayscale):

The resulting image has only one channel, where each pixel represents intensity
(brightness) as a value between 0 (black) and 255 (white).

67
4.2.1 Why Use Grayscale?

Simplifies Image Data: Processing a single intensity channel is faster than


handling three color channels.

Focus on Structure: Removes color information and highlights shapes, edges,


and textures.

Necessary for Many Algorithms: Techniques like thresholding, edge detection,


or face recognition often work better on grayscale images.
import cv2
# Convert to grayscale
gray_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
The function cv2.cvtColor() in OpenCV stands for "Color Conversion". It is used
to convert an image from one color space to another. 68
4.2.2 Convert from BGR to Grayscale:
gray_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
gray_image
BGR (Blue, Green, Red) image to Grayscale: It will create an image where pixel
values represent the intensity (brightness), with values from 0 (black) to 255
(white).

original_image gray_image 69
4.3 Sampling (downsampling) of the grayscale image
# --- Step 1: Sampling ---
# Define sampling rate (e.g., take every 5th pixel)
sampling_rate = 5
sampled_image = gray_image[::sampling_rate, ::sampling_rate] #
Downsample

sampling_rate = 5: This variable determines how often we sample the image.

A rate of 5 means we will select every 5th pixel both horizontally and vertically.
A higher value will result in more downsampling (fewer pixels being selected).

::sampling_rate: This is slicing syntax in Python. It selects every 5th element


from the gray_image array (the image data) both along the rows and columns.

70
❑ Vertical slicing (::sampling_rate): This means every 5th row will be selected
from the image.

❑ Horizontal slicing (::sampling_rate): This means every 5th column will be


selected.

71
4.4 Sampled Coordinates
# Get sampled coordinates
rows, cols = sampled_image.shape
sampled_x, sampled_y = np.meshgrid(np.arange(0, cols), np.arange(0, rows))

rows, cols = sampled_image.shape

This line retrieves the shape (dimensions) of the sampled_image array.

sampled_image.shape returns a tuple (rows, cols), where:

rows: The number of rows (height) in the image.

cols: The number of columns (width) in the image

Tuples are used to store multiple items in a single variable. 72


sampled_x, sampled_y = np.meshgrid(np.arange(0, cols), np.arange(0, rows))

np.meshgrid() is a function in NumPy that creates coordinate grids from


coordinate vectors.

np.arange(0, cols) generates a range of numbers from 0 to cols-1 (i.e., the


column indices).

np.arange(0, rows) generates a range of numbers from 0 to rows-1 (i.e., the


row indices).
73
When combined with np.meshgrid(), this results in two 2D arrays:

sampled_x: Contains the column indices (repeated for each row).

sampled_y: Contains the row indices (repeated for each column).


74
Example:

Suppose we have a 4 x 4 image with rows = 4 and cols = 4. The code will
create coordinate grids like this:
sampled_x holds the sampled_x:
column indices. [[0, 1, 2, 3],
[0, 1, 2, 3],
sampled_y holds the [0, 1, 2, 3],
row indices. [0, 1, 2, 3]]

sampled_y:
[[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]] 75
import numpy as np

# Image dimensions (example)


image_width = 30
image_height = 30

# Sampling rate
sampling_rate = 5

# Sampled coordinates (every 5th pixel)


sampled_x = np.arange(0, image_width, sampling_rate) # x coordinates
sampled_y = np.arange(0, image_height, sampling_rate) # y
coordinates

# Create a grid of sampled coordinates


sampled_coordinates = [(x, y) for x in sampled_x for y in sampled_y]

print("Sampled Coordinates:", sampled_coordinates)


76
Sampled Coordinates: [(0, 0), (0, 5), (0, 10), (0, 15), (0, 20), (0, 25), (5, 0), (5, 5),
(5, 10), (5, 15), (5, 20), (5, 25), (10, 0), (10, 5), (10, 10), (10, 15), (10, 20), (10,
25), (15, 0), (15, 5), (15, 10), (15, 15), (15, 20), (15, 25), (20, 0), (20, 5), (20, 10),
(20, 15), (20, 20), (20, 25), (25, 0), (25, 5), (25, 10), (25, 15), (25, 20), (25, 25)]

4.5 Scaling Coordinates:

To map the coordinates of pixels in sampled_image back to their corresponding


positions in the original image, we need to scale the coordinates by the
sampling_rate.

# Scale coordinates to original image dimensions


original_x = sampled_x * sampling_rate
original_y = sampled_y * sampling_rate
77
Why Scale the Coordinates?

The reason we need to scale the coordinates is that in the sampled_image,


each pixel corresponds to multiple pixels in the original image.

For example, if we sample every 5th pixel, then the position of each pixel in the
sampled image needs to be mapped to a corresponding position in the original
image by multiplying by the sampling rate.

Without this scaling, the coordinates would correspond to positions that are off
by a factor of the sampling rate, leading to misalignment.

78
import numpy as np

# Image dimensions (example)


image_width = 30
image_height = 30

# Sampling rate
sampling_rate = 5

# Scaling factor
scaling_factor = 2

# Sampled coordinates (every 5th pixel)


sampled_x = np.arange(0, image_width, sampling_rate) # x
coordinates
sampled_y = np.arange(0, image_height, sampling_rate) # y
coordinates
79
# Create a grid of sampled coordinates
sampled_coordinates = [(x, y) for x in sampled_x for y in
sampled_y]

# Scale the coordinates


scaled_coordinates = [(x * scaling_factor, y * scaling_factor)
for (x, y) in sampled_coordinates]

print("Original Sampled Coordinates:", sampled_coordinates)


print("Scaled Coordinates:", scaled_coordinates)

Original Sampled Coordinates: [(0, 0), (0, 5), (0, 10), (0, 15), (0, 20), (0, 25), (5, 0), (5, 5), (5, 10), (5, 15), (5, 20),
(5, 25), (10, 0), (10, 5), (10, 10), (10, 15), (10, 20), (10, 25), (15, 0), (15, 5), (15, 10), (15, 15), (15, 20), (15, 25), (20,
0), (20, 5), (20, 10), (20, 15), (20, 20), (20, 25), (25, 0), (25, 5), (25, 10), (25, 15), (25, 20), (25, 25)]

Scaled Coordinates: [(0, 0), (0, 10), (0, 20), (0, 30), (0, 40), (0, 50), (10, 0), (10, 10), (10, 20), (10, 30), (10, 40), (10,
50), (20, 0), (20, 10), (20, 20), (20, 30), (20, 40), (20, 50), (30, 0), (30, 10), (30, 20), (30, 30), (30, 40), (30, 50), (40,
0), (40, 10), (40, 20), (40, 30), (40, 40), (40, 50), (50, 0), (50, 10), (50, 20), (50, 30), (50, 40), (50, 50)]
80
4.6 Quantization
# --- Step 2: Quantization ---
# Define the number of gray levels for quantization
num_gray_levels = 8
quantization_interval = 256 // num_gray_levels
quantized_image = (sampled_image // quantization_interval) *
quantization_interval

Since an image usually has pixel values from 0 to 255, dividing this range by
the number of gray levels gives the interval size.

Quantizes the pixel values by first dividing the pixel values of the sampled
image by the quantization interval (essentially flooring the values) and then
multiplying by the quantization interval to map them back to their
corresponding gray level.
81
Original Image:

Suppose we have a 2x2 sampled image with pixel values: sampled_image = [


[34, 89],
[180, 255]
Steps for Quantization: ]

Determine Quantization Interval:


quantization_interval=256 / 8=32

Quantize Each Pixel:

Divide each pixel value by the quantization_interval and floor it.


Multiply the result by quantization_interval.
82
Resulting Quantized Image:

quantized_image = [
[32, 64],
[160, 224]
]

83
The quantized image reduces the number of distinct pixel intensities from 256 to
8 levels: 0,32,64,96,…,224.

Each pixel in the original image is rounded down to the nearest quantization
level.

This process reduces the file size or visual detail, often used for compression or
image processing tasks.

84
Full code for sampling and quantization
import cv2
import numpy as np
import matplotlib.pyplot as plt

# Step 1: Read the image (original and grayscale)


image_path = "brain.jpg" # Replace with your image path
original_image = cv2.imread(image_path) # Read the image in color (for
visualization)

# Convert to grayscale
gray_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)

# --- Step 1: Sampling ---


# Define sampling rate (e.g., take every 5th pixel)
sampling_rate = 5
sampled_image = gray_image[::sampling_rate, ::sampling_rate] # Downsample

85
# --- Step 2: Quantization ---
# Define the number of gray levels for quantization
num_gray_levels = 8
quantization_interval = 256 // num_gray_levels
quantized_image = (sampled_image // quantization_interval) *
quantization_interval

# --- Visualization ---


fig, axes = plt.subplots(1, 3, figsize=(15, 5))

# Original grayscale image


axes[0].imshow(gray_image, cmap="gray")
axes[0].set_title("Original Grayscale Image")
axes[0].axis("off")

86
# Sampled image
axes[1].imshow(sampled_image, cmap="gray")
axes[1].set_title("Sampled Image")
axes[1].axis("off")

# Quantized image
axes[2].imshow(quantized_image, cmap="gray")
axes[2].set_title(f"Quantized Image ({num_gray_levels}
Levels)")
axes[2].axis("off")

plt.tight_layout()
plt.show()

87
88

You might also like