我正在尝试从图像中提取一只手。我正在使用OpenCV 4.0和Python 3.6。
cnts = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# return None, if no contours detected
if len(cnts) == 0:
return
else:
# based on contour area, get the maximum contour which is the hand
segmented = max(cnts, key=cv2.contourArea)
这给了我这个错误:
Traceback (most recent call last):
File "test.py", line 85, in <module>
hand = segment(gray)
File "test.py", line 37, in segment
segmented = max(cnts, key=cv2.contourArea)
TypeError: Expected cv::UMat for argument 'contour'
自从半年前开始工作以来,我假设该错误是由于某种模块更改而发生的。怎么解决?
答案 0 :(得分:0)
您没有注意cv2.findContours
的输出。对于任何OpenCV版本> = 4.0,它都是
contours, hierarchy = cv2.findContours(...)
我做了一个例子:
import cv2
import numpy as np
# Set up dummy image
image = np.zeros((400, 400), np.uint8)
cv2.circle(image, (150, 250), 100, 255, cv2.FILLED)
# Find contours: OpenCV 4.x
cnts, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(cnts) == 0:
# return None, if no contours detected
segmented = None
else:
# based on contour area, get the maximum contour which is the hand
segmented = max(cnts, key=cv2.contourArea)
print('Number of contour points:', segmented.shape[0])
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
示例图片如下:
输出:
Number of contour points: 292
希望有帮助!