0% found this document useful (0 votes)
3 views7 pages

OpenCV Image Processing Techniques

Uploaded by

mo.khalid.2026
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)
3 views7 pages

OpenCV Image Processing Techniques

Uploaded by

mo.khalid.2026
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

Chapter 3: Image Processing and Analysis

In this chapter, we'll dive into image processing and analysis


techniques using OpenCV. We'll cover a range of topics, including
image thresholding, edge detection, image gradients, image
histograms, and contour detection.
3.1 Image Thresholding:
Image thresholding is a technique used to separate objects from
the background based on pixel intensity values. Let's explore
different thresholding methods using OpenCV:
import cv2

# Read a grayscale image


image = [Link]('[Link]', 0)

# Apply different thresholding methods


_, binary_threshold = [Link](image, 127, 255,
cv2.THRESH_BINARY)
_, binary_inverse_threshold = [Link](image, 127, 255,
cv2.THRESH_BINARY_INV)
_, adaptive_threshold = [Link](image, 255,
cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)

# Display the original and thresholded images


[Link]('Original Image', image)
[Link]('Binary Threshold', binary_threshold)
[Link]('Binary Inverse Threshold', binary_inverse_threshold)
[Link]('Adaptive Threshold', adaptive_threshold)
[Link](0)
[Link]()
In this example, we read a grayscale image and apply different
thresholding methods:
* cv2.THRESH_BINARY: Converts pixels above the threshold value
to the maximum value (255) and the rest to zero.
* cv2.THRESH_BINARY_INV: Converts pixels below the threshold
value to the maximum value (255) and the rest to zero.
* cv2.ADAPTIVE_THRESH_MEAN_C: Computes the threshold
value as the mean of the neighborhood area.
* [Link]: Applies adaptive thresholding based on
local pixel neighborhoods.
We display the original image and the thresholded images using
the imshow function.
3.2 Edge Detection:
Edge detection is crucial for identifying boundaries and contours
in images. Let's explore edge detection techniques using OpenCV:
import cv2

# Read a grayscale image


image = [Link]('[Link]', 0)
# Apply different edge detection methods
canny_edges = [Link](image, 100, 200)
sobel_edges_x = [Link](image, cv2.CV_64F, 1, 0, ksize=3)
sobel_edges_y = [Link](image, cv2.CV_64F, 0, 1, ksize=3)

# Display the original and edge-detected images


[Link]('Original Image', image)
[Link]('Canny Edges', canny_edges)
[Link]('Sobel Edges X', sobel_edges_x)
[Link]('Sobel Edges Y', sobel_edges_y)
[Link](0)
[Link]()
In this code snippet, we read a grayscale image and apply different
edge detection methods:
* [Link]: Detects edges using the Canny edge detection
algorithm.
* [Link]: Computes the gradient of the image using the Sobel
operator.
We display the original image and the edge-detected images using
the imshow function.
3.3 Image Gradients:
Image gradients provide information about the intensity change
across an image. Let's explore gradient computation techniques
using OpenCV:
import cv2

# Read a grayscale image


image = [Link]('[Link]', 0)

# Compute image gradients using different methods


sobel_x = [Link](image, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = [Link](image, cv2.CV_64F, 0, 1, ksize=3)
laplacian = [Link](image, cv2.CV_64F)

# Display the original image and the computed gradients


[Link]('Original Image', image)
[Link]('Sobel X', sobel_x)
[Link]('Sobel Y', sobel_y)
[Link]('Laplacian', laplacian)
[Link](0)
[Link]()
In this example, we read a grayscale image and compute the
image gradients using different methods:
* [Link]: Computes the gradient using the Sobel operator for
both x and y directions.
* [Link]: Computes the Laplacian of the image.
We display the original image and the computed gradients using
the imshow function.
3.4 Image Histograms:
Image histograms provide insights into the distribution of pixel
intensities in an image. Let's explore histogram computation and
equalization using OpenCV:
import cv2
import [Link] as plt

# Read a grayscale image


image = [Link]('[Link]', 0)

# Compute and plot the image histogram


histogram = [Link]([image], [0], None, [256], [0, 256])
[Link](histogram)
[Link]('Image Histogram')
[Link]('Pixel Value')
[Link]('Frequency')
[Link]()

# Perform histogram equalization


equalized_image = [Link](image)
# Display the original and equalized images
[Link]('Original Image', image)
[Link]('Equalized Image', equalized_image)
[Link](0)
[Link]()
In this code snippet, we read a grayscale image and compute its
histogram using the calcHist function. We plot the histogram
using Matplotlib. We then perform histogram equalization on the
image using the equalizeHist function. Finally, we display the
original image and the equalized image using the imshow function.
3.5 Contour Detection:
Contour detection is useful for identifying and analyzing the
boundaries of objects in an image. Let's explore contour detection
techniques using OpenCV:
import cv2

# Read a grayscale image


image = [Link]('[Link]', 0)

# Apply a threshold to obtain a binary image


_, binary_image = [Link](image, 127, 255,
cv2.THRESH_BINARY)

# Find contours in the binary image


contours, _ = cv2.findContours(binary_image,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Draw the contours on a blank image


contour_image = [Link](np.zeros_like(image),
contours, -1, (0, 255, 0), 2)

# Display the original image and the contour image


[Link]('Original Image', image)
[Link]('Contour Image', contour_image)
[Link](0)
[Link]()
In this example, we read a grayscale image and apply thresholding
to obtain a binary image. We then use the findContours function
to detect contours in the binary image. Finally, we draw the
contours on a blank image using the drawContours function and
display both the original image and the contour image using the
imshow function.
By the end of this chapter, you'll have a solid understanding of
image processing and analysis techniques in OpenCV. You'll be
able to perform image thresholding, edge detection, compute
image gradients, analyze image histograms, and detect contours
in images. These skills will be invaluable in various computer
vision applications.

You might also like