00001 import numpy as np
00002 import util as ut
00003 import prob as pr
00004 import itertools as it
00005 import types
00006 import opencv.cv as cv
00007
00008 class DetectionAppearance:
00009 def __init__(self, cov):
00010 """
00011 Appearance model for tracking when there is a detector
00012 available that gives 2d poses of objects.
00013 cov - uncertainty of measurements
00014 measurement - 2x1 matrix
00015 state - 2x1 matrix
00016 """
00017 self.cov = cov
00018
00019
00020 def weight(self, measurement, particle):
00021 """
00022 measurement - 2D column numpy.matrix
00023 particle - Pose2D
00024 """
00025 gauss = pr.Gaussian(m = measurement, v = self.cov)
00026 f = gauss.pdf_mat()
00027 w = f(particle)
00028 return w[0]
00029
00030 def weight_partial(self, measurement):
00031 gauss = pr.Gaussian(m = measurement, v = self.cov)
00032 f = gauss.pdf_mat()
00033 def weightp(particle):
00034 w = f(particle)
00035 return w[0]
00036 return weightp
00037
00038 def weight_set(self, measurement, particle_set):
00039 pos_mat = ut.list_mat_to_mat(particle_set, axis=1)
00040 gauss = pr.Gaussian(m = measurement, v = self.cov)
00041 f = gauss.pdf_mat()
00042 weight_mat = f(pos_mat)
00043
00044 def pair_weights(tup):
00045 part, idx = tup
00046 return (part, weight_mat[idx])
00047 return map(pair_weights, it.izip(particle_set, xrange(len(particle_set))))
00048
00049
00050
00051 def draw_weighted_2D(display, max_weight, particles):
00052 for p in particles:
00053 if type(p) is types.TupleType:
00054 rpos, weight = p
00055 else:
00056 rpos = p
00057
00058 pos = display.to_screen(rpos)
00059
00060 if type(p) is types.TupleType:
00061 color = round(255.0 * (weight/max_weight))
00062 cv.cvCircle(display.buffer, cv.cvPoint((int) (pos[0,0]), (int) (pos[1,0])),
00063 3, cv.cvScalar(255, 255-color, 255), cv.CV_FILLED, cv.CV_AA)
00064 cv.cvCircle(display.buffer, cv.cvPoint((int) (pos[0,0]), (int) (pos[1,0])),
00065 3, cv.cvScalar(200, 200, 200), 1, cv.CV_AA)
00066 else:
00067 cv.cvCircle(display.buffer, cv.cvPoint((int) (pos[0,0]), (int) (pos[1,0])),
00068 2, cv.cvScalar(150, 150, 150), cv.CV_FILLED, cv.CV_AA)
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096