[go: up one dir, main page]

0% found this document useful (0 votes)
23 views38 pages

Digital Image Processing

The document covers various aspects of color image processing, including techniques like pseudo-coloring, intensity slicing, and color transformations. It discusses the importance of image segmentation for object detection and analysis, highlighting methods such as edge-based and region-based segmentation. Additionally, it addresses challenges like noise reduction, color correction, and image registration, along with their applications in fields like medical imaging and robotics.

Uploaded by

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

Digital Image Processing

The document covers various aspects of color image processing, including techniques like pseudo-coloring, intensity slicing, and color transformations. It discusses the importance of image segmentation for object detection and analysis, highlighting methods such as edge-based and region-based segmentation. Additionally, it addresses challenges like noise reduction, color correction, and image registration, along with their applications in fields like medical imaging and robotics.

Uploaded by

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

Slides Content

1. Eye cannot distinguish more than about 2 dozen gray levels in an image.
2. where each gray scale (generally at least 256 levels for most displays) mapped to a color level
through a LUT.
3. The eye is extremely sensitive to color and can distinguish thousands of color values in a picture.
4. CLUT(Color lookup table)::
A mapping of a pixel value to a color value shown on a display device.
 in a grayscale image with levels 0, 1, 2, 3, and 4, pseudo-coloring is a color lookup table that
maps 0 to black, 1 to red, 2 to green, 3 to blue, and 4 to white.

5. Intensity Slicing:-
The technique of intensity slicing or density slicing or color coding is one of the simplest
example of Pseudo-color image processing

6.
7.

8. g(x,y)=T[f(x,y)]
9. The pixel values here are triplets or quartets (i.e group of 3 or 4 values)
10. Si=Ti(r1,r2,…,rn) i=1,2,3,….n
11. ri and Si are variables denoting the color components of f(x,y)
12. and g(x,y) at any point (x,y).
13. n transformations combine to produce a single transformation T
14. If RGB color space is selected then n=3 & r1,r2,r3 denotes the red, blue and green components of the
image.
15. If CMYK color space is selected then n=4 & r1,r2,r3,r4 denotes the cyan, hue, magenta and black
components of the image.
16. Suppose we want to modify the intensity of the given image
using g(x,y)=k*f(x,y) where 0<k<1
17. HSI color space this can be done with the simple transformation

s3=k*r3
 where s1=r1 and s2=r2
 Only intensity component r3 is modified.
 In RGB color space 3 components must be transformed:

si=k*ri i=1,2,3.
In CMY color space 3 components must be transformed:

si=k*ri + (1-k) i=1,2,3.


 Using k=0.7 the intensity of an image is decreased by 30%
19. The hues(‫)شکل‬ opposite to one another on the Color Circle are called
Complements.

 Color Complement transformation is equivalent to image


negative in Grayscale images.

20. Image Slicing:


 Highlighting a specific range of colors in an image is useful for separating objects from
their surroundings.
 Displaythe colors of interest so that they are distinguished from
background.
 One way to slice a color image is to map the color outside some range of interest to a non
prominent neutral color.

Color Correction:-
 When adjusting the color components of an image it is important to realize that every
action affects the overall color balance of the image
(Perception of one color is affected by its surrounding colors)
 Based on the color wheel,
theproportion of any color can be increased by decreasing the amount
of the opposite color in the image
 increase by raising the proportion of two immediately
Similarly it can
adjacent colors or decreasing the percentage of the two colors adjacent to
the complement
 Suppose for example there is an abundance of magenta in an RGB image, it can decreased by

 Reducing both red and blue or


 Adding Green
Histogram Processing:-
 Color images are composed of multiple components,

 however it is not suitable to process each plane independently in case of


histogram equalization. This results in erroneous color.
 A more logical approach is to spread the color intensities uniformly, leaving the
colors themselves( hue, saturation) unchanged.

Color Image Smoothing:


 Color images can be smoothed in the same way as gray scale images, the difference is that
instead of scalar gray level values we must deal with component vectors of the following
form:
Noise in Color Image:
 Noise in color images can be removed through various noise models which we use in
Image Restoration in case the noise content of a color image has the same
characteristics in each color channel.
 color channels to be affected differently by noise so in this case noise are removed
from the image by independently processing each plane.
 applying smoothing filters (e.g gaussian, average, median) to each plane individually
