import cv2
import numpy as np
img = cv2.imread(‘ex4.jpg’)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
kernel_size = 7
kernel = np.ones((3,3),np.uint8)
gray = cv2.dilate(gray, kernel, iterations=4)
gray = cv2.erode(gray, kernel, iterations=4)
blur = cv2.blur(gray, (5, 5))
blur_gray = cv2.GaussianBlur(blur, (kernel_size, kernel_size), 1)
edged = cv2.Canny(blur_gray, 30, 180)
contours, _ = cv2.findContours(edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = max(contours, key=lambda x: cv2.contourArea(x))
epsilon = 0.0005*cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
print(contours)
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
cv2.imshow(‘Contours’, img)
cv2.waitKey(0)
cv2.destroyAllWindows()