llSourcell / Object_Detection_demo_LIVE

This is the code for the "How to do Object Detection with OpenCV" live session by Siraj Raval on Youtube
103 stars 80 forks source link

cv2.contourArea(cnt) #5

Open sudhasubramaniam opened 6 years ago

sudhasubramaniam commented 6 years ago

This is my code import cv2 import numpy as np img = cv2.imread('index.jpeg',0) ret,thresh = cv2.threshold(img,127,255,0) _,contours,hierarchy= cv2.findContours(thresh, 1, 2)

cnt = contour[0] M = cv2.moments(cnt) print(M) area = cv2.contourArea(cnt) perimeter = cv2.arcLength(cnt,True)

I'm getting this error. some one pls help me resolving it. error: /tmp/build/80754af9/opencv_1512491964794/work/modules/imgproc/src/shapedescr.cpp:319: error: (-215) npoints >= 0 && (depth == 5 || depth == 4) in function contourArea

pallavipundir commented 6 years ago

Same issue.Have you got any solution for this?

aijedi commented 6 years ago

Same Issue, someone please respond.

min_YCrCb = numpy.array([0,133,77],numpy.uint8) max_YCrCb = numpy.array([255,173,127],numpy.uint8) sourceImage = cv2.imread('abc.png') imageYCrCb = cv2.cvtColor(sourceImage,cv2.COLOR_BGR2YCR_CB) skinRegion = cv2.inRange(imageYCrCb,min_YCrCb,max_YCrCb) contours, hierarchy, _ = cv2.findContours(skinRegion, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for i, c in enumerate(contours): print (i) print (c) print (cv2.contourArea(c)) `

error: OpenCV(3.4.1) /io/opencv/modules/imgproc/src/shapedescr.cpp:272: error: (-215) npoints >= 0 && (depth == 5 || depth == 4) in function contourArea

xixiU commented 5 years ago

the error occurs in findContours you can use the following:

cnts = cv2.findContours(edged, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

cnts = cnts[0] if len(cnts) == 2 else cnts[1]

The detail shows in the link.