and then combine the result.

Image Compression:
 Compression is the process of reducing or eliminating redundant and/or irrelevant
information
 A compressed image is not directly displayable it must be decompressed before input
to a color monitor.
 In case if in a compressed image 1 bit of data represents 230 bits of data in the
original image, then compressed image could be transmitted over internet in 1
minute as compared to original image which will take 4 hours to transmit.

FINAL TERM
1. Introduction to Color Image Processing
 Color Image Processing involves manipulating color images to enhance details or extract
useful information.
 Pseudo-color Processing (False Color):
Adds colors to grayscale images to highlight differences.

The human eye can see thousands of colors but struggles with subtle(‫ )نرم رنگتیں‬shades of
gray, so this method helps clarify details.

2. Pseudo-coloring Using LUT (Lookup Table)


 Color Lookup Table (CLUT):

Matches pixel values to specific colors.


o Example:
In a grayscale image with values 0-4:
 0 → Black
 1 → Red
 2 → Green
 3 → Blue
 4 → White

 This technique improves visualization by replacing shades of gray with distinct colors.
3. Intensity Slicing
 Divides grayscale intensity levels (e.g., black to white) into smaller sections and assigns
each section a specific color.

 Makes details more apparent by converting intensity ranges into colors.

4. Basics of Full-Color Image Processing


 Two methods:

1. Process Each Color Component Separately:


Treat red, green, and blue (RGB) parts individually, then combine them.
2. Process Color Pixels Directly:
Treat each pixel as a vector of values (e.g., RGB or CMYK).

5. Color Transformations
 Transformation Formula:
Adjust input image colors to create an output image using mathematical rules.
 The number of components
(e.g., RGB = 3, CMYK = 4) determines the
transformations applied.

6. Color Complements

 Complementary colors (e.g., red and green) are opposite on the color wheel.
Adjusting complements helps balance the overall color in an image.

7. Color Slicing
 Highlights specific color ranges while suppressing/ignoring others.
 Useful for distinguishing objects from the background in an image.
8. Color Correction:
 Fixes color imbalances using:
o Increasing complementary colors to decrease a dominant one.
o Adjusting adjacent colors on the color wheel.
9. Histogram Processing
 Adjusts brightness or contrast without changing the image's hue (color) or
saturation (vividness).
 HSI (Hue, Saturation, Intensity) is ideal for such tasks.
10. Color Image Smoothing:
 Similar to grayscale smoothing but applied to RGB or other color
components.
 Uses neighborhood averaging to reduce noise and create a smoother image.

11. Color Image Sharpening:


 Increases the clarity of edges and fine details in the image.

 Techniques like high-pass filtering are used to enhance sharpness.


12. Noise in Color Images
 Noise affects color channels differently:
o Uniform Noise:

Use filters (e.g., Gaussian or median) on all channels.


o Unequal Noise:

Process each channel separately.


13. Color Image Compression

 Reduces the size of an image by removing redundant data.


 Compressed images save space and bandwidth but must be decompressed before
use.
Example:
A compressed file might reduce data transmission time from hours to minutes.

Lecture 10
What is Image Segmentation?
 Definition:

Image segmentation splits an image into smaller parts or regions, called segments.
This helps analyze specific parts of the image better.

 Purpose:

It's the first step in automated computer vision tasks and helps identify
objects within an image.

Why is Image Segmentation Important?


 Efficiency:

Instead of analyzing the entire image, it focuses on the meaningful


parts.

 Applications:
o Detecting objects more precisely (e.g., cancer cells in medical
imaging).
o Understanding shapes and features of objects in detail.
o Used in traffic systems, self-driving cars, and satellite imaging.
Object Detection vs. Image Segmentation
 Object Detection:

Creates boxes around objects but doesn't detail their shape.


 Image Segmentation:

Assigns each pixel to an object, giving a more detailed view.

Segmentation Approaches
1. Discontinuity-Based:

Detects sudden changes, like edges in an image.

2. Similarity-Based:
Groups pixels that are similar in color, texture, or other
properties.

Types of Segmentation
1. Local Segmentation:

Focuses on specific parts of the image.


2. Global Segmentation:

Splits the entire image into regions.


Image Segmentation Techniques
1. Edge-Based:
o Focuses on edges or boundaries where pixel values change

