cv2.凸面缺陷

2024-10-03 19:26:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用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()

Tags: importimgobject错误cv2attributeerrorhasmodule
1条回答
网友
1楼 · 发布于 2024-10-03 19:26:57

ubuntu12.04附带了过时的opencv2.3.1-7(http://packages.ubuntu.com/precise/python-opencv),而{}则是从opencv2.4开始提供的。在

您可以改为使用cv.ConvexityDefects。从official docs

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

   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.

The function finds all convexity defects of the input contour and returns a sequence of the CvConvexityDefect structures, where CvConvexityDetect is defined as:

相关问题 更多 >