英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论