Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Python OpenCV 365 day learning plan, enter the graphics realm with eraser. This blog is the 55th in the series.

Learned in the previous

Before formal learning, first review the content of the last blog, through a series of contour operations, get the contour of the target image.

The goal of this blog is to gradually transform it into perspective.

Gets the coordinates of the four vertices of the contour

The last blog post got the contour coordinates, but the position of each coordinate is uncertain and needs to be determined by calculation. Contour coordinates changed from three maintenance to two dimensional

cv.drawContours(src, [screen_cnt], -1, (0.0.255), 2)

print(screen_cnt)
print(screen_cnt.shape)
print(screen_cnt.reshape(4.2))
Copy the code

The code works as follows, with different values for different images.

[[94] [30] [273] [17] [278] [469] [106] [462]] (4, 1, 2) [94] [30 [273] [469] 278 [106] 462]Copy the code

After coordinate transformation, the corresponding position of the picture is shown in the figure below.

In general, we describe a rectangular region, and in OpenCV, we useUp left, up right, down right, down leftClockwise, so we need to convert these four coordinates.

Declare a function to perform the corresponding computation.

def change_points(input_points) :
	pass

# function call
change_points(screen_cnt.reshape(4.2))
Copy the code

Compute the sum of the horizontal and vertical coordinates of the four points

def change_points(input_points) :
    s = input_points.sum(axis=1)
    print(s)
Copy the code

The core uses the sum function in NUMpy. Pay attention to the axis parameter, 0 represents vertical and 1 represents horizontal, where horizontal is the sum of the horizontal and vertical coordinates of the point.

For a rectangular region, the upper left point and the lower right point are the points with the smallest sum of horizontal and vertical coordinates and the largest point respectively. Based on this, continue to improve the code.

The following code uses the np.argmin() and np.argmax() functions for quick learning through a search engine.

def change_points(input_points) :
    s = input_points.sum(axis=1)
    p1 = input_points[np.argmin(s)]
    p3 = input_points[np.argmax(s)]
    print(p1,p3)
Copy the code

The data obtained are as follows:

[[30 94] [17 273] [469 278] [462 106]] [30 94] [469 278]Copy the code

The lower left and upper right points are calculated by using the np.diff() function, minimizing the difference between the horizontal and vertical coordinates and finding the point in the upper right corner, maximizing the difference between the horizontal and vertical coordinates and finding the point in the lower left corner. The lower left point 300-100 = 200, and the upper right point 100-400 = -300.

def change_points(input_points) :
    s = input_points.sum(axis=1)
    p1 = input_points[np.argmin(s)]
    p3 = input_points[np.argmax(s)]
    print(p1,p3)

    diff = np.diff(input_points,axis=1)
    p2 = input_points[np.argmin(diff)]
    p4 = input_points[np.argmax(diff)]
    print(p2,p4)
Copy the code

At this point, all four coordinates have been obtained, and an empty matrix is declared and returned after the assignment.

def change_points(input_points) :
    s = input_points.sum(axis=1)
    p1 = input_points[np.argmin(s)]
    p3 = input_points[np.argmax(s)]

    diff = np.diff(input_points, axis=1)
    p2 = input_points[np.argmin(diff)]
    p4 = input_points[np.argmax(diff)]

    Declare a matrix where all elements are 0
    rect = np.zeros((4.2), dtype="float32")
    rect[0] = p1
    rect[1] = p2
    rect[2] = p3
    rect[3] = p4
    print(rect)
    return rect
change_points(screen_cnt.reshape(4.2))
Copy the code

The corresponding position of the coordinate value and the picture is as follows:

Eraser section

I hope you got something out of your hour today, and we’ll see you in our next blog