Ask Your Question
0

Unable to draw contours using this code. Please help

asked 2018-03-15 18:20:24 -0600

import cv2 import numpy as np import matplotlib.pyplot as plt

image = cv2.imread('C:\Users\suman\Desktop\shapes.jpg') cv2.imshow('Input image', image) cv2.waitKey(0)

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

edged= cv2.Canny(gray, 30,200) cv2.imshow('Canny Edges', edged) cv2.waitKey(0)

image, contours, heirarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) cv2.imshow('Canny edges after countouring', edged) cv2.waitKey(0)

print ("Number of Contours found =" + str(len(contours)))

cv2.drawContours(image, contours, -1, (0,255,0),3) cv2.imshow('Contours', image) cv2.waitKey(0) image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-03-16 03:17:03 -0600

berak gravatar image

the problem is here:

image, contours, heirarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

this will replace your 3 channels image with a 1 channel one, and you cannot draw something green into that (it'll be invisible)

instead, try:

_, contours, heirarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

now drawContours should work as expected ;)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-03-15 18:20:24 -0600

Seen: 642 times

Last updated: Mar 16 '18