Automatically calculates the Hop Distance and python distance in the python hop series.
So far, through the descriptions and analysis in the previous blog posts, we have been able to automatically determine the position of the chess pieces and the Board, and calculate the distance between the two centers, and draw the image as follows.
Effect
The chess pieces in the figure are identified by HSV color, and the Board positioning is obtained by Contour Segmentation. Interested parties can verify the other positioning methods themselves.
Code
#-*-Coding: UTF-8-*-# VS2017 + python3.6 + opencv3.4 #2018.02.03 # Author: Ike import cv2 import numpy as npimport mathdef hsv (frame): lower_blue = np. array ([115,75, 75]) # sets the blue threshold value upper_blue = np. array ([130,255,125]) r = 0 # initial radius = 0 x, y = hsv = cv2.cvtColor (frame, cv2.COLOR _ BGR2HSV) # convert to HSV space mask_blue = cv2.inRange (hsv, lower_blue, upper_blue) CNT = cv2.findContours (mask_blue, cv2.RETR _ EXTERNAL, cv2.CHAIN _ APPROX_SIMPLE) [-2] if len (CNT)> 0: c = max (CNT, key = cv2.20.area) # Find the contour with the largest area (x, y), radius) = cv2.minEnclosingCircle (c) # determine the outer circle center of the contour with the largest area = (int (x ), int (y) return centerdef thresh (img): x, y, w, h, x1, y1, w1, h1, x2, y2, w2, h2 = 0, 0, 0, 0, 0, 0, 0, 0, 0 gray = cv2.cvtColor (img, cv2.COLOR _ BGR2GRAY) # gray = cv2.GaussianBlur (gray, (13, 13), 0) # convert to grayscale h0, w0 = img. shape [: 2] top = gray [h0 // 3, 1] bottom = gray [h0*2 // 3, 1] # min_vale = min (top, bottom) # max_vale = max (top, bottom) thresh1 = cv2.threshold (gray, top, 255, cv2.THRESH _ BINARY) [1] thresh2 = cv2.threshold (gray, 175,255, cv2.THRESH _ BINARY_INV) [1] img1 = thresh1 [h0 // 3: h0 * 2 // 3,0: w0] img2 = thresh2 [h0 // 3: h0 * 2 // 3,0: w0] cnts1, hierarchy1, rr1 = reverse (img1, cv2.RETR _ EXTERNAL, cv2.CHAIN _ APPROX_SIMPLE) cnts2, hierarchy2, rr2 = reverse (img2, EXTERNAL _ EXTERNAL, cv2.CHAIN _ APPROX_SIMPLE) aim1 = 0 y_min = h0 // 3 for c in hierarchy1: if hierarchy1 = None: x1, y1, w1, h1 = w0 // 2, h0 // 3, w0 // 3, h0 // 3 break else: x, y, w, h = cv2.boundingRect (c) if y <= y_min: y_min = y aim1 = c x1, y1, w1, h1 = cv2.boundingRect (aim1) # cv2.rectangle (img, (x1, y1 + h0 // 3), (x1 + w1, y1 + h1 + h0 // 3 ), (255,), 2) aim2 = 0 y_min = h0 // 3 for c in hierarchy2: if hierarchy2 = None: x2, y2, w2, h2 = w0 // 2, h0 // 3, w0 // 3, h0 // 3 break else: x, y, w, h = cv2.boundingRect (c) if y <= y_min: y_min = y aim2 = c x2, y2, w2, h2 = cv2.boundingRect (aim2) # cv2.rectangle (img, (x2, y2 + h0/3 ), (x2 + w2, y2 + h2 + h0 // 3), (0,255,255), 2) if y1 + h1 // 2 <= y2 + h2 // 2: x, y, w, h = x1, y1, w1, h1 else: x, y, w, h = x2, y2, w2, h2 cv2.imshow ('img1', thresh1) cv2.imshow ('img2', thresh2) return (x + w // 2, y + h0 // 3 + h // 2) def length (pt1, pt2): x1, y1 = pt1 x2, y2 = pt2 length = math. sqrt (x2-x1) ** 2 + (y2-y1) ** 2) return int (length) def main (): filepath = 'e: /python/jump/hsv/007.png 'video = 'e:/python/jump/blackwhite/jumpnew. avi 'Cap = cv2.VideoCapture (video) ret = cap. isOpened () ret = True while ret: # ret, img = cap. read () # read frame img = cv2.imread (filepath) if not ret: cv2.waitKey (0) point1 = hsv (img) point2 = thresh (img) len = length (point1, point2) cv2.circle (img, point1, 3, (255, 255),-1) cv2.circle (img, point1, 15, (,), 2) cv2.circle (img, point2, 3, (255, 255),-1) cv2.circle (img, point2, 15, (255,255,255,), 2) cv2.line (img, point1, point2 ), 2) cv2.putText (img ,'{}'. format (len), (point2 [0]-10, point2 [1]-20), cv2.FONT _ HERSHEY_SIMPLEX, 0.6, (0, 0,255), 2, cv2.LINE _ 8, 0) cv2.imshow ('img ', img) # cv2.imwrite (filepath, img) cv2.waitKey (0) cap. release () cv2.destroyAllWindows () if _ name __= = '_ main _': main ()
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.