0% found this document useful (0 votes)
133 views111 pages

OpenCV Image Processing Techniques

Uploaded by

gunturkarampuppy
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)
133 views111 pages

OpenCV Image Processing Techniques

Uploaded by

gunturkarampuppy
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

IV-UNIT

Advanced image processing using open CV


(Open Source Computer Vision Library)
*Overview of OpenCV*
OpenCV (Open Source Computer Vision Library) is a popular library used for
computer vision tasks. It provides various functionalities for image processing,
including reading, displaying, and modifying images.
*Installing OpenCV*
Before you can use OpenCV, you need to install it. You can do this via pip:
>>>pip install opencv-python

import cv2
Blending is a technique used to combine two images into one. This can be useful
in various applications like creating effects, overlays, or transitions.

To blend two images using OpenCV in Python, you can use the [Link]()
function. This function allows you to specify the weights for each image, which
determines how much of each image will be included in the blended result.
The function [Link]() takes the two images and their respective weights,
along with the gamma value, and computes the blended result.

The formula used in OpenCV for blending two images is:


{Blended_image} = alpha x {Image1} + beta x {Image2} + gamma ]
Where:

- *Image1* and *Image2* are the original images.

- *α (alpha)* is the weight of the first image.

- *β (beta)* is the weight of the second image.

- *γ (gamma)* is a scalar value added to each sum (usually set to 0).

The values of α and β should be chosen such that their sum is typically equal to 1 for

a balanced effect. For example, if α is 0.5, then β should also be 0.5.

For blending, both images must have the same dimensions.


Blending Two Images
import cv2

# Load the two images


image1 = [Link](r"C:\Users\saimo\Pictures\standard_test_images\lena_color_256.tif")
image2 = [Link](r"C:\Users\saimo\Pictures\standard_test_images\[Link]")

# Check if the images are loaded properly


if image1 is None or image2 is None:
print("Error: Could not load one or both images.")
exit()

# Resize images to the same shape if they are not already the same size
image1 = [Link](image1, (640, 480))
image2 = [Link](image2, (640, 480))
# Set the blending weights
alpha = 0.5 # Weight for the first image
beta = 0.5 # Weight for the second image
gamma = 0.0 # Scalar added to each sum

# Blend the images


blended_image = [Link](image1, alpha, image2, beta, gamma)

# Display the blended image


[Link]('Blended Image', blended_image)
[Link](0)
[Link]()

# Optionally, save the blended image


[Link](r"C:\Users\saimo\Pictures\standard_test_images\ blended_image.tif",
blended_image)
[Link]('Blended Image', blended_image)
[Link](0)
[Link]()
- [Link]() creates a window displaying the blended image.
- [Link](0) waits indefinitely until a key is pressed.
- [Link]() closes the window after the key is pressed.

[Link]('blended_image.tif', blended_image)
This saves the resulting image as 'blended_image.tif’ in the current working
directory.
Changing Contrast and Brightness of the given image using open CV
import cv2
import numpy as np

def adjust_brightness_contrast(image, brightness=0, contrast=0):


# Adjust brightness and contrast
# Brightness can be any integer from -255 to +255
# Contrast can be any integer from -127 to +127
beta = brightness # Brightness
alpha = 1 + contrast / 127.0 # Contrast scaling factor

# Apply the brightness and contrast adjustments


adjusted_image = [Link](image, alpha=alpha, beta=beta)

return adjusted_image
# Load the image
image_path = r"C:\Users\saimo\Pictures\standard_test_images\[Link]"
image = [Link](image_path)

if image is None:
print("Error: Unable to load image.")
else:
# Adjust brightness and contrast
brightness = 50 # Change this value as needed
contrast = 30 # Change this value as needed

adjusted_image = adjust_brightness_contrast(image, brightness, contrast)

# Display the original and adjusted images


[Link]('Original Image', image)
[Link]('Adjusted Image', adjusted_image)

# Wait until a key is pressed and then close the windows


[Link](0)
[Link]()

# Optionally, save the adjusted image


[Link]('adjusted_image.jpg', adjusted_image)
Explanation
- *Brightness*: The brightness parameter is added directly to the pixel values. It can
range from -255 to +255.
- *Contrast*: The contrast parameter is used to scale the pixel values.

The formula used is alpha = 1 + contrast / 127.0,


where alpha is the scaling factor.
- The function [Link]() is used to apply these adjustments efficiently.
*Function Definition*: This function takes an image along with brightness and
contrast values as input.
- *Parameters*:
- image: The image to be processed.
- brightness: An integer to adjust brightness (default is 0).
- contrast: An integer to adjust contrast (default is 0).

#### Inside the Function:


python
beta = brightness # Brightness
alpha = 1 + contrast / 127.0 # Contrast scaling factor
- *Brightness (beta)*: This value is directly added to the pixel values of the image. It
can range from -255 (darkening) to +255 (brightening).
- *Contrast (alpha)*: This scaling factor is computed based on the input contrast
value. The formula used ensures that:
- A contrast value of 0 means no change (alpha = 1).
- Increasing contrast beyond 0 enhances the differences between light and dark
pixels.
- The formula 1 + contrast / 127.0 effectively normalizes the contrast adjustment.
adjusted_image = [Link](image, alpha=alpha, beta=beta)

- *[Link]()*: This function applies the linear transformation to the


