current position：Home>On the 55th day of the journey, python opencv perspective transformation front knowledge contour coordinate points
On the 55th day of the journey, python opencv perspective transformation front knowledge contour coordinate points
20220130 14:13:42 【Dream eraser】
Little knowledge , Great challenge ！ This article is participating in 「 A programmer must have a little knowledge 」 Creative activities
This article has participated in 「 Digging force Star Program 」 , Win a creative gift bag , Challenge creation incentive fund .
Python OpenCV 365 Day study plan , Go into the field of image with the eraser . This blog is the third in this series 55 piece .
Learning ahead
Before formal study , First the Last one Review the content of the blog , Through a series of contour operations , Get the contour of the target image .
The goal of this blog will gradually realize the perspective transformation .
Get the four vertex coordinates of the contour
Last blog has got the outline coordinates , But the position of each coordinate is uncertain , It needs to be made clear by calculation . The contour coordinates are changed from 3D to 2D
cv.drawContours(src, [screen_cnt], 1, (0, 0, 255), 2)
print(screen_cnt)
print(screen_cnt.shape)
print(screen_cnt.reshape(4, 2))
Copy code
The code works as follows , Different pictures get different values .
[[[ 30 94]]
[[ 17 273]]
[[469 278]]
[[462 106]]]
(4, 1, 2)
[[ 30 94]
[ 17 273]
[469 278]
[462 106]]
Copy code
After coordinate transformation , The position corresponding to the picture is shown in the figure below .
In general , We describe a rectangular area , stay OpenCV in , It's using Top left , The upper right , The lower right , The lower left Clockwise order , So next we need to transform these four coordinates .
Declare a function to perform the corresponding calculation .
def change_points(input_points):
pass
# Function call
change_points(screen_cnt.reshape(4, 2))
Copy code
Calculation 4 The sum of the abscissa and ordinate of each point
def change_points(input_points):
s = input_points.sum(axis=1)
print(s)
Copy code
The core is numpy Medium sum
function , Be careful axis
Shaft parameters ,0
Vertical means vertical ,1
It means horizontal , The transverse direction here is the sum of the abscissa and ordinate of the point .
For a rectangular area , The upper left point and the lower right point are the point with the smallest sum of abscissa and ordinate and the point with the largest sum of abscissa and ordinate respectively , Based on this , Continue to improve the code .
The following code uses np.argmin()
and np.argmax()
function , You can learn quickly through search engines .
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 code
The data obtained are as follows ：
[[ 30 94]
[ 17 273]
[469 278]
[462 106]]
[30 94] [469 278]
Copy code
The lower left point and the upper right point are calculated as , Use np.diff()
function , Calculate the difference between abscissa and ordinate to find the point in the upper right corner , The difference between abscissa and ordinate is the largest. Find the point in the lower left corner . Corresponding to the figure below can help understand , Lower left 300100 = 200
, Top right 100400 = 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 code
here ,4 All coordinates have been obtained , Finally, declare an empty matrix , The assignment is followed by a return .
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 that all elements are 0 Matrix
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 code
The corresponding position of coordinate value and picture is as follows ：
Eraser bars
I hope today's 1 Hours （ It doesn't seem to be enough ） You get something , I'll see you on our next blog ~
copyright notice
author[Dream eraser],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301413410578.html
The sidebar is recommended
 Python code reading (Part 44): find the location of qualified elements
 Elegant implementation of Django model field encryption
 40 Python entry applet
 Pandas comprehensive application
 Chapter 2: Fundamentals of python3 character string
 Python pyplot draws a parallel histogram, and the xaxis value is displayed in the center of the two histograms
 [Python crawler] detailed explanation of selenium from introduction to actual combat [1]
 Curl to Python self use version
 Python visualization  3D drawing solutions pyecharts, Matplotlib, openpyxl
 Use python, opencv's meanshift and CAMSHIFT algorithms to find and track objects in video
guess what you like

Using python, opencv obtains and changes pixels, modifies image channels, and trims ROI

[Python data collection] university ranking data collection

[Python data collection] stock information collection

Python game development, pyGame module, python takes you to realize a magic tower game from scratch (2)

Python solves the problem of suspending execution after clicking the mouse in CMD window (fast editing mode is prohibited)

[Python from introduction to mastery] (II) how to run Python? What are the good development tools (pycharm)

Python type hints from introduction to practice

Python notes (IX): basic operation of dictionary

Python notes (8): basic operations of collections

Python notes (VII): definition and use of tuples
Random recommended
 Python notes (6): definition and use of lists
 Python notes (V): string operation
 Python notes (IV): use of functions and modules
 Python notes (3): conditional statements and circular statements
 Python notes (II): lexical structure
 Notes on python (I): getting to know Python
 [Python data structure series]  tree and binary tree  basic knowledge  knowledge point explanation + code implementation
 [Python daily homework] Day7: how to combine two dictionaries in an expression?
 How to implement a custom list or dictionary in Python
 15 advanced Python tips for experienced programmers
 Python string method tutorial  how to use the find() and replace() functions on Python strings
 Python computer network basics
 Python crawler series: crawling global airport information
 Python crawler series: crawling global port information
 How to calculate unique values using pandas groupby
 Application of builtin distribution of Monte Carlo simulation SciPy with Python
 Gradient lifting method and its implementation in Python
 Pandas: how to group and calculate by index
 Can you create an empty pandas data frame and fill it in?
 Python basic exercises teaching! can't? (practice makes perfect)
 Exploratory data analysis (EDA) in Python using SQL and Seaborn (SNS).
 Turn audio into shareable video with Python and ffmpeg
 Using rbind in python (equivalent to R)
 Pandas: how to create an empty data frame with column names
 Talk about quantifying investment using Python
 Python, image restoration in opencv  CV2 inpaint
 Python notes (14): advanced technologies such as objectoriented programming
 Python notes (13): operations such as objectoriented programming
 Python notes (12): inheritance such as objectoriented programming
 Chapter 2: Fundamentals of python5 Boolean
 Python notes (11): encapsulation such as objectoriented programming
 Python notes (10): concepts such as objectoriented programming
 Gradient lifting method and its implementation in Python
 Van * Python  simple crawling of a site course
 Chapter 1 preliminary knowledge of pandas (list derivation and conditional assignment, anonymous function and map method, zip object and enumerate method, NP basis)
 Nanny tutorial! Build VIM into an IDE (Python)
 Fourier transform of Python OpenCV image processing, lesson 52
 Introduction to python (III) network request and analysis
 China Merchants Bank credit card number recognition project (Part I), python OpenCV image processing journey, Part 53
 Python practice  capture 58 rental information and store it in MySQL database