"ZeroDivisionError: float division by zero" for image processing code using Rapberry Pi3 and camera
"ZeroDivisionError: float division by zero" for image processing code using Rapberry Pi3 and camera
我已经编辑了这些代码,这些是代码:
from pyimagesearch.shapedetector import ShapeDetector
import argparse
import imutils
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",help="path to the (optional) video file")
ap.add_argument("-b", "--buffer",type=int, default=64,help="max buffer size")
args = vars(ap.parse_args())
if not args.get("video", False):
camera = cv2.VideoCapture(0)
else:
camera = v2.VideoCapture(args["video"])
while True:
(grabbed, frame) = camera.read()
if args.get("video") and not grabbed:
break
frame = imutils.rotate(frame, angle=180)
resized = imutils.resize(frame, width=300)
ratio = frame.shape[0] / float(resized.shape[0])
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
sd = ShapeDetector()
for c in cnts:
M = cv2.moments(c)
cX = int((M["m10"] / M["m00"]) * ratio)
cY = int((M["m01"] / M["m00"]) * ratio)
shape = sd.detect(c)
c = c.astype("float")
c *= ratio
c = c.astype("int")
cv2.drawContours(frame, [c], -1, (0, 255, 0), 2)
cv2.putText(frame, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (255, 255, 255), 2)
cv2.imshow("Gambar", frame)
key = cv2.waitKey(1) & 0xFF
# if the 'q' key is pressed, stop the loop
if key == ord("q"):
break
camera.release()
cv2.destroyAllWindows()
在我运行之后,我注意到
文件 "detect_shapes_using_video.py",第 61 行,在
cX = int((M["m10"] / M["m00"]) * 比率)
ZeroDivisionError: 浮点除以零
我该怎么办?因为我还是 python 的初学者,我不知道该怎么做。这些代码用于在 raspberry pi
中使用相机进行图像处理
这个街区
M = cv2.moments(c)
cX = int((M["m10"] / M["m00"]) * ratio)
cY = int((M["m01"] / M["m00"]) * ratio)
就是求图像中找到的轮廓的质心。 ZeroDivisionError: float division by zero
发生在 M["m00"] = 0
时,因为轮廓只是一条线。
您可以添加条件 if M["m00"] == 0: shape = "line"
来避免此错误。
[编辑]这是我的建议:
from pyimagesearch.shapedetector import ShapeDetector
import argparse
import imutils
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",help="path to the (optional) video file")
ap.add_argument("-b", "--buffer",type=int, default=64,help="max buffer size")
args = vars(ap.parse_args())
if not args.get("video", False):
camera = cv2.VideoCapture(0)
else:
camera = v2.VideoCapture(args["video"])
while True:
(grabbed, frame) = camera.read()
if args.get("video") and not grabbed:
break
frame = imutils.rotate(frame, angle=180)
resized = imutils.resize(frame, width=300)
ratio = frame.shape[0] / float(resized.shape[0])
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
sd = ShapeDetector()
for c in cnts:
M = cv2.moments(c)
if(M["m00"]==0): # this is a line
shape = "line"
else:
cX = int((M["m10"] / M["m00"]) * ratio)
cY = int((M["m01"] / M["m00"]) * ratio)
shape = sd.detect(c)
c = c.astype("float")
c *= ratio
c = c.astype("int")
cv2.drawContours(frame, [c], -1, (0, 255, 0), 2)
cv2.putText(frame, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (255, 255, 255), 2)
cv2.imshow("Gambar", frame)
key = cv2.waitKey(1) & 0xFF
# if the 'q' key is pressed, stop the loop
if key == ord("q"):
break
camera.release()
cv2.destroyAllWindows()
我已经编辑了这些代码,这些是代码:
from pyimagesearch.shapedetector import ShapeDetector
import argparse
import imutils
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",help="path to the (optional) video file")
ap.add_argument("-b", "--buffer",type=int, default=64,help="max buffer size")
args = vars(ap.parse_args())
if not args.get("video", False):
camera = cv2.VideoCapture(0)
else:
camera = v2.VideoCapture(args["video"])
while True:
(grabbed, frame) = camera.read()
if args.get("video") and not grabbed:
break
frame = imutils.rotate(frame, angle=180)
resized = imutils.resize(frame, width=300)
ratio = frame.shape[0] / float(resized.shape[0])
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
sd = ShapeDetector()
for c in cnts:
M = cv2.moments(c)
cX = int((M["m10"] / M["m00"]) * ratio)
cY = int((M["m01"] / M["m00"]) * ratio)
shape = sd.detect(c)
c = c.astype("float")
c *= ratio
c = c.astype("int")
cv2.drawContours(frame, [c], -1, (0, 255, 0), 2)
cv2.putText(frame, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (255, 255, 255), 2)
cv2.imshow("Gambar", frame)
key = cv2.waitKey(1) & 0xFF
# if the 'q' key is pressed, stop the loop
if key == ord("q"):
break
camera.release()
cv2.destroyAllWindows()
在我运行之后,我注意到
文件 "detect_shapes_using_video.py",第 61 行,在
cX = int((M["m10"] / M["m00"]) * 比率)
ZeroDivisionError: 浮点除以零
我该怎么办?因为我还是 python 的初学者,我不知道该怎么做。这些代码用于在 raspberry pi
中使用相机进行图像处理这个街区
M = cv2.moments(c)
cX = int((M["m10"] / M["m00"]) * ratio)
cY = int((M["m01"] / M["m00"]) * ratio)
就是求图像中找到的轮廓的质心。 ZeroDivisionError: float division by zero
发生在 M["m00"] = 0
时,因为轮廓只是一条线。
您可以添加条件 if M["m00"] == 0: shape = "line"
来避免此错误。
[编辑]这是我的建议:
from pyimagesearch.shapedetector import ShapeDetector
import argparse
import imutils
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",help="path to the (optional) video file")
ap.add_argument("-b", "--buffer",type=int, default=64,help="max buffer size")
args = vars(ap.parse_args())
if not args.get("video", False):
camera = cv2.VideoCapture(0)
else:
camera = v2.VideoCapture(args["video"])
while True:
(grabbed, frame) = camera.read()
if args.get("video") and not grabbed:
break
frame = imutils.rotate(frame, angle=180)
resized = imutils.resize(frame, width=300)
ratio = frame.shape[0] / float(resized.shape[0])
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
sd = ShapeDetector()
for c in cnts:
M = cv2.moments(c)
if(M["m00"]==0): # this is a line
shape = "line"
else:
cX = int((M["m10"] / M["m00"]) * ratio)
cY = int((M["m01"] / M["m00"]) * ratio)
shape = sd.detect(c)
c = c.astype("float")
c *= ratio
c = c.astype("int")
cv2.drawContours(frame, [c], -1, (0, 255, 0), 2)
cv2.putText(frame, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (255, 255, 255), 2)
cv2.imshow("Gambar", frame)
key = cv2.waitKey(1) & 0xFF
# if the 'q' key is pressed, stop the loop
if key == ord("q"):
break
camera.release()
cv2.destroyAllWindows()