image:

{output image} = {input image} x alpha + beta

It converts the result to an absolute value (ensures pixel values remain within valid
range) and handles overflow by clipping values at 255 (the maximum for an 8-bit
image).
#Adding a text to an image
import cv2
# Load the image
image_path = r"C:\Users\saimo\Pictures\standard_test_images\[Link]"
image = [Link](image_path)

if image is None:
print("Error: Unable to load image.")
else:
text = "SVR ENGINEERING COLLEGE“ # Define the text to be added
# Set the font, scale, color, and thickness
font = cv2.FONT_HERSHEY_SIMPLEX # Font type
scale = 1 # Font scale (size)
color = (255, 255, 255) # White color in BGR (Blue, Green, Red)
thickness = 2 # Thickness of the text

# Position for the text (x, y coordinates)


position = (50, 100) # Change these values as needed
# Put the text on the image
[Link](image, text, position, font, scale, color, thickness, cv2.LINE_AA)

# Display the image with the text


[Link]('Image with Text', image)

# Wait until a key is pressed and then close the window


[Link](0)
[Link]()

# Optionally, save the image with the text

[Link](r"C:\Users\saimo\Pictures\standard_test_images\image_with_text.tif",
image)
Image smoothing is a fundamental process in digital image processing aimed at reducing noise
and improving the quality of an image. It is often used as a preprocessing step in computer vision
tasks to enhance the performance of algorithms by minimizing noise that could interfere with
feature extraction or recognition processes.
There are several types of filters used for smoothing images, each with its characteristics and
applications:

1. *Mean Filter*: A simple linear filter that replaces each pixel value with the average of its
neighbors. It effectively reduces noise, but can also blur edges.

2. *Gaussian Filter*: A more sophisticated linear filter that applies a Gaussian function to weight
pixels differently based on their distance from the center. It reduces noise while preserving edges
better than the mean filter.
3. *Median Filter*: A nonlinear filter that replaces each pixel with the median of the pixel
values in its neighborhood. It is particularly effective at removing salt-and-pepper noise while
preserving edges.

4. *Bilateral Filter*: Combines domain and range filtering to preserve edges while smoothing
images. It uses both spatial distance and intensity difference to weight pixels.

### The Median Filter

The median filter is a widely used technique for noise reduction in images. Its primary
advantage is its ability to remove noise without blurring edges, making it ideal for applications
where edge preservation is crucial.
How the Median Filter Works

1. *Kernel Definition*:
- The filter uses a small, square-shaped kernel (e.g., 3x3, 5x5) that slides over the image.
- The kernel size is typically an odd number to ensure there's a central pixel.

2. *Sliding Window Approach*:


- The kernel moves across the image from the top-left to the bottom-right corner.
- For each pixel, the values within the kernel's window are extracted.

3. *Calculating the Median*:


- The extracted values are sorted, and the median value is determined.
- This median replaces the original pixel value at the center of the kernel.

4. *Border Handling*:
- Special strategies are employed at the image borders, such as padding with zeros, mirroring
the image, or using a smaller kernel.
Advantages of the Median Filter
- Edge Preservation: Unlike linear filters, the median filter preserves sharp edges, making it
suitable for images with significant detail.

- Noise Reduction: Highly effective at eliminating salt-and-pepper noise, common in digital


images due to transmission errors or sensor faults.

- Non-linear Nature: The non-linear operation helps in maintaining the integrity of significant
features while altering less critical data points.
Practical Applications

1. Medical Imaging: Enhancing images from medical scans (e.g., MRI, CT) by reducing noise
while preserving anatomical structures.

2. Astronomy: Cleaning astronomical images to remove noise from sensors while maintaining
star and galaxy edges.

3. Photography: Reducing noise in digital photographs, especially in low-light conditions,


without losing detail.

4. Surveillance: Improving the clarity of video frames in security footage, which often suffer
from various types of noise.

5. Robotics and Autonomous Vehicles: Preprocessing images captured by sensors to ensure


reliable feature extraction for navigation and object recognition.
Limitations
- Computationally Intensive: Sorting pixel values for each kernel position can be
computationally expensive, especially for large kernels or high-resolution images.

- Ineffective for Gaussian Noise: While excellent for impulsive noise, the median
filter may not be as effective for Gaussian noise, where Gaussian filters might be
preferred.

- Image Border Handling: Managing the edges of the image can be challenging,
requiring specific strategies to avoid artifacts.
Smoothing images using Median Filter:
You can smooth images using a median filter in Python with the help of the OpenCV
library. The median filter is effective in reducing noise while preserving edges in an
image.

#Image Smoothing using Median Filter:


import cv2
# Load the image
image_path = r"C:\Users\saimo\Pictures\standard_test_images\[Link]"
image = [Link](image_path)
if image is None:
print("Error: Unable to load image.")
else:
# Apply median filter
# The second parameter is the kernel size; it must be an odd number.
kernel_size = 3 # You can change this value to adjust the level of smoothing
smoothed_image = [Link](image, kernel_size)

[Link]('Original Image', image) # Display the original images


[Link]('Smoothed Image', smoothed_image) # Display the smoothed images

# Wait until a key is pressed and then close the windows


[Link](0)
[Link]()

# Optionally, save the smoothed image


