0% found this document useful (0 votes)
14 views4 pages

Program 11: Write A Program To Contour An Image

This document provides a program using OpenCV to load an image, convert it to grayscale, apply binary thresholding, find contours, and display the original and contour images. It explains the significance of contours in image processing, including their role in object detection and shape analysis. Additionally, it details the parameters and functions used in the program, particularly focusing on Otsu's binarization method for optimal thresholding.
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)
14 views4 pages

Program 11: Write A Program To Contour An Image

This document provides a program using OpenCV to load an image, convert it to grayscale, apply binary thresholding, find contours, and display the original and contour images. It explains the significance of contours in image processing, including their role in object detection and shape analysis. Additionally, it details the parameters and functions used in the program, particularly focusing on Otsu's binarization method for optimal thresholding.
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/ 4

Program 11: Write a program to contour an image.

import cv2
import numpy as np

# Load the image


image =
cv2.imread("D:/DSATM/CG/CG&DIP/Lab_Images/Prog7_flower.jpg")

# Convert the image to grayscale


gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply binary thresholding


ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV +
cv2.THRESH_OTSU)

# Find contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)

# Create a copy of the original image to draw contours on


contour_image = image.copy()

# Draw contours on the image


cv2.drawContours(contour_image, contours, -1, (0, 255, 0), 2)

# Display the original and contour images


cv2.imshow('Original Image', image) cv2.imshow('Contours', contour_image)

# Wait for a key press to close the windows


cv2.waitKey(0)
cv2.destroyAllWindows()

Output:
Explanation:

In image processing and computer vision, a contour refers to a curve joining all the
continuous points (along a boundary) that have the same color or intensity. Contours are a
useful tool for shape analysis and object detection and recognition.

Key Points about Contours:


1. Contours are simply the boundaries: They outline the shapes of objects in an
image. Contours can be thought of as a curve that connects all the points along the
boundary of a shape with the same color or intensity.

2. Useful for Object Detection: By detecting contours, you can easily identify and
isolate different shapes in an image, making them useful for tasks such as object
detection and image segmentation.

3. Hierarchy: Contours can have a hierarchical relationship, especially if one contour


is inside another.

import cv2: This imports the OpenCV library, which is used for computer vision tasks.

cv2.imread("D:/DSATM/CG/CG&DIP/Lab_Images/Prog7_flower.jpg"): This function


reads the image from the specified path. The image is loaded in color mode by default.

Convert to Grayscale:

 The loaded color image is converted to a grayscale image because contour detection
is typically performed on single-channel images.

Apply Binary Thresholding:

 A binary threshold is applied to the grayscale image. cv2.THRESH_BINARY_INV


creates a binary image with inverted colors (white becomes black and black
becomes white).
 cv2.THRESH_OTSU automatically calculates the optimal threshold value.

Find Contours:

 Contours are detected in the binary image.


 cv2.RETR_EXTERNAL retrieves only the external contours.
 cv2.CHAIN_APPROX_SIMPLE compresses horizontal, vertical, and diagonal segments,
keeping only their end points.
Create a Copy of the Original Image:

 A copy of the original image is made to draw the contours on.

Draw Contours:

 Contours are drawn on the copied image.


 -1 indicates that all contours should be drawn.
 (0, 255, 0) specifies the color of the contours (green in BGR format).
 2 specifies the thickness of the contour lines.

Displaying the Images

 The original and the contour images are displayed in separate windows.

Waiting for a Key Press and Closing the Windows

 cv2.waitKey(0): This function waits indefinitely until a key is pressed. It keeps the
image windows open until a key event occurs.
 cv2.destroyAllWindows(): This function closes all the OpenCV windows opened by
the program.

Summary

This program effectively demonstrates how to load an image, preprocess it, find contours,
and display the results using OpenCV.

Additional information

The function call ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV +


cv2.THRESH_OTSU) applies a binary threshold to a grayscale image using Otsu's
binarization method.

Function and Parameters


 cv2.threshold: This function applies a fixed-level threshold to each pixel in the
grayscale image.

Parameters:
 gray: The source grayscale image.
 0: The threshold value. In this case, since we are using Otsu's method, this value is
ignored.
 255: The maximum value to use with the cv2.THRESH_BINARY_INV thresholding
type. Pixels with intensity greater than the threshold value will be set to this value
(255).
 cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU: Combined flags indicating the
type of thresholding to be applied.

 cv2.THRESH_BINARY_INV: This flag indicates that binary inverse thresholding


should be used. In binary inverse thresholding, pixel values greater than the
threshold value are set to 0, and pixel values less than or equal to the threshold
value are set to the maximum value (255).
 cv2.THRESH_OTSU: This flag indicates that Otsu's binarization method should be
used to automatically determine the optimal threshold value.

Otsu's Binarization
 Otsu's method is a global thresholding technique that automatically determines the
optimal threshold value by minimizing the intra-class variance (the variance within
the foreground and background pixel intensities). This method is particularly useful
for images with a bimodal histogram (two distinct peaks).

Return Values
 ret: The threshold value used. Since Otsu's method is applied, this value is
computed automatically and returned by the function.
 thresh: The resulting binary image after applying the thresholding. This is a binary
image where pixels are either 0 or 255, based on the thresholding conditions.

You might also like