sharply.
o Uses algorithms like Sobel, Canny, and Prewitt to detect edges.
2. Region-Based:
o Groups regions based on similar pixel values.
o Uses thresholds to classify areas.
3. Clustering:
o Groups pixels into clusters(Bunch) using techniques like K-means.
4. Texture-Based:
o Groups regions with similar texture patterns.
5. Color-Based:
o Groups pixels with similar colors.

Autonomous vs. Rugged Segmentation


 Autonomous:

Fully automated without human involvement.

 Rugged:
Complex but can identify individual objects in detail.
Edge Detection

 Detects boundaries where brightness changes.


 Helps extract important data for computer vision tasks.

Region Growing
 Starts with initial points (seeds) and grows regions based on
similarity.
 Applications:
Finding tumors in medical images or locating targets in satellite images.

What Are Image Features?


 Definition:
Characteristics like edges or points of interest in an image.
 Uses:
Helps with object recognition,

matching images, and more.

Discontinuity Detection
Identifies points, lines, or edges where there are sudden changes in
pixel intensity.

 Point Detection:

Finds small isolated regions.


 Line Detection:

Detects lines using specific filters.

What is Image Registration?


 two or more images of the same scene or object
It’s the process of aligning
taken from different angles, times, or using different methods (like
medical scans or satellite images).
 The goal is to align these images into the same system for easy comparison
or further analysis.

Steps in Image Registration


1. Feature Detection
o Find unique features (points, edges, regions) in each image.

Examples of feature detection methods:


 SIFT:

Finds patterns that stay the same despite(‫ )باوجود‬scaling or


rotation.

 SURF:

A faster version of SIFT.


 ORB, Harris Corner, FAST:

Other methods to detect features.

2.Feature Matching:
o Match similar features between images using techniques like:
 Euclidean Distance:
Measures how far two features are.

 Cross-Correlation:

Compares brightness patterns.

 Sum of Absolute Differences (SAD) or Normalized Cross-Correlation


(NCC).

3. Transformation Estimation:
o Use matched features to calculate how one image needs to be moved or
reshaped to align with the other.

Types of transformations:
 Rigid:

Moves and rotates without changing the shape.

 Affine:

Adds scaling and shearing (like stretching or compressing).

 Projective:
For images taken from different angles.

 Non-rigid:

For images of objects that can bend or stretch (like body


scans).

o A common method to refine these transformations is RANSAC, which avoids


errors caused by mismatched points.
4. Image Resampling & Refinement
o Modify one
image to match the other using interpolation
techniques (like bilinear or cubic).
o Improve alignment accuracy with extra methods like mutual information
or gradient-based optimization.
5.Fusion (Optional)
o Combine multiple aligned images into one for better analysis, often
used in medical or satellite images.

Types of Image Registration


1. Pixel-Based Methods
o Uses statistical methods like cross-correlation to find matches.

Drawback:
Slow and computationally heavy.

2. Point Mapping Methods:


o Focuses on matching specific features like corners or edges.
Drawback:

May struggle with areas that lack distinct features.


3. Contour-Based Methods:
o Matches features based on color boundaries or shapes.

Drawback:
Can be slow and often requires manual intervention.

4. Multimodal Registration (Using Mutual Information)


o Matches images from different sensors (like CT and MRI).

Drawback:

May fail if the image resolution is low or overlap is small.

5. Frequency Domain Methods


o Uses the Fourier transform to register images that only need shifting.

Drawback:
Requires interpolation, which can reduce accuracy.

6. Genetic Algorithms:
o Inspired by
evolution, this method iteratively finds the best alignment
using a population of solutions.

Works well for complex images, such as satellite data.

Applications of Image Registration


1. Medical Imaging
o Aligning scans (e.g., CT, MRI) to track diseases or compare data
from different tests.
2.Remote Sensing
o Aligning satellite images from different times to study changes in the
environment or land use.
3. Computer Vision
o Applications like

object recognition,

motion tracking,

creating panoramas, or

Reconstructing 3D models.
4. Robotics
o Helps robots
map their surroundings by combining data from
multiple sensors.

