行人越界检测 越线 越界区域 多边形IOU越界判断

行人越界判断
越界判断方式:(1)bbox中心点越界(或自定义)(2)交并比IoU判断
越界类型:(1)越线 (2)越界区域
1.越线判断
bbox中心点xc、yc判断是否越线

import cv2
def is_passing_line(point, polyline):  # 在直线上方,status =1   下方,status =-1 
	status = 1   
	poly_y = ((polyline[1][1] - polyline[0][1]) * (point[0] - polyline[0][0])) / (polyline[1][0] - polyline[0][0]) +  polyline[0][1] # 点映射在直线的高度
	if point[1] > poly_y:
		status = -1
	return status
	
pt = [xc,yc]
lines = [[x1,y1],[x2,y2]]
cv2.line(img,(x1,y1),(x2,y2),(255,0,0),2)
cv2.circle(img, pt, 4, (0,0,255), -1)	
status = is_passing_line(pt,lines)
cv2.imwrite('color_line.jpg',img)
print('status up 1 down -1:',status)

2.越界判断
bbox中心点xc、yc判断是否在多边形区域内

import cv2
import numpy as np
import matplotlib.path as mplPath

pt=[1067,382] #bbox 中心点xc,yc
POLYGON = np.array([[870, 163],[1022, 180],[1060, 415],[954, 713],[727, 658],])
imgpath = 'demo.jpg'
img = cv2.imread(imgpath)
cv2.polylines(img, [POLYGON], True, (144, 238, 144), 2)
cv2.circle(img, pt, 4, (0,0,255), -1)
is_in = mplPath.Path(POLYGON).contains_point(pt)
cv2.imwrite('color.jpg',img)
print('is_in:',is_in) # True即在多边形区域内

3.矩形IoU越界判断
二者皆为矩形

def iou(box1, box2):                                           
    '''                                                        
    box: [ 0,  1,  2,  3]                                      
    box: [x1, y1, x2, y2],依次为左上右下坐标                  
    '''                                                        
    w = max(0, min(box1[2], box2[2]) - max(box1[0], box2[0]))  
    h = max(0, min(box1[3], box2[3]) - max(box1[1], box2[1]))  
    Inter = w * h                                              
    S_box1 = (box1[2]-box1[0]) * (box1[3]-box1[1])             
    S_box2 = (box2[2]-box2[0]) * (box2[3]-box2[1])             
    Union = S_box1 + S_box2 - Inter                            
    iou = Inter / Union                                        
    return iou                                                 
box1 = [100, 100, 200, 200]                                    
box2 = [100, 150, 200, 250]                                    
IoU = iou(box1, box2)                                          
print(IoU)

4.多边形IoU越界判断
支持任意多边形二者之间IoU计算

from shapely.geometry import Polygon                               
                                                                                  
poly1 = [(100, 100),(50,150), (100, 200), (200, 200), (200, 100)]   #逆时针顶点坐标
poly2 = [(100, 150), (100, 250), (200, 250), (200, 150)]           
                                                                   
# 创建多边形                                                       
poly1 = Polygon(poly1)                                             
poly2 = Polygon(poly2)                                             
                                                                   
# 计算交集和并集                                                   
intersection = poly1.intersection(poly2)                           
union = poly1.union(poly2)                                         
                                                                   
# 计算IoU                                                          
iou = intersection.area / union.area                               
print(f"IoU: {iou}") 
上一篇:LVS集群的DR模式


下一篇:外贸展示型网站设计