【问题标题】:when i tried to run below coding i got an error, so guys try to solve the error当我尝试在编码下运行时出现错误,所以大家尝试解决错误
【发布时间】: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


【解决方案1】:

让我提出一个可以帮助您解决问题的建议。

error: (-215:Assertion failed) total >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::isContourConvex'

这个错误意味着你应该检查实际的cv::isCounterConvex() 函数,看看为什么total 不大于0,并且深度参数不等于CV_32FCV_32S。然后你可能会解决你的问题。 这些断言是最好的错误类型,因为它实际上说明了问题所在,您唯一应该做的就是仔细阅读它并检查源代码以了解实际情况。

【讨论】:

  • 不是为什么是,而是为什么不是。断言,也就是那个表达式,failed
  • 谢谢你的建议,现在我可以理解给出的断言了,我终于明白了。
  • 很高兴听到这个消息! :)
猜你喜欢
  • 2019-01-26
  • 2023-02-04
  • 2022-01-15
  • 2020-02-11
  • 1970-01-01
  • 2023-04-08
  • 2021-12-24
  • 2022-06-14
  • 2012-05-26
相关资源
最近更新 更多