如何解决关于ZeroDivisionError的问题:float division by zero?

huangapple go评论87阅读模式
英文:

How to solve this problem about ZeroDivisionError: float division by zero?

问题

compactness_list = []
circularity_list = []

for contour in contours:
    perimeter = cv2.arcLength(contour, True)
    area = cv2.contourArea(contour)

    compactness = (perimeter ** 2) / (area)
    compactness_list.append(compactness)

    circularity = (4 * np.pi * area) / (perimeter)
    circularity_list.append(circularity)
英文:
compactness_list = []
circularity_list = []

for contour in contours:
    perimeter =  cv2.arcLength(contour, True)
    area = cv2.contourArea(contour)

    compactness = (perimeter ** 2) /(area)
    compactness_list.append(compactness)

    circularity = (4 * np.pi * area) / (perimeter)
    circularity_list.append(circularity)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-165-aa66a2f29568> in <cell line: 4>()
      6     area = cv2.contourArea(contour)
      7 
----> 8     compactness = (perimeter ** 2) / (area)
      9     compactness_list.append(compactness)
     10 

ZeroDivisionError: float division by zero

答案1

得分: 1

你可以添加一个try...except块:

for contour in contours:
    perimeter = cv2.arcLength(contour, True)
    area = cv2.contourArea(contour)

    try:
        compactness = (perimeter ** 2) / area
        circularity = (4 * np.pi * area) / perimeter
    except ZeroDivisionError:
        compactness = 0  # 或者任何其他值
        circularity = 0  # 或者任何其他值

    compactness_list.append(compactness)
    circularity_list.append(circularity)

或者一个简单的if...else

for contour in contours:
    perimeter = cv2.arcLength(contour, True)
    area = cv2.contourArea(contour)

    if area == 0:  # 检查面积是否为零
        compactness = 0  # 或者任何其他值
        circularity = 0  # 或者任何其他值
    else:
        compactness = (perimeter ** 2) / area
        circularity = (4 * np.pi * area) / perimeter

    compactness_list.append(compactness)
    circularity_list.append(circularity)
英文:

You can add a try...except block:

for contour in contours:
    perimeter =  cv2.arcLength(contour, True)
    area = cv2.contourArea(contour)

    try:
        compactness = (perimeter ** 2) / area
        circularity = (4 * np.pi * area) / perimeter
    except ZeroDivisionError:
        compactness = 0  # or any other value 
        circularity = 0  # or any other value 

    compactness_list.append(compactness)
    circularity_list.append(circularity)

Or a simple if...else:

for contour in contours:
    perimeter =  cv2.arcLength(contour, True)
    area = cv2.contourArea(contour)

    if area == 0:  # check if area is zero
        compactness = 0  # or any other value
        circularity = 0  # or any other value
    else:
        compactness = (perimeter ** 2) / area
        circularity = (4 * np.pi * area) / perimeter

    compactness_list.append(compactness)
    circularity_list.append(circularity)

答案2

得分: 1

在这种情况下,您可以将 epsilon 添加到分母

    import sys
    compactness_list = []
    circularity_list = []
    
    epsilon = sys.float_info.epsilon
    for contour in contours:
        perimeter =  cv2.arcLength(contour, True)
        area = cv2.contourArea(contour)
    
        compactness = (perimeter ** 2) /(area+epsilon)
        compactness_list.append(compactness)
    
        circularity = (4 * np.pi * area) / (perimeter+epsilon)
        circularity_list.append(circularity)
英文:

in Such case you Can add epsilon to the denominator

import sys
compactness_list = []
circularity_list = []

epsilon = sys.float_info.epsilon
for contour in contours:
    perimeter =  cv2.arcLength(contour, True)
    area = cv2.contourArea(contour)

    compactness = (perimeter ** 2) /(area+epsilon)
    compactness_list.append(compactness)

    circularity = (4 * np.pi * area) / (perimeter+epsilon)
    circularity_list.append(circularity)

huangapple
  • 本文由 发表于 2023年6月6日 02:11:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定