0% found this document useful (0 votes)
17 views13 pages

Lab Program 11

sss

Uploaded by

Vinutha H M
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)
17 views13 pages

Lab Program 11

sss

Uploaded by

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

Write a program to contour an image

import cv2

image = cv2.imread('images.jpg', cv2.IMREAD_GRAYSCALE)


ret, thresh = cv2.threshold(image, 127, 255,
cv2.THRESH_BINARY_INV)

contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL,


cv2.CHAIN_APPROX_SIMPLE)
contour_image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
cv2.drawContours(contour_image, contours, -1, (0, 255, 0), 2)
cv2.imshow('Original Image', image)

cv2.imshow('Contoured Image', contour_image)


cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
• Import the cv2 module.
image = cv2.imread('images.jpg', cv2.IMREAD_GRAYSCALE)
'images.jpg':

• This is the file path of the image you want to read.


cv2.IMREAD_GRAYSCALE:
• This is a flag used with cv2.imread() to specify how the image
should be read.
• cv2.IMREAD_GRAYSCALE indicates that the image should be
read as a grayscale image.
• ret, thresh = cv2.threshold(image, 127, 255,
cv2.THRESH_BINARY_INV)
Thresholding is a popular image processing technique used to
convert a grayscale image into a binary image, where the
pixels are either black or white.
• 4 arguments are
1. Source image: It should be grey scale image
2. Threshold Value: The threshold value that is used to divide
the pixels in the image into different classes.
3. maxval : The maximum value assigned to pixels that are
above the threshold.
4. type: The type of thresholding to be applied.
• contours, _ = cv2.findContours(thresh,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
• findContours() function is used to detect the
contours in the image.

• It has 3 arguments.

1. image: Binary input image

2. mode: Contour-retrieval mode


3. method: Contour-approximation method
• contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
Return values of findContours()
1. contours : Holds the list of contours found in thresh image

2. _ : The second return value is typically ignored (hence


assigned to _), as it represents the hierarchy of contours
(parent-child relationship), which is not necessary when
only external contours are needed (cv2.RETR_EXTERNAL).
• contour_image = cv2.cvtColor(image,
cv2.COLOR_GRAY2BGR)
Above function converts a gray scale image to color
image.

cvtColor function in OpenCV is used to convert an


image from one color space to another.
• cv2.drawContours(contour_image, contours, -1, (0, 255, 0), 2)

• drawContours() function is used to overlay the contours on


the RGB image
1. image: This is the input RGB image on which you want to
draw the contour.

2. contours: Indicates the contours obtained from


the findContours() function.
3. contourIdx: Using this argument, you can specify the index
position from this list, indicating exactly which contour point
you want to draw. Providing a negative value will draw all the
contour points.
4. color: This indicates the color of the contour points
you want to draw.
5. thickness: This is the thickness of contour points.
cv2.imshow('Original Image', image)
cv2.imshow('Contoured Image', contour_image)
• cv2.imshow() method is used to display an image in a
window.
waitKey()

• waitKey() function of Python OpenCV allows users to


display a window for given milliseconds or until any
key is pressed.

• If 0 is passed in the argument it waits till any key is


pressed.

cv2.waitKey(0)
cv2.destroyAllWindows()
• The function cv2.destroyAllWindows() is used in
OpenCV (cv2) to close all the OpenCV windows that
were created with cv2.imshow().
Output

You might also like