opencv - ZeroDivisionError (Python) -
i getting 0 division error images (even though lot of them work fine) :
here's code :
image = skimage.io.imread('test.png', false) image_gray = skimage.io.imread('test.png', true) blurred = cv2.gaussianblur(img_as_ubyte(image_gray), (5, 5), 0) thresh = threshold_li(blurred) binary = blurred > thresh binary_cv2 = img_as_ubyte(binary) # find contours in thresholded image cnts = cv2.findcontours(binary_cv2.copy(), cv2.retr_external, cv2.chain_approx_simple) cnts = cnts[0] if imutils.is_cv2() else cnts[1] # loop on contours c in cnts: # compute center of contour m = cv2.moments(c) cx = int(m["m10"] / m["m00"]) cy = int(m["m01"] / m["m00"]) # draw contour , center of shape on image cv2.drawcontours(img_as_ubyte(image), [c], -1, (0, 255, 0), 2) cv2.circle(img_as_ubyte(image), (cx, cy), 7, (255, 255, 255), -1) cv2.puttext(img_as_ubyte(image), "center", (cx - 20, cy - 20), cv2.font_hershey_simplex, 0.5, (255, 255, 255), 2) viewer = imageviewer(image) viewer.show() traceback (most recent call last): file "center.py", line 26, in <module> cx = int(m["m10"] / m["m00"]) zerodivisionerror: float division 0 thanks in advance!
you cannot devide zero. if m["m00"] zero, there no solution division. check 0 values.
if m["m00"] != 0: cx = int(m["m10"] / m["m00"]) cy = int(m["m01"] / m["m00"]) else: cx, cy = 0, 0
Comments
Post a Comment