The source is Mr. Bai Xiang of Huazhong University of science and technology.
import numpy as np
import shapely
from shapely.geometry import Polygon,MultiPoint #Polygon
line1=[2,0,2,2,0,0,0,0,2] #One-dimensional array representation of the coordinates of the four points of the quadrilateral, [x,y,x,y....]
a = np.array(line1).reshape(4, 2) # quadrilateral two-dimensional coordinate representation
poly1 = Polygon(a).convex_hull # python quadrilateral object, will automatically calculate four points, the last four points in the order of: top left bottom right bottom right top left top
print(Polygon(a).convex_hull) # you can print to see if this is the case
line2=[1,1,4,1,4,4,1,4]
b=np.array(line2).reshape(4, 2)
poly2 = Polygon(b).convex_hull
print(Polygon(b).convex_hull)
union_poly = np.concatenate((a,b)) #Merge two box coordinates to become 8*2
#print(union_poly)
print(MultiPoint(union_poly).convex_hull) # contains the smallest polygon point of the two quadrilaterals
if not poly1.intersects(poly2): #If the two quadrilaterals do not intersect
iou = 0
else:
try:
inter_area = poly1.intersection(poly2).area #intersection area
print(inter_area)
#union_area = poly1.area + poly2.area - inter_area
union_area = MultiPoint(union_poly).convex_hull.area
print(union_area)
if union_area == 0:
iou= 0
#iou = float(inter_area)/(union_area-inter_area) #wrong
iou=float(inter_area)/union_area
# iou=float(inter_area) /(poly1.area+poly2.area-inter_area)
# The source code gives two ways to calculate IOU, the first one is: intersection part / area of the smallest polygon containing two quadrilaterals
# The second one: intersection/merge (common way to calculate IOU of rectangular box)
except shapely.geos.TopologicalError:
print('shapely.geos.TopologicalError occured, iou set to 0')
iou = 0
print(a)
print(iou)
Read More:
- Read in wannier90 output file hr.dat to construct real space Hamiltonian to calculate Fermi surface
- Blender graphic tutorial: loop cut for polygon modeling commands
- A repeated string is composed of two identical strings. For example, abcabc is a repeated string with length of 6, while abcba does not have a duplicate string. Given any string, please help Xiaoqiang find the longest repeated substring.
- Latex sets page margin, page size, page margin and geometry macro package
- Edge detection: two methods
- [error reported] [Python] [Matplotlib] importerror: failed to import any QT binding
- error: src refspec master does not match any error:failed to push some refs to ‘[email protected]:xxxx
- Two solutions to Cannot load module file xxx.iml
- Firefox: How to Solve “Network Protocol Error” (Two Methods)
- Two dimensional array and pointer to one dimensional array
- Two methods of fixing WPF menu bar after scrolling to the top
- pathspec ‘master’ did not match any files known to git
- Two lines of code to solve your vs flashback problem
- Lookuperror: couldn’t find path to unrear library! two thousand and twenty
- Error response from daemon: failed to parse mydockerfile-centos: ENV must have two arguments
- In pandas, dataframe and np.array The mutual transformation between the two
- Altium designer 18: activeroute failed to complete any routing
- Disk error press any key to restart solution
- Error: 0x800703fa – Press any key to continue
- pathspec commit did not match any files known to git