Tooth uncle tutorial is easy to understand

First rule of problem solving

Write the questions down

The original image

saturation

lightness

gray

There’s no difference between the dimensions of the picture,

Use the original

Hoff find round

You can see that the circle is basically in the right place, but it’s a little bit off,

Let’s make circles more accurate

The figure above has found the circle. Based on this circle, correct the deviation of the circle by the color of the outline or text.

Theoretically correct the position of the circle, not the focus of this paper, only for reference

Pawn screenshots

Above is a screenshot of all the pieces, after the selected pieces, the four corners will be marked red

It might interfere with the map finding, so let’s make the radius of the pieces smaller

One thing I didn’t figure out was which piece the picture was

This should be inferred from the position of the circle

Circles are considered to be in the same row as long as their ordinates don’t differ much

All circles are divided into 6 groups, bottom + medium + 5 pawns

  • Red three groups
  • Black three groups

Iterate over the circle, taking the first circle as the starting class and comparing the other circles with it on the ordinate,

If there is not much difference, it goes into one category, otherwise it goes into another category,

Finally, we sorted them in ordinate order to separate the six groups of circles

The groups are done. Let’s sort the groups in ordinate order

pieceGroups.sort(function (pieceGroup1, pieceGroup2) { let piece1 = pieceGroup1[0]; let piece2 = pieceGroup2[0]; if (piece1.circle.y < piece2.circle.y) { return -1; } else if (piece1.circle.y > piece2.circle.y) { return 1; } else { return 0; }});Copy the code

Next to each set of pieces, you have to sort them on the x-coordinate

for (var i = 0; i < pieceGroups.length; i++) { let pieceGroup = pieceGroups[i]; pieceGroup.sort(function (piece1, piece2) { if (piece1.circle.x < piece2.circle.x) { return -1; } else if (piece1.circle.x > piece2.circle.x) { return 1; } else { return 0; }}); }Copy the code

Re-cut the picture of the pieces, and record the names of the pieces;

For the time being, the colors are as shown in the picture, black at the top and red at the bottom.

In post-optimization, the color can take multiple points on the image, vote democratically,

That’s a popular color. That’s the color

Not the focus of this tutorial, ideas for reference only

for (var i = 0; i < pieceGroups.length; i++) { let pieceGroup = pieceGroups[i]; for (var j = 0; j < pieceGroup.length; j++) { let piece = pieceGroup[j]; let circle = piece.circle; let checkPieceArea = calculateCheckPieceArea(img, circle); let chessPieceImg = clipChessPieceImg(img, checkPieceArea); let color; If (count < 15) {color = "black "; } else {color = "red "; } let filePath = dir + "/" + piece.num + "-" + color + "-" + pieceNames[count] + ".png"; log(filePath); files.createWithDirs(filePath); images.save(chessPieceImg, filePath); chessPieceImg.recycle(); count++; }}Copy the code

We’re all set. Check it out,

Validation step

  1. A few moves of chess
  2. screenshots
  3. View identification results

I found another problem with duplicate pieces,

Start over. Delete the duplicate pieces no

for (var i = 0; i < pieceGroups.length; i++) { let pieceGroup = pieceGroups[i]; for (var j = 0; j < pieceGroup.length; j++) { let piece = pieceGroup[j]; let circle = piece.circle; let checkPieceArea = calculateCheckPieceArea(img, circle); let chessPieceImg = clipChessPieceImg(img, checkPieceArea); let color; If (count <= 15) {color = "black "; } else {color = "red "; } let archivedPiece = archivedPieces[color + pieceNames[count]]; If (archivedPiece) {// duplicate pieces, leaving only one count++; continue; } else { archivedPieces[color + pieceNames[count]] = 1; } let filePath = dir + "/" + piece.num + "-" + color + "-" + pieceNames[count] + ".png"; files.createWithDirs(filePath); images.save(chessPieceImg, filePath); chessPieceImg.recycle(); count++; }}Copy the code

At this point, there should be 14 pictures of the pieces

Identify the chess again and write in green after identifying it

for (var i = 0; i < pieces.length; i++) { let piece = pieces[i]; Var points = images.matchtemplate (img, piece.img, {threshold: 0.9,}).points; if (points) { points.forEach((point) => { canvas.drawText(piece.name, point.x, point.y, textPaint); }); }}Copy the code

All correctly identified

The test environment

Mi 11 Pro Android version: 12 Autojs version: 9.1.13

Quotes.

Ideas are the most important, other baidu, Bing, StackOverflow, Github, Android docs, AutoJS docs, and last but not least, ask in the group

This tutorial is intended for learning purposes only and is not intended for other use

Wechat official account tooth Uncle tutorial