1

我在使用convexityDevects. 我得到错误:AttributeError: 'module' object has no attribute convexityDefects

你有效地使用了这个命令吗?

#! /usr/bin/env python

import cv2
import numpy as np

img = cv2.imread('star.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 127, 255,0)
contours,hierarchy = cv2.findContours(thresh,2,1)
cnt = contours[0]

hull = cv2.convexHull(cnt,returnPoints = False)
defects = cv2.convexityDefects(cnt,hull)
cv2.imshow('img',img)
cv2.waitKey(0)

cv2.destroyAllWindows()
4

1 回答 1

1

Ubuntu 12.04 附带过时的 OpenCV 2.3.1-7 ( http://packages.ubuntu.com/precise/python-opencv ),并且cv2.convexityDefects从 OpenCV 2.4 开始可用。

你可以cv.ConvexityDefects改用。来自官方文档

cv.ConvexityDefects(contour,convexhull, storage)→convexityDefects 参数:

   contour – Input contour.
   convexhull – Convex hull obtained using ConvexHull2() that should contain
                pointers or indices to the contour points, not the hull points
                themselves (the returnPoints parameter in ConvexHull2() should
                be zero).
   storage – Container for the output sequence of convexity defects.
             If it is NULL, the contour or hull (in that order)
             storage is used.

该函数查找输入轮廓的所有凸面缺陷并返回 CvConvexityDefect 结构的序列,其中 CvConvexityDetect 定义为:

于 2013-10-20T00:58:38.633 回答