Python jump series: color recognition for chess pieces and python chess pieces
Python jump, Preface
This is an article in the python jump series. The main content is to use the color recognition method to locate the jump villain.
Color Recognition
After observation, we can find that, despite constant changes in the background and chess pieces, the shape and color of the jump villain remain unchanged. For the shape, in the previous blog, we used the template matching method for identification and positioning, which worked very well. This blog post will verify the color recognition.
Basic Ideas
Use the HSV color space to process the input image, mask the image with a specified color, and then obtain a binarization black and white image. After expansion and corrosion, remove noise, calculate the contour area, draw the center and center of the center, and achieve dynamic tracking.
The basic steps are as follows:
Set the required Color Threshold
Read Image
Convert to HSV Image
Color masks are used for binarization to obtain black and white images.
Noise reduction and contour Processing
Draw the center
Python3.6 code
Import cv2 import numpy as np import timelower_blue = np. array ([115,75, 75]) # Set the blue threshold upper_blue = 0000') hsv = cv2.cvtColor (frame, cv2.COLOR _ BGR2HSV) # convert to the 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) cv2.circle (frame, center, int (radius + 10), (0, 0,255), 3) # Draw the center cv2.circle (frame, center, 3, (0, 0,255),-1) cv2.circle (hsv, center, int (radius + 10), (255,255,255), 3) # Draw the center cv2.circle (hsv, center, 3, (0, 0,255),-1) cv2.circle (mask_blue, center, int (radius + 10), (255,255,255), 3) # Draw the cv2.circle (mask_blue, center, 3, (0, 0,255),-1) cv2.imshow ('framework', frame) cv2.imshow ('hsv ', hsv) cv2.imshow ('mask', mask_blue) if cv2.waitKey (0) = ord ('q'): cv2.destroyAllWindows ()
Static Image Recognition
The left side is the original image, the middle is the second value after the mask, and the right side is the HSV image. The recognition effect is good.
We can see that the selected color can basically filter out all the outlines of the villain. The biggest part of the outline is just on the chassis, and the center of the circle is exactly what we need.
Dynamic Real-time Identification
A dynamic graph is provided to show the effect of Dynamic Real-time recognition.
Rating
The color is used to identify and locate the chassis position of the jump villain. This method has nothing to do with the pixel and screen size of the mobile phone, and is truly universal to all platforms.
Notice
In the next blog, I will verify the color Traversal method used by wechat-jump on github.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.