Pixel traversal in the position of chess pieces in the python WeChat jump series, python chess pieces

Source: Internet
Author: User

Pixel traversal in the position of chess pieces in the python hop Series

Preface

In the previous blogs, we introduced and practiced color recognition, template matching, and other positioning methods of pawns, this blog will verify the pixel Traversal method used in the most popular jump plug-in on github.

Method description

The essence of pixel traversal is color recognition.
In the method provided in github, the pixel Traversal method is as follows:

  • Traverse from 1/3 in height to 2/3 in height;
  • Search at an interval of 50 pixels
  • When the pixel color is different from the pixel color at the beginning of each line, it is considered that the top board position is found, then return the previous interval start traversal (i-50 );
  • The pixels of each row are traversed. When the given color range is met, the y value and the sum and number of x values in the bottom row are recorded, and the center x value is obtained on average.
  • The obtained x and y values are the center position at the bottom of the chess piece;
  • Adjust the height of the chess piece according to the resolution of your mobile phone (minus a fixed value)

The code for the appeal method is as follows (you can download it from github ):

Def find_piece_and_board (im): # Find the key coordinates w, h = im. size piece_x_sum = 0 piece_x_c = 0 piece_y_max = 0 board_x = 0 board_y = 0 scan_x_border = int (w/8) # scan_start_y = 0 # Start y coordinate im_pixel = im. load () # With a 50px step, try to detect scan_start_y for I in range (int (h/3), int (h * 2/3), 50): last_pixel = im_pixel [0, i] for j in range (1, w): pixel = im_pixel [j, I] # The value of scan_start_y is recorded if Pixel! = Last_pixel: scan_start_y = I-50 break if scan_start_y: break print ('Scan _ start_y :{}'. format (scan_start_y) # scan down from scan_start_y. The pawn pieces should be located in the upper part of the screen. It is tentatively set to 2/3 for I in range (scan_start_y, int (h * 2/3 )): # in terms of abscissa, the scanning overhead is also reduced for j in range (scan_x_border, w-scan_x_border): pixel = im_pixel [j, I] # determined based on the color of the lowest row of the pawns, find the average of the vertices in the last line. The color # should be OK. if (50 <pixel [0] <60) \ and (53 <pixel [1] <63) \ and (95 <pixel [2] <110): piece_x_sum + = j piece_x_c + = 1 piece_y_max = max (I, piece_y_max) if not all (piece_x_sum, piece_x_c): return 0, 0, 0, 0 piece_x = int (piece_x_sum/piece_x_c) piece_y = piece_y_max-piece_base_height_1_2 # half of the height of the move-up chess piece Chassis

Dynamic demonstration of traversal Process

Actual running animation

The following are the positioning animations that are running.

Advantage and disadvantage Analysis

The advantage of pixel traversal is that no additional python library is needed, but the speed is obviously lower than the other method I used earlier. This is not surprising, the color recognition methods used by external libraries are optimized and integrated. pixel traversal is the underlying technical solution, and the speed is slow.

Improvement

In fact, the above method can be further optimized to greatly reduce the traversal area:

  • The traversal starts from the height of 1/3 at an interval of 50 pixels;
  • When the color of the given piece is satisfied for the first time, the sum and number of x values are recorded, and the x value of the piece is obtained after the average value;
  • Traverse the x value from top to bottom to obtain the maximum y value and the minimum y value that meet the color of the pawn;
  • The y value is processed. For example, if a fixed height is subtracted, the y value at the bottom of the chess piece is obtained;
  • Average the highest point and the lowest point to obtain the y value in the center of the board.

The traversal process is as follows:

Postscript

We can see that the optimized traversal quantity is greatly reduced, and the speed is greatly improved.

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.