[Link]('smoothed_image.jpg', smoothed_image)
# Image Smoothing using Gaussian Filter
import cv2
# Load the image
image_path = r"C:\Users\saimo\Pictures\standard_test_images\gaussian_noisy_image.jpg"
image = [Link](image_path)

if image is None:
print("Error: Unable to load image.")
else:
# Apply Gaussian filter
# The first parameter is the source image,
#the second parameter is the kernel size (must be odd) and
#the third is the standard-deviation in the X and Y directions.
kernel_size = (5, 5) # Must be odd numbers
sigma = 1.5 # Standard deviation for the Gaussian kernel
smoothed_image = [Link](image, kernel_size, sigma)
# Display the original and smoothed images
[Link]('Original Image', image)
[Link]('Smoothed Image', smoothed_image)

# Wait until a key is pressed and then close the windows


[Link](0)
[Link]()

# Optionally, save the smoothed image


[Link](r"C:\Users\saimo\Pictures\standard_test_images\smoothed_image.tif",
smoothed_image)
1. [Link]:
- This is a function provided by the OpenCV library (cv2 is the module name).
- The GaussianBlur function applies a Gaussian blur to an image, which is a common
technique used in image processing to reduce noise and detail.
2. Parameters:
- The GaussianBlur function takes three main parameters: the source image, the kernel size,
and the standard deviation (sigma).
3. image:
- This is the input image that you want to smooth. It should be a valid image array, typically
read into a variable using [Link]().
- The image can be either grayscale or color (RGB format).
4. kernel_size:
- This parameter specifies the size of the Gaussian kernel (filter).
- It is usually represented as a tuple, e.g., (width, height). The dimensions must be odd numbers
(e.g., (3, 3), (5, 5), (7, 7)) to ensure there is a center pixel in the kernel.
- The kernel size determines how much of the image is considered when applying the blur. A
larger kernel size will result in more blurring.
5. sigma:
- This parameter represents the standard deviation of the Gaussian distribution.
- It affects the amount of blurring applied to the image. A larger sigma value will produce a more
pronounced blur, while a smaller sigma will have a milder effect.
- If sigma is set to 0, OpenCV will automatically calculate it based on the kernel size.
The Gaussian blur works by convolving the image with a Gaussian function, which is
characterized by its bell-shaped curve. The Gaussian function assigns weights to the pixel values
based on their distance from the center pixel in the kernel:

1. Weight Calculation:
- The weights are determined by the Gaussian function:

G(x , y) = [1 / (2 x π x σ^2)] x e^[-(x^2 + y^2) / (2 x σ^2)]

Where:
- (x, y) are the distances from the center of the kernel.
- σ is the standard deviation.

2. Convolution:
- Each pixel value in the output image is calculated as a weighted sum of the neighboring pixels,
where the weights are derived from the Gaussian function.
- This results in a smoothing effect, where pixel values are blended with their neighbors
according to the Gaussian weights.
# Image Smoothing using Bilateral Filter
import cv2
# Load the image
image_path = 'path_to_your_image.jpg' # Replace with your image path
image = [Link](image_path)
if image is None:
print("Error: Unable to load image.")
else:
# Apply Bilateral filter
# The first parameter is the source image.
# The second parameter is the diameter of the pixel neighborhood.
# The third parameter is the sigma in the color space.
# The fourth parameter is the sigma in the coordinate space.
diameter = 15 # Diameter of the pixel neighborhood
sigma_color = 75 # Filter sigma in color space
sigma_space = 75 # Filter sigma in coordinate space
smoothed_image = [Link](image, diameter, sigma_color, sigma_space)
# Display the original and smoothed images
[Link]('Original Image', image)
[Link]('Smoothed Image', smoothed_image)

# Wait until a key is pressed and then close the windows


[Link](0)
[Link]()

# Optionally, save the smoothed image


[Link]('smoothed_image_bilateral.jpg', smoothed_image)
Overview of Bilateral Filtering

Bilateral filtering is a non-linear, edge-preserving, and noise-reducing smoothing filter.

Unlike linear filters (like Gaussian or mean filters), which apply a uniform smoothing effect
across the entire image, the bilateral filter considers both the spatial distance of pixels and their
color similarities.

This allows it to smooth areas of the image while preserving sharp edges, making it effective for
applications where detail is important.
Function Signature

The function signature for [Link] is as follows:

python
dst = [Link](src, d, sigmaColor, sigmaSpace, borderType=cv2.BORDER_DEFAULT)

### Parameters

1. *src*:
- This is the source image that you want to process. It can be a grayscale or a color (BGR)
image.

2. *d*:
- This parameter specifies the diameter of the pixel neighborhood that is used during
filtering. It determines how many neighboring pixels will influence the output pixel.
- A larger value means more pixels are considered for the filter, leading to greater
smoothing.
- If set to 0, it will be calculated based on sigmaSpace.
3. *sigmaColor*:
- This parameter defines the filter sigma in the color space. It controls how much the colors of
neighboring pixels influence each other during filtering.
- A larger value allows for a wider range of colors to blend together, leading to more significant
smoothing.
- If two pixels are close in color (within sigmaColor), their values will be averaged.

4. *sigmaSpace*:
- This parameter defines the filter sigma in the coordinate space. It controls how much the spatial
distance influences the filtering.
- A larger value means that pixels further away from the center pixel will still have a significant effect
on the output.
- This parameter helps maintain edges by reducing the effect of distant pixels in areas with
significant color differences.

