0% found this document useful (0 votes)
3 views

Class Notes

ip project
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Class Notes

ip project
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

IP - Program - 11

Write a program to contour an image.


# Import Necessary Modules
import cv2
import numpy as np
import matplotlib.pyplot as plt

# Read the image


image = cv2.imread("1.jpg") #Replace Image Path

# Convert the image to grayscale (contours work best on binary images)


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

# Print Original Image


img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.title("Original Image")
plt.axis("off")
plt.show()

# Apply thresholding
_, binary_image = cv2.threshold(gray, 127, 255,
cv2.THRESH_BINARY)

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

# Draw all contours on the original image


cv2.drawContours(image, contours, -1, (0, 255, 0), 3);
# Display the result using Matplotlib
plt.figure(figsize=(10, 10))
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title('Contours')
plt.axis('off')
plt.show()

You might also like