Python WeChat jump series color block outline positioning board, python Color Block

Source: Internet
Author: User

The color block outline positioning board and python Color Block of the python hop Series

In the previous blog posts, we used color recognition, template matching, pixel traversal, and other methods to locate the chess pieces and board. For details, see my previous article, in this article, we will explore a new way to locate the board.

Analysis

After observation, we can see that there is always a very obvious Color comparison between the Board and the background under any circumstances, which is of course necessary, otherwise, the game players will not be able to distinguish chess pieces, boards, and backgrounds. Obviously, if we divide each image into color blocks and convert the color chart into a black-and-white two-value image, we can isolate the background from the checkerboard, then, the white outlines in the black and white charts are analyzed, and the outlines with the highest position (the smallest y value) are marked. This Outline is the next hop board.

Procedure

  • Capture images;
  • Converts an image to a grayscale image;
  • Determine the working area (h // 3-2 h // 3) and the pixel threshold value;
  • Generate a black and white binary image, and generate two kinds of black and white images, respectively, isolate the color blocks that are highlighted in the background and hidden in the background;

Shadow Processing

There are often shadows on the board. You can further narrow down the target area and divide the color block to precisely locate the board. Interested parties can exercise on their own.

Code

#-*-Coding: UTF-8-*-# VS2017 + python3.6 + opencv3.4 #2018.02.03 # Author: Ike import cv2 def thresh (img): x1, y1, w1, h1, x2, y2, w2, h2 =, gray = cv2.cvtColor (img, cv2.COLOR _ BGR2GRAY) # gray = cv2.GaussianBlur (gray, (13, 13), 0) # Gaussian Blur h0, w0 = img. shape [: 2] top = gray [h0 // 255] bottom = gray [h0*2 //] thresh1 = cv2.threshold (gray, top, cv2.THRESH _ BINARY) [1] thresh2 = cv2.threshold (gray, bottom, 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 = cv2.findContours (img2, cv2.RETR _ 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, 0), 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, 0), 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 main (): video = 'jump. avi 'Cap = cv2.VideoCapture (video) ret = cap. isOpened () ret = True while ret: # ret, img = cap. read () # read frame img = cv2.imread ('e:/python/jump/hsv/006.png ') if not ret: cv2.waitKey (0) point = thresh (img) cv2.circle (img, point, 3, (255, 255),-1) cv2.circle (img, point, 15, (,), 2) cv2.imshow ('img ', img) if cv2.waitKey (25) = ord ('q'): break cap. release () cv2.destroyAllWindows () if _ name __= = '_ main _': main ()

In opencv, the parameter types for black/white binary segmentation are as follows:

 

During code compilation, cv2.THRESH _ BINARY and cv2.THRESH _ BINARY_INV must be used together to distinguish both bright and dark colors.

Effect

Let's take a few examples of board recognition for your reference.

 

This is a typical example. The upper half of the board is divided into dark colors, the lower half is divided into bright colors, the first is the Separation and Identification of bright colors, and the second is the segmentation and identification of dark colors, finally, the color block outlines recognized by each other are drawn and compared. In this example, the final position is on the white point in the center of the board, and the effect is still very good.

 

This is also a very typical example. The bright and dark parts of the Board are intertwined. The bright and dark parts are recognized on the left and the dark part is recognized on the middle, the final comparison result is also in the center of the board, and the effect is good.

 

This example also locks the final position to the center of the board.

Put another one.

 

This is also a typical case. It is feasible to locate it at the center white point of the Board.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.