5. *borderType* (optional):
- This parameter defines how the borders of the image are handled. The default is
cv2.BORDER_DEFAULT, but other options like cv2.BORDER_REFLECT, cv2.BORDER_CONSTANT, etc., can
be used to manage the image boundaries.
How Bilateral Filtering Works

1. Weight Calculation:

- For each pixel in the image, the bilateral filter calculates weights based on both spatial and
color differences from the center pixel.
- The weights are calculated using Gaussian functions for both the color differences and spatial
distances as follows:
2. *Normalization and Output Calculation*: The output pixel value is calculated using the
following equation:

- The output pixel value = (Summation of weighted neighboring pixel values) / (the sum of the
weights of all neighboring pixels), i.e.,
I’ = [sum (w_i x I_i)] / [sum (w_i)]
BROWNIAN NOISE
(Fractal Noise)
Changing the shape of Images
Image Shape Transformation

Changing the shape of images refers to altering the dimensions or structure of an


image.
This can include operations such as resizing, cropping, rotating, flipping, and warping
images.
These transformations are fundamental in computer vision, image processing, and
graphics applications for various purposes, such as preparing images for machine
learning, adjusting images for display, and creating visual effects.
Common Image Shape Transformations

1. Resizing:
- Changing the dimensions of the image (width and height).
- Can be done to fit images into a specific format or to scale them for processing.
- Maintains the aspect ratio or changes it based on the requirement.

2. Cropping:
- Removing a portion of the image to focus on a specific area.
- Useful for highlighting subjects or removing unwanted parts of an image.

3. Rotating:
- Rotating the image around its center by a specified angle.
- Commonly used to correct orientation or create artistic effects.
4. Flipping:
- Mirroring the image along a specified axis (horizontal or vertical).
- Useful for creating symmetrical designs or augmenting datasets.

5. Warping:
- Applying a non-linear transformation to alter the shape of the image.
- Can be used for artistic effects or to correct perspective distortions.
### Python Code for Changing the Shape of Images

Below is a Python script that demonstrates how to perform several common shape
transformations using OpenCV.
#Changing the shape of the image
import cv2

# Load the image


image_path = r"C:\Users\saimo\Pictures\standard_test_images\[Link]"
image = [Link](image_path)

if image is None:
print("Error: Unable to load image.")
else:
# 1. Resizing
new_size = (300, 300) # Width, Height
resized_image = [Link](image, new_size)