Slides Content
Image Segementation:
1. It is an important starting step in most of the scene analysis problems and automatic pictorial pattern
recognition.
2. Object Detection: But it tells us nothing about the shape of the object. We only get the set of bounding
box coordinates.
3. Image Segementation :This technique gives us a far more granular understanding of the object(s) in the
image.
4. There are many other applications where Image segmentation is transforming industries:
Traffic Control Systems
Self Driving Cars
Locating objects in satellite images
There are even more applications where Image Segmentation is very useful.
5. Global segmentation concerned with dividing the whole image. So, it consists of large number of pixels
as compared to local segmentation.
6. semantic segmentation:
Every pixel belongs to a particular class .All the pixels belonging to a particular class are represented by the same
color (background as black and person as pink).
7. instance segmentation:
has also assigned a particular class to each pixel of the image. However, different
objects of the same class have different colors (Person 1 as red, Person 2 as green,
background as black, etc.).
8. In this case, we use threshold value and set a specific threshold value.
Then pixel value classified according to that threshold value i.e. values below and above the threshold level.
This is called Threshold Segmentation.
9. Region growing is a simple region-based image segmentation method.
10. In Global threshold, we define a single threshold value and divide image into two
regions (object and background).
11. In Local threshold, we define a multiple threshold values and divide image according to that
multiple values into multiple regions.
12. This is used when image contains multiple objects along with the background.
13. Different regions are grouped on basis of similarities. These groups are called clusters.
14. Texture based Segemntation Then through applying Mean-Shift clustering.
15. similar texture patterns judging by the pair-wise region comparison operations.
16. Color Based Segmentation each cluster defines a class of pixels that share similar color properties.
17. Image features yield two different types of problem:
The detection of area of interest in the image, typically contours, and the
description of local regions in the image,
typically for matching in different images.
18.Image Segmentation:

Detection of discontinuities:

a. There are three basic types of gray-level discontinuities:


i. points , lines , edges
b. the common way is to run a mask through the image

Image registration
• SIFT (Scale-Invariant Feature Transform)
• SURF (Speeded-Up Robust Features)
• ORB (Oriented FAST and Rotated BRIEF)
• Harris Corner Detector
• FAST (Features from Accelerated Segment Test)

Boundary Description
In digital image processing, boundary description refers
to the representation and characterization
of the edges or contours( ‫ )حلیہ‬of an object in an
image.
It is a key step in object recognition, shape
analysis, and image segmentation.

Methods of Boundary Description:


• Chain Codes
• Polygon Approximation
• Fourier Descriptors
• Shape Signatures

Chain Codes:
• Chain codes are a way to represent the boundary of a shape or
an object using a sequence of numbers.
• Each number in the sequence represents a direction or a
movement along the boundary.
• Chain codes are used to describe the boundary of a shape or an object
in a compact and efficient way.

Working
Here's how it works:

Boundary Tracing:
The boundary of the shape
or object is traced, and the direction of
movement at each point is recorded.
Chain Code Generation:
The recorded directions are converted into a sequence of
numbers, which represents the chain code.
Boundary Representation:
The chain code is used to represent the boundary of the shape or object.

Polygon approximation:

Polygon approximation is a technique used to approximate a complex


curve or shape with a simpler polygonal shape.
This is done by selecting a subset of points from the original curve
or shape and connecting them to form a polygon.
There are several types of polygon approximation algorithms, including:

Douglas-Peucker Algorithm:
This algorithm is a popular method for polygon approximation.

It works by selecting the most important points on the curve or shape


and connecting them to form a polygon.
K-Means Algorithm:
This algorithm is a clustering algorithm that can be used for
polygon approximation.
It works by selecting a set of points on the curve or shape
and clustering them into K groups , where K is the number of
vertices in the polygon.

Fourier Descriptors:
Uses Fourier transform to represent the boundary in the frequency
domain, useful for shape analysis.
Shape Signatures:
Represents the boundary
as a 1D function, such as the radial
distance from a centroid.
Why is it Important ?
Object Recognition:
Computers can identify objects by their
shapes.
Image Segmentation:
Dividing an image into meaningful
parts.
Shape Analysis:
Comparing and classifying shapes.
Image Retrieval:
Finding similar images based on
shape.
APPLICATIONS:
• Identifying object shapes in an image.
• Analyzing geometric properties like area, perimeter,
or compactness.
• Feature extraction for machine learning and
computer vision tasks.
BOUNDARY REPRESENTATION:
1. Captures the shape of an object by tracing its edges or
contours.
2. Typically done using boundary pixels or coordinates.
3. Captures the shape of an object by tracing its edges or
contours.
4. Typically done using boundary pixels or coordinates.

