【发布时间】:2021-12-21 00:13:35
【问题描述】:
filepath = 'blackmeasles (1).jpg'
BLUR = 21
CANNY_THRESH_1 = 10
CANNY_THRESH_2 = 200
MASK_DILATE_ITER = 10
MASK_ERODE_ITER = 10
MASK_COLOR = (0.0,0.0,0.0) # In BGR format
img = cv2.imread(filepath)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2)
edges = cv2.dilate(edges, None)
edges = cv2.erode(edges, None)
contour_info = []
_, contours = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
for c in contours:
contour_info.append((c, cv2.isContourConvex(c), cv2.contourArea(c),))
contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)
max_contour = contour_info[0]
mask = np.zeros(edges.shape)
cv2.fillConvexPoly(mask, max_contour[0], (255))
mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER)
mask = cv2.erode(mask, None, iterations=MASK_ERODE_ITER)
mask = cv2.GaussianBlur(mask, (BLUR, BLUR), 0)
mask_stack = np.dstack([mask]*3)
mask_stack = mask_stack.astype('float32') / 255.0
img = img.astype('float32') / 255.0
masked = (mask_stack * img) + ((1-mask_stack) * MASK_COLOR)
masked = (masked * 255).astype('uint8')
fileName, fileExtension = filepath.split('.')
fileName += '-masked.'
filepath = fileName + fileExtension
print(filepath)
cv2.imwrite(filepath, masked)
我遇到了以下错误,大佬们帮我解决一下,
图像分类领域 ---> 21 contour_info.append((c, cv2.isContourConvex(c), cv2.contourArea(c),)) 22 轮廓信息=排序(轮廓信息,键=lambda c:c[2],反向=真) 23
错误:OpenCV(4.5.4-dev) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\convhull.cpp:447: error: (-215:Assertion failed) 总计>= 0 && (depth == CV_32F || depth == CV_32S) 在函数'cv::isContourConvex'中
【问题讨论】:
-
调用
cv2.isContourConvex(c)是问题 - 轮廓是否闭合?他们必须满足哪些要求才能使isContourConvex能够处理它们? -
在想知道之前,只需打印并查看触发异常的轮廓
-
请澄清您的具体问题或提供更多详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
-
非常感谢大家(Grismar,Christoph Rackwitz),我调试成功了!
标签: python image opencv processing