Example of python calculating the percentage of overlap between two rectangular boxes
As shown below:
def mat_inter(box1,box2): # Determine if two rectangles intersect # box=(xA,yA,xB,yB) x01, y01, x02, y02 = box1 x11, y11, x12, y12 = box2 lx = abs((x01 + x02) / 2 - (x11 + x12) / 2) ly = abs((y01 + y02) / 2 - (y11 + y12) / 2) sax = abs(x01 - x02) sbx = abs(x11 - x12) say = abs(y01 - y02) sby = abs(y11 - y12) if lx <= (sax + sbx) / 2 and ly <= (say + sby) / 2: return True else: return False def solve_coincide(box1,box2): # box=(xA,yA,xB,yB) # Calculate the overlap of two rectangular boxes if mat_inter(box1,box2)==True: x01, y01, x02, y02 = box1 x11, y11, x12, y12 = box2 col=min(x02,x12)-max(x01,x11) row=min(y02,y12)-max(y01,y11) intersection=col*row area1=(x02-x01)*(y02-y01) area2=(x12-x11)*(y12-y11) coincide=intersection/(area1+area2-intersection) return coincide else: return False
The above example of python calculating the percentage of overlap of two rectangular boxes is all that I have shared with you.
Related articles
Example of Python crawling stock information and visualizing the data
This article mainly introduces the Python crawl stock information, and visualize the data example, to help you better understand and use python crawler, interested parties can understand the following2020-09-09File Input/Output Problems in Python
This article introduces the file input and output problems in Python, with good reference value, I hope to help you. If there is any error or not fully considered, please do not hesitate to advice2022-11-11Python Dictionary DICT Type Merge Explained
This article is mainly for you to introduce in detail the python dictionary DICT type merge, with some reference value, interested partners can refer to it!2017-08-08Django multi-process rolling log problem solution
This article introduces the Django multi-process rolling log problem solution, the text of the sample code through the introduction of a very detailed, for everyone to learn or work with a certain reference to the learning value of the need for friends can refer to the next!2019-12-12Explaining Python dictionary operations in detail
In this article, I have organized the relevant knowledge about Python dictionaries and how to operate, interested friends to learn.2019-03-03Python web page body conversion voice file operation method
This article mainly introduces the Python web page body to convert the operation of the voice file, need friends can refer to the following2018-12-12Python function's universal parameter pass parameter details
This article introduces the python function of the universal parameter pass reference detailed, the text of the sample code through the introduction of the very detailed, on everyone's learning or work has a certain reference learning value, the need for friends can refer to the following2019-07-07The method of cutting large text files with Python
Today, I'd like to share with you a method to realize large text file cutting with Python, with good reference value, I hope it will help you. Together follow the editor over to see it2019-01-01Troubleshooting PyCharm's problem of running unit tests instead of scripts
Today I'd like to share with you a solution to the problem that PyCharm does not run scripts, but runs unit tests, with good reference value, I hope it will help you. Together follow the editor over to see it2019-01-01The basic data types in Python set collection knowledge point summary
In this article, I summarize a summary of the contents of the basic data type set collection in Python, friends in need can learn.2021-08-08