Morphological Image Processing


This presentation explores the fascinating world of morphological
image processing, a powerful set of techniques used to analyze
and manipulate images based on their shape and structure.
Introduction to Morphological Image Processing
Definition:
Morphological image processing is a technique
that uses
mathematical morphology to analyze and modify image
shapes.
It involves manipulating image pixels based on their spatial
relationships with neighboring pixels.
Applications:
Morphological processing finds applications in various image processing tasks,
including:

• Noise reduction
• Object segmentation
• Boundary detection
• Feature extraction
• Shape analysis

Fundamental Morphological Operations


Dilation
Expands the boundaries of objects, filling in gaps and making them
thicker.

Erosion
Shrinks the boundaries of objects, removing small details and
thinning them.
Opening:
Combines erosion(‫ )کٹاؤ‬followed by dilation, effectively
smoothing the object boundaries and removing small objects.
Closing:
Combines dilation followed by erosion, filling in holes and
gaps in objects, and smoothing rough edges.

Erosion and Dilation


Erosion
Erosion uses a structuring element to "shrink" an image,
removing pixels that touch the structuring element's boundary. This
effectively thins the object's boundaries and removes small
details.
Dilation:
Dilation uses a structuring element to "grow" an image,
adding pixels to the object's boundary.

This fills in gaps and makes objects thicker.

Opening
Opening smooths object boundaries and removes small
objects.
It is used to remove noise and isolate individual objects.
• Definition:
Erosion followed by dilation.
• • Effect:
Removes small objects/noise.
• • Use Case:
Noise removal in binary images.
• • Visual Example.
Closing:-
Closing fills in holes and gaps in objects, smoothing out their
boundaries. It is used to fill in holes and connect broken objects.
• • Definition:

Dilation followed by erosion.

• • Effect:

Closes small holes in objects.

• • Use Case:

Filling gaps in objects.

• • Visual Example.
Gradiant:-
The morphological gradient highlights object edges by subtracting
the eroded image from the dilated image.
It provides a sharp boundary representation.
Top hat
The top hat transformation extracts the "bright"
details in an image
by subtracting the opened image from the original image.
It is useful for detecting small details.
Structuring Element:

• • Definition:
Matrix used for probing the image.
• • Types:
• - Rectangular
• - Circular
• - Custom shapes
• • Importance in Morphological Operations.
Example of morphological Algorithms:

• • Boundary Extraction:
Original - eroded version.
• • Region Filling: Iterative dilation.
• • Convex Hull: Simplifies object contours.
Application in Reallife:

• • Medical imaging (tumor detection)


• • Document image analysis
• • Shape analysis in remote sensing
• • Industrial inspection (defect detection)

Advantages and the limitations:


Advantages

• Simple, intuitive algorithms


• Efficient for binary images

Limitations:

• - Sensitive to structuring element size and shape

1. What Are Image Transforms?


Image transforms are techniques that change
how image data is represented,
making it easier to highlight certain features or characteristics. These are
widely used in tasks like:
 Compression:
Reducing file size.

 Filtering:

Removing noise.

 Feature Extraction:

Highlighting edges, textures, etc.

2. Types of Image Transforms

There are two main categories:

 Spatial Domain Transforms:

Work directly on the image's pixels, like smoothing or edge enhancement.


 Frequency Domain Transforms:

Convert images into their frequency components (e.g., Fourier Transform) to make
manipulation easier.

3. Key Transforms

A. Discrete Fourier Transform (DFT)


 Purpose:

Breaks an image into its frequency components.


 Applications:
o Compression:

Keeps important frequencies and removes others to save space.

o Filtering:

Removes noise or enhances specific features.

o Reconstruction:

Converts frequency data back into an image, especially in


medical imaging.
B. Discrete Cosine Transform (DCT)
 Purpose:

Represents an image as a sum of cosine waves, focusing on energy compaction


(most data in a small area).
 Applications:
o Image Compression:

Used in JPEG.
o Noise Reduction:

Removes unwanted data while preserving key details.

C. Haar Transform
 Purpose:

Uses differences and averages to transform an image into simpler forms.


 Applications:
o Compression:

Reduces image size effectively (e.g., JPEG 2000).

o Edge Detection:

Finds sharp intensity changes.

