import numpy as np import cv2
image = cv2.imread('E:\DOWNLOADS\Computer Vision\misc\house.jpg') orig_image = image.copy() cv2.imshow('Original Image', orig_image) cv2.waitKey(0)
Grayscale and Binarize
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
Find Contours
contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
Iterate through each contour
for c in contours: x,y,w,h = cv2.boundingRect(c) cv2.rectangle(orig_image, (x, y), (x+w, y+h), (255, 0, 255), 2) cv2.imshow('Bounding Rectangle', orig_image) cv2.waitKey(0)
Iterate through each contour and compute the approx contour
for c in contours: accuracy = 0.03 * cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, accuracy, True) cv2.drawContours(image, [approx], 0, (255, 255, 0), 2) cv2.imshow('Approx Poly DP', image)
cv2.waitKey(0) cv2.destroyAllWindows()
This is my code. And when I run it, it shows only the original image. I am unable to see the contours though there is no error in the code.