Ask Your Question
0

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

asked 2019-07-12 01:22:32 -0500

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

berak gravatar image

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.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-07-12 06:18:33 -0500

supra56 gravatar image

Yes! You can see it. It is because you are using too many cv.waitkey(0). Meaning, you will see original image's window, then close it. You will see immediately Bounding Rectangle' s window. open Then close Bounding Rectangle' s window.. Then immediately Approx Poly DP's window open. If you want to see all windows on fly, you will have to rem it out 2 waitKey() and leave one at bottom. Code:

#!/usr/bin/env python3
#Raspberry pi 4b, Buster Ver10, OpenCV 4.1.0
#Date: 12th July, 2019

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()
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-07-12 01:22:32 -0500

Seen: 67 times

Last updated: Jul 12