Ask Your Question

Revision history [back]

python cv2.drawContours function is not drawing contours. I don't have any error in the program. I need help.

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.

click to hide/show revision 2
None

updated 2019-07-12 01:48:31 -0600

berak gravatar image

python cv2.drawContours function is not drawing contours. I don't have any error in the program. I need help.

import numpy as np import cv2

image = cv2.imread('E:\DOWNLOADS\Computer Vision\misc\house.jpg')
cv2.imread('E:\\DOWNLOADS\\Computer Vision\\misc\\house.jpg')
orig_image = image.copy()
cv2.imshow('Original Image', orig_image)
cv2.waitKey(0)

cv2.waitKey(0) # Grayscale and Binarize

Binarize gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)

cv2.THRESH_BINARY_INV) # Find Contours

Contours contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

cv2.CHAIN_APPROX_NONE) # Iterate through each contour

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)

cv2.waitKey(0) # Iterate through each contour and compute the approx contour

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)

image) cv2.waitKey(0) cv2.destroyAllWindows()

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.