Live up to the time, the creation of non-stop, this article is participating in 2021 year-end summary essay contest

preface

Using Python to achieve OpenCV edge detection. Without further ado.

Let’s have a good time

The development tools

Python version: 3.6.4

Related modules:

Cv2 module;

Numpy module;

And some modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required related modules.

Edge detection is the use of OpenCV Canny function, although the algorithm is very complex, but the code is very simple.

In 5 steps, gaussian filter is used to denoise the image, gradient is calculated, non-maximum suppression (NMS) is used on the edge, false positive is used on the detected edge, all edges and their connections are analyzed, real edges are retained and non-obvious edges are eliminated.

The following is to achieve a “jump jump” edge detection, to obtain the center of the box.

Edge detection

Canny edge detection code

import cv2
import numpy as np

# Read the original image
img = cv2.imread('game.png'.0)

# Display the original image
cv2.namedWindow('img'.0)
cv2.resizeWindow('img'.400.600)
cv2.imshow('img', img)

# Gaussian blur
img_rgb = cv2.GaussianBlur(img, (5.5), 0)
canny_img = cv2.Canny(img_rgb, 1.10)

# Display edge detection image
cv2.namedWindow('canny'.0)
cv2.resizeWindow('canny'.400.600)
cv2.imshow('canny', canny_img)

# Output edge detection image height and width
H, W = canny_img.shape
print(H, W)
Copy the code

The output images are 1920 and 1080 in height and width respectively

Below are the original image grayscale image and edge detection image

Find the first vertex (top vertex) of the block by edge detection image

# The height of the first vertex,row is the list (representing the pixel value of each row), Max (row) gets the largest pixel value in the list \
y_top = np.nonzero([max(row) for row in canny_img[400: []])0] [0] + 400
Copy the code

Walk through the image with a height greater than 400 (this removes the influence of the number 270 and the small block above)

Np.nonzero () gets the position where the list element is not zero, the first being the height of the top vertex

Gets the width of the upper vertex

The width of the first vertex \
x_top = int(np.mean(np.nonzero(canny_img[y_top])))
Copy the code

Determine the position of the vertices under the cube

To bypass the effect of the small white circle, add 80 pixels to the height of the top vertex

It then moves down, keeping the width constant, until it finds a point where the pixel value is not zero

I get the coordinates of the lower vertex of the cube

# Jump over the little white circle and walk through it
y_bottom = y_top + 80
for row in range(y_bottom, H):
    ifcanny_img[row, x_top] ! =0:
        y_bottom = row
        break

# Get the center of the square
x_center, y_center = x_top, (y_top + y_bottom) // 2

Draw a circle with the center of the square as the center
cv2.circle(canny_img, (x_center, y_center), 33, (255.0.255), 2)

# Display the resulting image
cv2.namedWindow('result'.0)
cv2.resizeWindow('result'.400.600)
cv2.imshow('result', canny_img)

# end
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

The center point of the box is obtained by the coordinates of the upper and lower vertices

The left image is the original image of edge detection, and the right image is the image of finding the center point of the square and drawing the circle with the center point as the center of the circle

Beating implementation

Now calculate the distance between the two centers using the small checkers position obtained from the previous template match

Check three strands, four strings and five, and you get the distance between the two centers

If you’ve ever done a hop, you know that the amount of time you need to press is different for different distances

You can set a parameter for distance and press time, which is set to 1.35

Complete the “hop and skip” automation by completing a certain press time with ADB command