# 2. Cropping
# Define the coordinates for cropping (y_start:y_end, x_start:x_end)
cropped_image = image[50:250, 100:300] # Adjust these values as needed
# 3. Rotating
angle = 45 # Angle of rotation
(h, w) = [Link][:2]
center = (w // 2, h // 2) # Center of the image
matrix = cv2.getRotationMatrix2D(center, angle, 1.0) # Rotation matrix
rotated_image = [Link](image, matrix, (w, h))

# 4. Flipping
flipped_image_horizontal = [Link](image, 1) # 1 for horizontal flip
flipped_image_vertical = [Link](image, 0) # 0 for vertical flip

# Display all transformed images


[Link]('Original Image', image)
[Link]('Resized Image', resized_image)
[Link]('Cropped Image', cropped_image)
[Link]('Rotated Image', rotated_image)
[Link]('Flipped Image (Horizontal)', flipped_image_horizontal)
[Link]('Flipped Image (Vertical)', flipped_image_vertical)
# Wait until a key is pressed and then close the windows
[Link](0)
[Link]()

# Optionally, save the transformed images


[Link]('resized_image.jpg', resized_image)
[Link]('cropped_image.jpg', cropped_image)
[Link]('rotated_image.jpg', rotated_image)
[Link]('flipped_image_horizontal.jpg', flipped_image_horizontal)
[Link]('flipped_image_vertical.jpg', flipped_image_vertical)
- The [Link]() function changes the dimensions of the image to the specified size.

cropped_image = image[50:250, 100:300] # Adjust these values as needed

- This extracts a rectangular region from the original image using slicing.

- The image is rotated by the specified angle around its center using the rotation matrix.

- The [Link]() function flips the image horizontally or vertically based on the specified

parameter.
Effecting Image Thresholding
“Effecting image thresholding” refers to the process of applying a threshold to an image to
separate the given image into different regions based on pixel intensity values.
This technique is commonly used in image processing to create binary images from grayscale
images.
Thresholding Operation:- Each pixel in gray scale image(for example) has a value representing its
intensity, typically ranging from 0 (black) to 255 (white). A specific intensity value is chosen as the
cutoff. Pixels with intensity values above the threshold are set to one value (usually white), and
those below are set to another (usually black).
This method is useful for tasks like object detection, segmentation, and simplifying images for
further analysis.
Different types of thresholding methods (e.g., global, adaptive) can be applied depending on the
image and the desired outcome.
*Segmentation*: By converting a grayscale image into a binary image, thresholding helps to
segment the image into distinct regions. This makes it easier to isolate objects of interest
from the background.

*Simplification*: Thresholding reduces the complexity of the image by eliminating variations


in color and intensity, which can simplify subsequent analysis. This allows algorithms to focus
on the shape and position of objects rather than the details.

*Noise Reduction*: Applying a threshold can help eliminate noise and minor variations
within the image, making object boundaries clearer and more defined.
*Feature Extraction*: After thresholding, it becomes easier to extract features like
contours, edges, and shapes, which are essential for identifying and classifying
objects.

*Real-Time Processing*: Thresholding is computationally efficient, allowing for real-


time processing in applications such as video surveillance, robotics, and autonomous
vehicles.

*Adaptive Methods*: Techniques like adaptive thresholding can adjust the threshold
dynamically based on local image characteristics, making them effective in varying
lighting conditions or complex scenes.
Classification of Thresholding Methods:
Thresholding methods can be classified into several categories based on their
approach and application. Here are the main types:
1. *Global Thresholding*In global thresholding, a single intensity threshold value is
applied to the entire image.
- *Method*: A fixed value (e.g., determined through experimentation or statistical
analysis) is chosen, and all pixel values above this threshold are set to one value
(usually white), while those below are set to another (usually black).
- *Use Case*: Effective for images with uniform lighting and contrast.
*Example*: Otsu's method, which calculates an optimal threshold to minimize intra-
class variance.
2. *Local (Adaptive) Thresholding*Local thresholding, or adaptive thresholding,
calculates the threshold for smaller regions of the image, allowing for different
thresholds in different areas.
- *Method*: The threshold is determined based on the local neighborhood of each
pixel. This is particularly useful for images with varying illumination.
- *Use Case*: Effective for images where lighting conditions change across the
image.
*Example*: Mean or Gaussian adaptive thresholding, where the threshold is
computed as the mean or weighted mean of pixel values in the surrounding region.
3. *Binarization Methods*These methods are specifically designed for converting
images into binary format (black and white).
- *Method*: Similar to global thresholding but often incorporates techniques to
handle noise or specific object characteristics.
- *Use Case*: Commonly used in document image analysis (e.g., OCR) and medical
imaging.
*Example*: Sauvola's method, which adjusts the threshold based on local standard
deviation.
4. *Multi-level Thresholding* In multi-level thresholding, multiple thresholds are

used to segment the image into several regions instead of just two.

- *Method*: More than one threshold value divides the image into multiple classes

or segments.

- *Use Case*: Useful for images with multiple objects or varying intensity levels.

*Example*: Using k-means clustering to find multiple thresholds based on pixel

intensity distributions.
5. *Fuzzy Thresholding* Fuzzy thresholding incorporates fuzzy logic principles to
determine thresholds based on the degree of membership of pixel values in various
classes.
- *Method*: Instead of a hard threshold, it assigns a degree of belonging to each
class based on fuzzy rules and membership functions.
- *Use Case*: Beneficial for images with overlapping intensity distributions and
ambiguous boundaries.
6. *Dynamic Thresholding* Dynamic thresholding adjusts the threshold in real-time
based on specific criteria or feedback.
- *Method*: The threshold can change during processing based on user interaction
or algorithmic feedback.
- *Use Case*: Useful in video processing where lighting conditions vary rapidly.
# Different Thresholding Techniques
import cv2
import numpy as np
import [Link] as plt

# Load the image in grayscale


image = [Link](r"C:\Users\saimo\Pictures\standard_test_images\[Link]",
cv2.IMREAD_GRAYSCALE)

# Check if the image was loaded correctly


if image is None:
print("Error loading image")
exit()

# Global Thresholding
_, global_thresh = [Link](image, 127, 255, cv2.THRESH_BINARY)
# Otsu's Thresholding
otsu_thresh, otsu_result = [Link](image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# Adaptive Thresholding
adaptive_thresh_mean = [Link](image, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
11, 2)

adaptive_thresh_gaussian = [Link](image, 255,


cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,
11, 2)
# Plotting the results
[Link](figsize=(12, 10))

[Link](2, 3, 1)
[Link]('Original Image')
[Link](image, cmap='gray')
[Link]('off')

[Link](2, 3, 2)
[Link]('Global Thresholding')
[Link](global_thresh, cmap='gray')
[Link]('off’)

[Link](2, 3, 3)
[Link]("Otsu's Thresholding")
[Link](otsu_result, cmap='gray')
[Link]('off')
[Link](2, 3, 4)
[Link]('Adaptive Thresholding (Mean)')
[Link](adaptive_thresh_mean, cmap='gray')
[Link]('off')

[Link](2, 3, 5)
[Link]('Adaptive Thresholding (Gaussian)')
[Link](adaptive_thresh_gaussian, cmap='gray')
[Link]('off')

plt.tight_layout()
[Link]()
Explanation:
- *Loading the Image*: The code starts by loading a grayscale image using OpenCV.
- *Global Thresholding*: A fixed threshold of 127 is applied to create a binary image.
- *Otsu's Thresholding*: Automatically calculates an optimal threshold using Otsu’s
method.
- *Adaptive Thresholding*: Two types of adaptive thresholding are demonstrated:
- *Mean*: Uses the mean of the neighborhood area.
- *Gaussian*: Uses a weighted sum of the neighborhood area, giving more weight
to closer pixels.
- *Plotting Results*: Finally, the original and thresholded images are displayed using
Matplotlib.
The statement
otsu_thresh, otsu_result = [Link](image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
is used in OpenCV to apply Otsu's thresholding method to a grayscale image. Here's a breakdown
of each component of the statement:

### Components Explained

1. *[Link]*: This is a function from the OpenCV library used to apply a thresholding
operation to an image.

2. *image*: This is the input image to which the thresholding operation is applied. It should be a
grayscale image, where each pixel has a value representing its intensity (typically from 0 to 255).

3. *0*: This parameter is intended to be the threshold value. However, when using Otsu's method,
this value is ignored because Otsu's algorithm automatically calculates the optimal threshold value
based on the image histogram.
4. *255*: This is the maximum value that will be assigned to pixels exceeding the threshold. In
binary images, pixel values greater than the threshold will be set to this value (white).

5. *cv2.THRESH_BINARY*: This specifies the type of thresholding to be applied. THRESH_BINARY


means that all pixel values above the threshold will be set to the maximum value (255), and all
values below will be set to 0 (black).

6. *cv2.THRESH_OTSU*: This flag tells the [Link] function to use Otsu's method to
automatically determine the optimal threshold value from the image histogram. Otsu's method
maximizes the variance between the two classes of pixels (foreground and background), making
it effective for images with bimodal intensity distributions.
Return Values
- *otsu_thresh*: This variable will hold the calculated threshold value determined by Otsu's
method. Although you can ignore it in further processing, it can be useful for analysis or
debugging.
- *otsu_result*: This variable contains the resulting binary image after applying the thresholding
operation. Pixels in this image will either be set to 255 (white) or 0 (black).
Summary
In summary, this statement applies Otsu's thresholding to the input grayscale image,
automatically determining the best threshold to separate the foreground from the background.
The result is a binary image where the pixels are classified based on the optimal threshold,
making it a popular choice for image segmentation tasks.
The statement adaptive_thresh_mean = [Link](image, 255,
cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) applies adaptive thresholding to
a grayscale image using OpenCV. Here’s a breakdown of each component:

### Components Explained

1. *[Link]*: This is a function from the OpenCV library used to apply adaptive
thresholding to an image. Unlike global thresholding, adaptive thresholding calculates the
threshold value for smaller regions of the image, allowing for more flexibility in areas with
varying lighting conditions.

2. *image*: This is the input grayscale image that you want to threshold. It should be a single-
channel image where pixel values range from 0 to 255.

3. *255*: This parameter specifies the maximum value to be assigned to pixels that exceed the
calculated threshold for their respective neighborhoods. In a binary image, this value typically
represents white.
4. *cv2.ADAPTIVE_THRESH_MEAN_C*: This flag indicates the adaptive thresholding method to be
used. ADAPTIVE_THRESH_MEAN_C computes the threshold value for a pixel as the mean of the
pixel values in a neighborhood around it, minus a constant (which is specified next). This is
effective for images with varying illumination.

5. *cv2.THRESH_BINARY*: This specifies the type of thresholding to apply. THRESH_BINARY means


that pixel values greater than the calculated threshold will be set to 255 (white), while those
below will be set to 0 (black).

6. *11*: This parameter defines the size of the neighborhood (block size) used to calculate the
mean for adaptive thresholding. It must be an odd number (e.g., 11) because the neighborhood is
centered around each pixel.

7. *2*: This constant is subtracted from the mean calculated in the neighborhood. It helps to fine-
tune the thresholding process. The value can be adjusted based on the particular characteristics
of the image to enhance the results.
The statement adaptive_thresh_gaussian = [Link](image, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) applies adaptive thresholding to
a grayscale image using the Gaussian method in OpenCV. Here’s a detailed breakdown of each
component:
### Components Explained

1. *[Link]*: This function from the OpenCV library is used for adaptive
thresholding, which calculates the threshold for smaller regions of the image rather than using a
single global threshold. This is particularly useful in images with varying lighting conditions.

2. *image*: This is the input grayscale image that you want to process. It should be a single-
channel image, where pixel values range from 0 (black) to 255 (white).

3. *255*: This parameter specifies the maximum value that will be assigned to pixels that exceed
the calculated threshold in their respective neighborhoods. In binary images, this value typically
represents white.
4. *cv2.ADAPTIVE_THRESH_GAUSSIAN_C*: This flag indicates the adaptive thresholding method to
use. ADAPTIVE_THRESH_GAUSSIAN_C calculates the threshold for each pixel based on the
weighted sum of the neighborhood pixel values, where weights are assigned according to a
Gaussian window. This method tends to give more influence to pixels closer to the center of the
neighborhood.

5. *cv2.THRESH_BINARY*: This specifies the type of thresholding to apply. With THRESH_BINARY,


pixel values that are greater than the calculated threshold will be set to 255 (white), while those
below will be set to 0 (black).

6. *11*: This parameter defines the size of the neighborhood (block size) used to calculate the
Gaussian-weighted mean for adaptive thresholding. It must be an odd number (e.g., 11), as the
neighborhood is centered around each pixel.

7. *2*: This constant is subtracted from the calculated Gaussian-weighted mean. It acts as a
tuning parameter to adjust the thresholding result. By modifying this value, you can enhance or
reduce the sensitivity of the thresholding based on the characteristics of the image.
Calculating Gradients
Gradient: -
The ‘steepness of intensity values’ in an image refers to the rate of change of pixel intensity
values across the image.
In the context of image processing and computer vision, a gradient represents the the
steepness of intensity values in an image. That is, a gradient represents the rate of change of
pixel intensity values across the image.
Gradients are fundamental in understanding how the pixel values change in the spatial
domain, and they are crucial for various tasks, such as edge detection, image segmentation,
and feature extraction.
Let us consider the digital image with z represents the pixel intensity
values as shown below:
*Magnitude of the Gradient*:
- The magnitude of the gradient is computed using the formula:

Magnitude of the gradient = 𝐺𝑥 2 + 𝐺𝑦 2

Where Gx and Gy are the gradients in the x (horizontal) and y (vertical) directions,
respectively.
- This magnitude provides a single value representing how steep the change in intensity is at
a given pixel location.

4. *Direction of the Gradient*:


- The direction of the gradient of an image indicates where the steepest increase in intensity
occurs. It can be calculated using:
\[
\text{Direction} = \tan^{-1}\left(\frac{G_y}{G_x}\right)
\]
### Implications of Steepness

1. *Edge Detection*:
- Areas where the steepness of intensity values is high (i.e., where the gradient magnitude is
large) correspond to edges in the image. These edges are crucial for identifying shapes,
objects, and boundaries.

2. *Texture Analysis*:
- The steepness of intensity changes can also indicate texture. For instance, a surface with
abrupt changes in intensity might appear rough, while a smooth surface would have gentle
gradients.

3. *Image Segmentation*:
- Steepness helps in segmenting an image into different regions based on intensity
variations, allowing for better identification of objects and features.
### Example

To illustrate the steepness of intensity values, consider a simple scenario:

- *Smooth Transition*: In an area of an image where the intensity values change


gradually (e.g., a gradient from black to white), the steepness is low. The gradient
values will be small.

- *Sharp Edge*: In an area where there is a sudden change in intensity (e.g., from
black to white), the steepness is high, resulting in larger gradient values.
### Visual Representation
Here’s a simple example of how steepness can be visualized:
- *Grayscale Image*:
- A smooth gradient from dark gray (intensity value 50) to light gray (intensity value 200)
will have a low gradient.
- An area with a sharp transition from dark gray (50) to white (255) will produce a high
gradient, representing a steep change.
### Conclusion
The Gradients (steepness of intensity values)in an image is a fundamental concept that
helps in understanding how brightness and color vary across the image. By analyzing
gradients, one can detect edges, identify regions, and extract meaningful features from
images, which are critical tasks in image processing and computer vision.
#### Calculating Gradients Using Convolution

import cv2
import numpy as np

# Load the image (convert to grayscale)


image_path = r"C:\Users\saimo\Pictures\standard_test_images\lena_color_256.tif"
image = [Link](image_path, cv2.IMREAD_GRAYSCALE)

if image is None:
print("Error: Unable to load image.")
else:
# Calculate gradients using the Sobel operator
sobel_x = [Link](image, cv2.CV_64F, 1, 0, ksize=3) # Gradient in x direction
sobel_y = [Link](image, cv2.CV_64F, 0, 1, ksize=3) # Gradient in y direction

# Calculate the magnitude and direction of the gradient


gradient_magnitude = [Link](sobel_x, sobel_y)
gradient_direction = [Link](sobel_x, sobel_y, angleInDegrees=True)
# Display the results
[Link]('Original Image', image)
[Link]('Gradient Magnitude', gradient_magnitude / [Link](gradient_magnitude)) #
# Normalize for display
[Link]('Gradient Direction', gradient_direction)

# Wait until a key is pressed and then close the windows


[Link](0)
[Link]()

# Optionally, save the output images


[Link](r"C:\Users\saimo\Pictures\standard_test_images\gradient_magnitude.tif",
gradient_magnitude)
[Link](r"C:\Users\saimo\Pictures\standard_test_images\gradient_direction.tif",
gradient_direction)
- The [Link]() function is used to compute the gradients in the x and y directions. The
parameters specify the source image, the depth of the output image (e.g., cv2.CV_64F),
and the order of the derivative (1 for x, 0 for y, and vice versa).

- The magnitude of the gradient is calculated using [Link](), which computes the
Euclidean distance of the gradient vector:
magnitude = sqrt{(Gx^2 + Gy^2)}

- The direction of the gradient is calculated using [Link](), which provides the angle of
the gradient vector in degrees.
### Applications of Gradients

- Edge Detection: Gradients are commonly used in edge detection algorithms (e.g., Canny
edge detector) to identify areas of significant intensity change.

- Image Segmentation: Gradients help in segmenting images based on intensity differences,


which is crucial in object detection and recognition tasks.

- Feature Extraction: Gradients provide important features that can be used in various
computer vision applications, such as texture analysis and shape recognition.
Performing Histogram Equalization
Histogram equalization is a technique in image processing used to improve the contrast of an
image.
It works by redistributing the intensity values of the image to cover the full range of possible
values, resulting in a more balanced histogram.
This process can enhance the visibility of features in an image that may be obscured(out of sight)
due to poor contrast.

Contrast refers to the difference in luminance or color that makes an object distinguishable from
other objects and the background in an image. It is a critical aspect of image quality that affects
how well details can be perceived. In simpler terms, contrast is the degree to which the lightest
and darkest parts of an image differ from each other.
Histogram: - A histogram is a graphical representation of the distribution of pixel intensity values
in an image. It shows how many pixels fall within each intensity level (from 0 to 255 for an 8-bit
grayscale image). - For example, a histogram may indicate that most pixels in an image are dark
(low intensity values), which can lead to a loss of detail in darker regions.
Purpose of Histogram Equalization: - The primary goal is to enhance the contrast of an image by
spreading out the most frequent intensity values. This makes the image appear more defined and
improves the visibility of details.
Cumulative Distribution Function (CDF): - Histogram equalization uses the cumulative distribution
function of the histogram to transform the pixel values. The CDF represents the cumulative sum of
the probabilities of pixel intensities.
How Histogram Equalization Works
The process of histogram equalization can be summarized in the following steps:

1. Calculate the Histogram:


- Compute the histogram of the input image to determine the frequency of each intensity
value.
2. Compute the CDF:
- Calculate the cumulative distribution function (CDF) from the histogram. The CDF at a
given intensity level represents the total number of pixels with that intensity or lower.
3. Normalize the CDF:
- Normalize the CDF to scale it to the range of intensity values (0 to 255 for an 8-bit image):
CDF(i)_normalized = {CDF(i) - CDFmin}/{(N - CDFmin)(L - 1)}
Where:
- CDF(i) is the cumulative distribution function value at intensity (i).
- CDFmin is the minimum non-zero value of the CDF.
- N is the total number of pixels in the image.
- L is the number of possible intensity levels (256 for an 8-bit image).
4. Map the Intensity Values: - Use the normalized CDF to map the old intensity
values to new intensity values. Each pixel's intensity is replaced with the
corresponding value from the normalized CDF.
5. Create the Equalized Image: - The resulting image will have a histogram that is
more uniformly distributed across the available intensity values, enhancing the
overall contrast.
Example of Histogram Equalization
1. Input Image:
- Consider an image with a histogram heavily weighted towards lower intensity
values (darker image).
2. Histogram Before Equalization:
- The histogram may show a peak at low intensity values (e.g., 0-100).
3. After Equalization:
- The histogram becomes more spread out, with values now distributed more
evenly across the entire range (0-255). The resulting image appears brighter and
with improved contrast.
Applications
Histogram equalization is widely used in various applications:
- Medical Imaging: Enhancing visibility in X-rays, MRIs, and CT scans to better identify anomalies.
- Satellite Imaging: Improving contrast in satellite images for better analysis of land use,
vegetation, and urban areas.
- Photography: Adjusting the contrast of photographs to bring out details that may be lost due to
poor lighting conditions.
Limitations
- Over Enhancement: In some cases, histogram equalization can lead to over-enhancement,
creating unnatural-looking images with excessive noise or artifacts.
- Loss of Detail: In images with a lot of fine detail, equalization may cause some details to be lost
or merged into broader intensity ranges.
import cv2
import numpy as np
from matplotlib import pyplot as plt

# Load the image in grayscale


image_path = r"C:\Users\saimo\Pictures\standard_test_images\[Link]"
image = [Link](image_path, cv2.IMREAD_GRAYSCALE)

# Perform histogram equalization


equalized_image = [Link](image)

# Display the original and equalized images along with their histograms
[Link]('Original Image', image)
[Link]('Equalized Image', equalized_image)
# Calculate histograms
hist_original = [Link]([image], [0], None, [256], [0, 256])
hist_equalized = [Link]([equalized_image], [0], None, [256], [0, 256])

# Plot histograms
[Link](figsize=(12, 6))
[Link](1, 2, 1)
[Link]('Original Histogram')
[Link](hist_original)
[Link]([0, 256])
[Link](1, 2, 2)
[Link]('Equalized Histogram')
[Link](hist_equalized)
[Link]([0, 256])
[Link]()

# Wait until a key is pressed and then close the windows


[Link](0)
[Link]()
The statement
hist_original = [Link]([image], [0], None, [256], [0, 256])
is used to calculate the histogram of an image using OpenCV in Python. Let's break down each
part of the statement:
Breakdown of the Statement
1. [Link]:
- This is a function from the OpenCV library used to compute the histogram of an image. A
histogram provides a graphical representation of the distribution of pixel intensity values.
2. Parameters of [Link]:
The calcHist function takes several parameters :
- [image]:
- This is the source image for which the histogram is being calculated. The image
should be provided as a list (hence the square brackets), which allows for processing
multiple images at once. In this case, we are calculating the histogram for a single
image.
- The image can be in grayscale or color format, but in this particular example, we
typically use a grayscale image for simplicity.
- [0]:
- This argument specifies the channels for which we want to compute the histogram. In the
case of a grayscale image, there is only one channel (index 0).
- For a color image (like RGB), you can specify [0] for the red channel, [1] for the green channel,
or [2] for the blue channel.

- None:
- This parameter is used to specify a mask. If you want to calculate the histogram for a specific
region of the image, you would provide a mask that defines that region. By passing None, we are
indicating that we want to include the entire image without any masking.
- [256]:
- This parameter specifies the number of bins to use for the histogram. In this case, 256 bins are
used, which is standard for an 8-bit grayscale image where intensity values range from 0 to 255.
Each bin will represent the count of pixels that fall within specific ranges of intensity values.

- [0, 256]:
- This argument defines the range of intensity values. Here, it specifies that we want to consider
the range from 0 to 256.
- Although pixel values in an 8-bit image range from 0 to 255, the upper limit of 256 ensures
that all pixel values are included in the histogram calculation. The last bin typically captures the
count of pixels with an intensity of 255.

You might also like