From 8940872a79c0186104569a454498f320cc28626b Mon Sep 17 00:00:00 2001 From: Cole Deck Date: Sun, 8 Dec 2019 15:05:05 -0600 Subject: [PATCH] central line draw instead of bounding box, text position --- main.py | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index 5668174..bee7732 100644 --- a/main.py +++ b/main.py @@ -50,6 +50,13 @@ def near(a, b, close): if abs(a-b) < close: return True return False + +def swap(a, b): + tmp = a + a = b + b = tmp + + # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, @@ -210,7 +217,7 @@ for c in cnts: rectangular = False if boxiness > circleness: rectangular = True - cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) + #cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) else: circular = True cv2.circle(orig,(int(x),int(y)),int(radius),(0,255,0),2) @@ -260,7 +267,22 @@ for c in cnts: objtype = "Axle" iteml = (radius * 2 / pixelsPerMetric + itemw) / 2 - + rows,cols = orig.shape[:2] + [vx,vy,xx,yy] = cv2.fitLine(c, cv2.DIST_L2,0,0.01,0.01) + lefty = int((-xx*vy/vx) + yy) + righty = int(((cols-xx)*vy/vx)+yy) + #cv2.line(orig,(cols-1,righty),(0,lefty),(0,255,0),2) + slope = (lefty - righty) / (1 - cols) + angle = math.atan(slope) + xpos = x - math.cos(angle) * radius + ypos = y - math.sin(angle) * radius + xpos2 = x + math.cos(angle) * radius + ypos2 = y + math.sin(angle) * radius + if xpos > xpos2: + swap(xpos, xpos2) + swap(ypos, ypos2) + if rectangular: + cv2.line(orig,(int(xpos),int(ypos)),(int(xpos2), int(ypos2)),(0,255,0),2) #print(str(iteml)) # draw the object sizes on the image if args2.show: @@ -268,23 +290,23 @@ for c in cnts: # (int(trbrX + 20), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, # 0.65, (255, 255, 255), 2) cv2.putText(orig, str(objtype), - (int(trbrX + 20), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, + (int(xpos2 + 10), int(ypos2 + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) if objtype == "Unknown": cv2.putText(orig, "{:.2f}in".format(itemw) + " x {:.2f}in".format(itemh), # print axle length - (int(trbrX + 20), int(trbrY + 20)), cv2.FONT_HERSHEY_SIMPLEX, + (int(xpos2 + 10), int(ypos2 + 40)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) if objtype == "Screw": - cv2.putText(orig, str(iteml) + "in thread", # print screw length - (int(trbrX + 20), int(trbrY + 20)), cv2.FONT_HERSHEY_SIMPLEX, + cv2.putText(orig, str(iteml) + "in", # print screw length + (int(xpos2 + 10), int(ypos2 + 40)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) if objtype == "Standoff": cv2.putText(orig, str(iteml) + "in", # print standoff length - (int(trbrX + 20), int(trbrY + 20)), cv2.FONT_HERSHEY_SIMPLEX, + (int(xpos2 + 10), int(ypos2 + 40)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) if objtype == "Axle": cv2.putText(orig, "{:.2f}in".format(iteml), # print axle length - (int(trbrX + 20), int(trbrY + 20)), cv2.FONT_HERSHEY_SIMPLEX, + (int(xpos2 + 10), int(ypos2 + 40)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) # show the output image