o Face Detection:

Used in algorithms like Viola-Jones.

4. Importance of Image Transforms

 Simplify image data for easier processing.


 Reduce storage size without losing important details.
 Enhance and prepare images for tasks like object recognition or filtering.

5. Limitations
 like Haar, may struggle with high-resolution or highly
Some transforms,
detailed images.
 Can introduce artifacts (unwanted distortions) during reconstruction.

Why This Is Useful

By understanding and using image transforms, engineers and researchers can process images more efficiently,
making tasks like compression and enhancement much easier in applications like photography, video
processing, and medical imaging.

Practical
The code in your file performs several digital image processing tasks in Python using libraries like OpenCV,
NumPy, and Matplotlib. Here’s a simple explanation of each part:

1. Mounting Google Drive

The code connects your Google Drive to access files stored there:

from google.colab import drive


drive.mount('/content/drive')

This allows the program to load an image (Einstein.jpg) stored on Google Drive.

2. Importing Libraries

It imports the necessary libraries for image processing:

 cv2: For working with images (reading, modifying, and displaying them).
 numpy: For mathematical operations on image data.
 matplotlib.pyplot: For showing images with titles.
 skimage: Additional utilities for image adjustments.

3. Displaying Images

The display_image function shows images with a title:

def display_image(title, image):


plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title(title)
plt.axis('off')
plt.show()
It uses Matplotlib to display images in the RGB color format.

4. Pseudo Coloring

Pseudo coloring applies a specific color map (e.g., COLORMAP_JET) to a grayscale image to make it visually
appealing:

def pseudo_coloring(image, colormap=cv2.COLORMAP_JET):


pseudo_image = cv2.applyColorMap(image, colormap)
return pseudo_image

Example Output: A grayscale image is transformed into a vibrant-colored version.

5. Color Transformations

Color transformations alter the image's colors using a transformation matrix:

def color_transform(image, matrix):


transformed = cv2.transform(image, matrix)
return np.clip(transformed, 0, 255).astype(np.uint8)

Here, a custom matrix is applied to enhance or modify the color tones of the image.

6. Color Correction (Gamma Correction)

Gamma correction adjusts the brightness of an image:

def color_correction(image, gamma=1.0):


gamma_corrected = np.power(image / 255.0, gamma) * 255.0
return np.clip(gamma_corrected, 0, 255).astype(np.uint8)

 Gamma > 1: Brightens the image.


 Gamma < 1: Darkens the image.

7. Color Complement

Color complement inverts the colors of the image:

def color_complement(image):
return 255 - image

Example Output: A negative of the image is produced (dark areas become bright, and vice versa).
8. Color Slicing

Color slicing highlights specific color ranges:

def color_slicing(image, lower_bound, upper_bound):


mask = cv2.inRange(image, np.array(lower_bound), np.array(upper_bound))
sliced_image = cv2.bitwise_and(image, image, mask=mask)
return sliced_image

Example Output: Parts of the image with colors within a certain range are kept; others are removed.

9. Removing Noise

The code removes noise using a denoising filter:


def remove_noise(image):
denoised = cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21)
return denoised

Example Output: A smoother and cleaner image with reduced noise.

General Workflow

1. The image (Einstein.jpg) is loaded in grayscale or color.


2. Each operation (e.g., pseudo coloring, gamma correction) is applied step-by-step.
3. The processed image is displayed for each operation.

Let me know if you want detailed guidance on any specific part!

This code demonstrates several image segmentation techniques using Python and image processing libraries.
Below is a simplified explanation of each part:

Image Segmentation

1. Importing Libraries

The code uses these libraries:

 OpenCV (cv2): For loading, processing, and displaying images.


 NumPy (np): For handling numerical operations.
 Matplotlib: For visualizing images.
 Scikit-image filters (sobel): To detect edges in images.
 Scikit-learn (KMeans): For clustering data.
2. Displaying Images

The display_image function is used to display images with titles. It takes the image and optionally a colormap
(e.g., for grayscale images) and shows the image using Matplotlib.

3. Loading the Input Image

