1. Nicholas Piël
  2. Projects

Source

Projects / PersonDetection / perdet / utils / geometry.py

# encoding: utf-8
# Author: Nicholas Piël
#
# GeoMetry file containing conveniance functions


def intersection(boxa, boxb):
    ''' Returns area of overlap
    '''
    xa,ya,Xa,Ya = boxa
    xb,yb,Xb,Yb = boxb
    dx = min(Xa, Xb) - max(xa, xb)
    if not dx > 0: return 0
    dy = min(Ya, Yb) - max(ya, yb)
    if not dy > 0: return 0
    return dx*dy
    


def surface(box):
    ''' Calculate the surface of a box
    '''
    x,y,X,Y = box
    return (X-x)*(Y-y)
    

def inside(boxa, boxb):
    ''' Return true when boxa is totally inside boxb
    '''
    xa,ya,Xa,Ya = boxa
    xb,yb,Xb,Yb = boxb
    return xa > xb and Xa < Xb and ya > yb and Ya < Yb
    


def overlap_ratio(boxa, boxb):
    ''' This function returns a tuple: 
        namely its smallestbox and the overlapratio
    '''
    overlap = intersection(boxa, boxb)
    if not overlap: return None, 0
    surfa = surface(boxa)
    surfb = surface(boxb)
    if surfa < surfb:
        return boxa, (float(overlap) / surfa)
    else:
        return boxb, (float(overlap) / surfb) 


####### Manipulations

def multiply(box, factor):
    ''' Multiply the box by a certain factor 
        seen from the center of the box
    '''
    x,y,X,Y = box
    dx = (X-x)/2.0 * factor
    dy = (Y-y)/2.0 * factor
    return x-dx, y-dy, X+dx, Y+dy


def create_body(facebox, width=3, height=5, shift=0):
    ''' Given a face box create a bady box
    '''
    x,y,X,Y = facebox
    dx = (X-x)/2.0*width
    dy = (Y-y)/2.0
    return x-dx, y+dy*shift, X+dx, Y+dy*height