0

How to avoid the None return from convexityDefects function?

If the function returns None I cannot use .shape for None objects (which showed an error). However, if I insert my if statement right before the for loop, there is another error from numpy saying that

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

Then if I put defects.all() for the if statement, the None object error shows again

cnt = contours[0]

hulll = cv.convexHull(cnt, returnPoints=False)

defects = cv.convexityDefects(cnt, hulll)
  if (defects != None):
     for i in range(defects.shape[0]):
        s, e, f, d = defects[i, 0]

How can I execute the for loop without any problems?

Sina
  • 270
  • 1
  • 23
Hau Ha
  • 1
  • usually convex hull is not the problem. problem is your imperfect segmentation result. should check on that . You can use morph dialiet to incrase the surface area to avoid such. but at cost of getting noise. There are other way to select good canadiate e.g use with deep learned detector. there are many ways. Show your specific error plz. – Dr Yuan Shenghai Jun 11 '19 at 04:15
  • Possible duplicate of [ValueError when checking if variable is None or numpy.array](https://stackoverflow.com/questions/36783921/valueerror-when-checking-if-variable-is-none-or-numpy-array) – Oliort UA Jun 11 '19 at 08:36
  • `if not defects is None`. Use `is` instead of `==`/`!=`. Read above comment. – Oliort UA Jun 11 '19 at 08:38
  • It was solved. Thank you! – Hau Ha Jun 11 '19 at 14:49

0 Answers0