The code reads an image file (micky.jpg) from Google Drive. If the file cannot be loaded, it displays an error
message. It converts the image from BGR (OpenCV's default format) to RGB for proper display.

4. Color-Based Segmentation

This technique isolates parts of the image based on color intensity ranges.

 How it works: A mask is created to include only pixels within a specific color range.
 Example: Detecting blue colors:
 lower_bound = np.array([0, 0, 50])
 upper_bound = np.array([200, 200, 255])

Pixels outside this range are ignored.

5. Region-Based Segmentation

This approach starts with a "seed point" and expands the region by including neighboring pixels with similar
intensity values.

 Example: Using a starting point (50, 50) with a small intensity difference threshold (5).
 Flood Fill: The algorithm grows the region like pouring water into a specific area.

6. Texture-Based Segmentation

This technique identifies regions based on texture patterns:

 How it works: Converts the image to grayscale and applies texture analysis (e.g., using Local Binary
Patterns or thresholding).
 Example Output: Regions with different surface textures (e.g., smooth vs rough) are separated.

7. Edge-Based Segmentation

Edge detection identifies boundaries between objects:


 How it works: Uses the Sobel filter to find intensity changes.
 Example Output: Outlines of objects (edges) are highlighted.

8. Clustering-Based Segmentation

This method groups similar pixels into clusters using K-Means Clustering.

 How it works:
1. Image data is reshaped into a list of pixel values.
2. The K-Means algorithm groups these pixels into clusters (e.g., 4 clusters).
3. Each cluster represents a region in the image.
 Example Output: The image is segmented into different regions based on pixel similarity.

Summary of Techniques

Technique Purpose
Color-Based Segmentation Isolate regions based on color.
Region-Based Segmentation Grow regions from a starting point (seed).
Texture-Based Segmentation Differentiate regions by texture.
Edge-Based Segmentation Highlight object boundaries (edges).
Clustering-Based Segmentation Group pixels into clusters (e.g., using K-Means).

Let me know if you want me to focus on any particular method in more detail!

Image Registration
This code is about Digital Image Processing and focuses on Image Registration—a process of aligning two or
more images of the same scene taken at different times, angles, or conditions. Let’s break down each method
and explain it in simple steps.

1. Pixel-Based Image Registration

This method aligns images by directly comparing pixel intensity values.

Key Steps:

1. Load Images: Two grayscale images are loaded (fixed and moving).
2. Resize: The moving image is resized to match the fixed image size.
3. Difference Image: The absolute difference between the pixel values of the two images is calculated.
4. Visualization:
o Shows the fixed image.
o Shows the resized moving image.
o Shows the difference between the two images.
Why it works:

This method assumes the images are already roughly aligned. If they are not, the difference image will highlight
the misalignment.

2. Feature-Based Image Registration

This method identifies unique features (corners, edges) in both images and aligns them.

Key Steps:

1. Load Images: Two grayscale images are loaded.


2. Detect Features: ORB (Oriented FAST and Rotated BRIEF) algorithm detects features in both images
and computes their descriptors (a kind of unique "signature").
3. Match Features: Matches the features using a method called BFMatcher.
4. Sort Matches: Matches are sorted based on quality (distance).
5. Draw Matches: Visualizes feature correspondences between the two images.
6. Compute Transformation:
o Finds a transformation matrix (Homography) to map points from one image to the other.
o Aligns the source image with the target using this transformation.
7. Visualization: Displays the original and aligned images side by side.

Why it works:

Features are robust to changes like scaling, rotation, and illumination. Matching these features provides accurate
alignment.

3. Contour-Based Image Registration

This method uses the shapes (contours) of objects in the images for alignment.

Key Steps:

1. Extract Contours:
o Convert both images to binary using a threshold.
o Find the contours (boundary lines of objects) in the images.
2. Draw Contours:
o Draw these contours on blank canvases for visualization.
3. Display:
o Shows the extracted contours of the fixed and moving images.

Why it works:

Contours capture the shapes of objects, making this method effective when pixel intensities differ.
4. Multimodal Image Registration Using Mutual Information

Used when images come from different sources (e.g., CT and MRI).

Key Steps:

1. Calculate Mutual Information:


o Mutual Information (MI) measures how well one image aligns with another based on shared
information.
2. Print MI Value:
o A higher value indicates better alignment.

Why it works:

This method does not rely on pixel intensities and is effective for images from different modalities.

5. Image Registration in the Frequency Domain

Aligns images by comparing their frequency components.

Key Steps:

1. Fourier Transform:
o Converts both images to the frequency domain using the Fast Fourier Transform (FFT).
2. Magnitude Spectrum:
o Computes and visualizes the magnitude spectrum (frequency components) of the images.
3. Display:
o Shows the frequency spectrum of the fixed and moving images.

Why it works:

In the frequency domain, certain transformations like translations or rotations become easier to identify.

6. Image Registration Using Genetic Algorithm

This method optimizes alignment using a Genetic Algorithm (GA).

Key Steps:

1. Objective Function:
o Defines a function to minimize (Sum of Squared Differences, SSD, between pixel values of
images).
2. Parameter Bounds:
o Sets bounds for rotation (-10° to 10°) and translation (-20 to 20 pixels in x and y directions).
3. Optimization:
o Uses the Genetic Algorithm to find the optimal alignment parameters.
4. Apply Transformation:
o Applies the optimal rotation and translation to align the moving image.
5. Display:
o Shows the fixed image and the aligned moving image.

Why it works:

GA explores possible alignments intelligently, finding the best match even in complex cases.

Summary Table:

Method Use Case Strength


Pixel-Based Images with slight misalignment. Simple but sensitive to noise.
Feature-Based Images with significant differences in rotation or scale. Robust and accurate.
Contour-Based Shape-based alignment. Effective for shape differences.
Mutual Information Multimodal images (e.g., CT vs. MRI). Works across different modalities.
Frequency Domain Global transformations (e.g., shifts). Efficient for certain problems.
Genetic Algorithm Complex cases with many parameters. Finds optimal transformations.

By following these steps, you can use different techniques for aligning images based on your specific
requirements. Let me know if you'd like to dive deeper into any part!

Segmentation
Here's a simplified explanation of the provided Digital Image Processing task. The goal is to apply Instance
Segmentation using a pre-trained Mask R-CNN model and visualize the results with colored masks.

Step-by-Step Explanation

1. Import Required Libraries

 Libraries like cv2, matplotlib, torch, and others are imported for handling images, deep learning
models, and visualization.

2. Load the Image

 The image is loaded using OpenCV (cv2.imread) or the Python Imaging Library (PIL.Image.open).
 OpenCV loads images in BGR format, so it's converted to RGB for compatibility with plotting tools
like matplotlib.

3. Display the Original Image

 The original image is displayed using plt.imshow() from Matplotlib to confirm it has been loaded
correctly.
4. Load Pre-Trained Mask R-CNN Model

 maskrcnn_resnet50_fpn is a pre-trained deep learning model from PyTorch’s torchvision library.


o It is used for Instance Segmentation, which detects individual objects and segments them in an
image.
 The model is set to evaluation mode (model.eval()), meaning it won’t update its weights during
inference.

5. Preprocess the Image

 The image is converted to a tensor (PyTorch's format for handling data) using F.to_tensor(image).
 A batch dimension is added using .unsqueeze(0) because models process images in batches.

6. Run the Model for Predictions

 The image tensor is passed through the model, and predictions are extracted.
 Predictions include:
o Boxes: Rectangles around detected objects.
o Labels: IDs of detected object categories.
o Scores: Confidence levels for each detection.
o Masks: Segmentation masks for detected objects.

7. Filter Predictions

 Only predictions with a confidence score above a specified threshold (e.g., 0.5) are kept.
 The corresponding masks, labels, and scores are filtered.

8. Define Mask Colors

 A list of predefined colors (e.g., Red, Green, Blue) is created for the masks.
 If there are more objects than colors, additional random colors are generated to ensure each object gets a
unique color.

9. Apply Colored Masks

 For each detected object:


o The segmentation mask is converted into a binary mask (True for object pixels, False for
background pixels).
o The corresponding color is applied to the object region in the image using blending:
 The original pixel value is averaged with the mask color (0.5 * original + 0.5 *
color).

10. Display the Result

 The modified image, now containing segmented objects with colored masks, is displayed using
plt.imshow().

Key Outputs

1. Original Image: Displayed to confirm it is loaded correctly.


2. Segmented Image: The final image showing objects with overlaid colored masks.

What Does It Accomplish?

 Identifies objects in the image (like birds, cars, etc.).


 Segments each object by coloring its region with a unique color.
 Provides a visual representation of instance segmentation.

This workflow is useful in tasks like object detection, scene understanding, and more, where identifying and
segmenting objects is critical.

You might also like