Example #1
0
def whiten(In):
	dt = numpy.dtype('f8')
	MN = MA(In.shape).item(0)
	a = numpy.mean(In,axis=0)
	b = numpy.std(In,axis=0) + 1e-14
	out = numpy.divide( ( In - (MA(numpy.ones((MN,1)),dt)*a)), (MA(numpy.ones((MN,1)),dt) *b) )
	return out,a,b
Example #2
0
def affparaminv(est):
    #!!!pay attention
    dt = np.dtype('f8')
    q = MA(np.zeros(2,3),dt)
    q = linalg.pinv(MA([[est[0,2],est[0,3]],[est[0,4],est[0,5]]])) * MA([[-est[0,0],1.0,0.0],[-est[0,1],0,1.0]])
    q=q.flatten(1)
    return MA([[q[0,0],q[0,1],q[0,2],q[0,4],q[0,3],q[0,5]]])
Example #3
0
def APGLASSOup(b,A,para):
	dt = numpy.dtype('f8')
	colDim = MA(A.shape).item(0)
	xPrev = MA(numpy.zeros((colDim,1)),dt)
	x= MA(numpy.zeros((colDim,1)),dt)
	tPrev =1
	t=1
	lambd = MA(para.Lambda)
	Lip = para.Lip
	maxit = para.Maxit
	nT = para.nT
	temp_lambda = MA(numpy.zeros((colDim,1)),dt)
	temp_lambda[0:nT,0] = lambd[0,0]
	temp_lambda[:,-1][0,0] = lambd[0,0]
	for i in range(maxit):
		temp_t = (tPrev-1)/t
		temp_y = (1+temp_t)*x - temp_t *xPrev;
		temp_lambda[nT:-1,0] = lambd[0,2]*temp_y[nT:-1,0]
		temp_y = temp_y - (A*temp_y-b + temp_lambda)/Lip
		xPrev=deepcopy(x) # Python by default copies by reference so changed to copy by value
		for j in range(nT):
			x[j,0] = max(temp_y[j,0].max(),0)
		x[-1,0] = max(temp_y[-1,0],0)
		y_input = temp_y[nT:-1,0]
		x[nT:-1,0] = softresh_c(y_input,lambd[0,1]/Lip)
		tPrev = t
		t = (1+numpy.sqrt(1+4*t*t))/2
		
	return x
Example #4
0
def crop_candidates(img_frame,curr_samples,template_size):
	dt = numpy.dtype('f8')
	nsamples = MA(curr_samples.shape).item(0)
	c = numpy.prod(template_size)
	gly_inrange = MA(numpy.zeros(nsamples),dt)
	gly_crop = MA(numpy.zeros((c,nsamples)),dt)
	for i in range(nsamples):
		curr_afnv = curr_samples[i,:]
		img_cut,gly_inrange[0,i] = IMGaffine_c(img_frame,curr_afnv,template_size)
		gly_crop[:,i] = MA(img_cut).flatten(1).T
	return gly_crop,gly_inrange
 def __init__(self):
     self.lambd = MA([[0.2, 0.001, 2]])
     self.angle_threshold = 40
     self.Lip = 8
     self.Maxit = 5
     self.nT = 50
     # number of templates for the sparse representation
     self.rel_std_afnv = MA([[0.003, 0.03, 0.003, 0.03, 1, 1]])
     # diviation of the sampling of particle filter
     self.n_sample = 100
     # No of particles
     self.sz_T = MA([[12, 15]])
Example #6
0
def drawbox(est,sz):
    temp = np.zeros(6)
    #import pdb;pdb.set_trace()
    temp[:] = est[0,:]
    est = temp
    #temp[:] = est[0,:]
    M = np.array([[est[0],est[2],est[3]],[est[1],est[4],est[5]]])
    w = sz[0,0]
    h = sz[0,1]
    corners = np.array([[1,-w/2,-h/2],[1,w/2,-h/2],[1,w/2,h/2],[1,-w/2,h/2],[1,-w/2,-h/2]]).T
    corners = MA(M) * MA(corners)
    #import pdb; pdb.set_trace()
    return corners
Example #7
0
def resample(curr_samples, prob, afnv):
    dt = numpy.dtype('f8')
    nsamples = MA(curr_samples.shape).item(0)
    if prob.sum() == 0:
        map_afnv = MA(numpy.ones(nsamples), dt) * afnv
        count = MA(numpy.zeros((prob.shape), dt))
    else:
        prob = prob / (prob.sum())
        count = MA(numpy.ceil(nsamples * prob), int)
        count = count.T
        map_afnv = MA(numpy.zeros((1, 6)), dt)
        for i in range(nsamples):
            for j in range(count[i]):
                map_afnv = MA(
                    numpy.concatenate((map_afnv, curr_samples[i, :]), axis=0),
                    dt)
        K = map_afnv.shape[0]
        map_afnv = map_afnv[1:K, :]
        ns = count.sum()
        if nsamples > ns:
            map_afnv = MA(
                numpy.concatenate(
                    (map_afnv, MA(numpy.ones(
                        ((nsamples - ns), 1)) * afnv, dt)),
                    axis=0), dt)
        map_afnv = map_afnv[0:nsamples, :]
    return map_afnv, count
Example #8
0
def InitTemplates(tsize, numT, img, cpt):
    dt = numpy.dtype('f8')
    p = []
    p.append(cpt)
    for i in range(numT - 1):
        p.append(cpt + MA(numpy.random.randn(2, 3)) * 0.6)
    A = numpy.prod(tsize)
    T = numpy.zeros((A, numT))
    T = MA(T, dt)
    T_norm = MA(numpy.zeros((numT, 1)), dt)
    T_mean = MA(numpy.zeros((numT, 1)), dt)
    T_std = MA(numpy.zeros((numT, 1)), dt)
    for i in range(numT):
        A1, T_norm[i], T_mean[i], T_std[i] = corner2image(img, p[i], tsize)
        T[:, i] = A1[:, 0]
    return T, T_norm, T_mean, T_std
Example #9
0
def affparam2mat(p):
    # Here I use array instead of matrix
    q = np.zeros(p.shape,'f8')
    p = np.array(p)
    temp = q
    sz = p.shape
    temp1 = p
    #import pdb; pdb.set_trace()
    #if len(p.shape) == 1: 
#	temp = np.zeros(1,p.shape[0])
#	temp[0,:] = p
    s = np.array(p[:,2])
    th = np.array(p[:,3])
    r = np.array(p[:,4])
    phi = np.array(p[:,5])
    cth = np.cos(th)
    sth = np.sin(th)
    cph = np.cos(phi)
    sph = np.sin(phi)
    ccc = cth*cph*cph
    ccs = cth*cph*sph
    css = cth*sph*sph
    scc = sth*cph*cph
    scs = sth*cph*sph
    sss = sth*sph*sph
    q[:,0] = np.array(p[:,0])
    q[:,1] = np.array(p[:,1])
    q[:,2] = s*(ccc +scs +r*(css -scs))
    q[:,3] = s*(r*(ccs -scc) -ccs -sss)
    q[:,4] = s*(scc -ccs +r*(ccs +sss))
    q[:,5] = s*(r*(ccc +scs) -scs +css)
    return MA(q)
def search_graph(query, nodes, DS, K):
    dt = numpy.dtype('f8')
    random.seed(100)
    k = nodes.shape[1]
    depth = 0
    flag = 0
    parent_id = randd(1, nodes.shape[0], 1)
    # Check this
    visited = 1
    while 1:
        parent_vec = MA(DS[parent_id, 0:], dt)  # parent node
        child_ids = nodes[parent_id, 0:]
        Val = numpy.zeros((len(child_ids), DS.shape[1]), dt)
        I = 0
        while I < len(child_ids):
            Val[I, 0:] = DS[child_ids[I], 0:]
            I += 1
        (nn1_ind, nn1_dist) = knnsearch(query, Val, K)
        visited = visited + k
        if (parent_dist <= nn1_dist):
            flag = 1
            break
        parent_id = child_ids(nn1_ind)
        depth = depth + 1
    if flag == 1:
        nn_id = parent_id
        nn_dist = parent_dist
    else:
        nn_id = -1
        nn_dist = -1

    return nn_id, nn_dist, visited
Example #11
0
    def __init__(self, n_samples, warp_generator, img, pts, initial_warp, res):
        self.nodes = None
        self.resx = res[0]
        self.resy = res[1]
        self.gnn = True
        self.indx = []
        n_points = pts.shape[1]
        print "Sampling Warps..."
        self.warps = [np.asmatrix(np.eye(3))
                      ] + [warp_generator() for i in xrange(n_samples - 1)]
        print "Sampling Images..."
        self.images = np.empty((n_points, n_samples))
        for i, w in enumerate(self.warps):
            self.images[:, i] = sample_and_normalize(img, pts,
                                                     initial_warp * w.I)
        print('Graph based Nearest Neighbour')
        print('------------------------------')
        if self.gnn == True:
            #self.flann = pyflann.FLANN()
            #print(self.images.shape)
            #self.flann.build_index(self.images.T, algorithm='kdtree', trees=10)
            self.images = MA(self.images, 'f8')
            self.nodes = build_graph(self.images.T, 40)

        else:
            desc = self.list2array(self.pixel2sift(self.images))
            # --- Building Flann Index --- #
            self.flann = pyflann.FLANN()
        print "Done!"
Example #12
0
def labels(x):
	(X,T) = scipy.ndimage.label(x)
	areastat = MA(numpy.zeros((T,2)))
	for j in range(1,T+1):
		count = replacement(X,j)
		areastat[j-1,:]= [j,count];
	
	return areastat,T
Example #13
0
def corners2affine(corners_in, size_out):
    dt = numpy.dtype('f8')
    rows = size_out.item(0)
    cols = size_out.item(1)
    inp = numpy.insert(corners_in, 2, numpy.matrix([[1, 1, 1]], dt), 0)
    outp = MA([[1, rows, 1], [1, 1, cols], [1, 1, 1]], dt)
    R = inp * outp.I
    afnv = MA([[
        R.item(0, 0),
        R.item(0, 1),
        R.item(1, 0),
        R.item(1, 1),
        R.item(0, 2),
        R.item(1, 2)
    ]], dt)
    afnv_obj = {'R': R, 'afnv': afnv, 'size_out': size_out}
    return afnv_obj
Example #14
0
def images_angle(I1,I2):
	I1v = I1.flatten(1)
	col = MA(I1v.shape).item(1);
	I2v = I2.flatten(1)
	I1vn = I1v/(numpy.sqrt(numpy.sum(numpy.multiply(I1v,I1v))) + 1e-14);
	I2vn = I2v/(numpy.sqrt(numpy.sum(numpy.multiply(I2v,I2v)))+ 1e-14);
	angle = math.degrees(math.acos(numpy.sum(numpy.multiply(I1vn,I2vn))))
	return angle
Example #15
0
def aff2image(aff_maps, T_sz):
    dt = numpy.dtype('f8')
    # % height and width of template
    r = T_sz[0, 0]  # MA(T_sz.shape).item(0);
    c = T_sz[0, 1]  # MA(T_sz.shape).item(1);
    # number of affine results
    n = MA(aff_maps.shape).item(1)
    boxes = numpy.zeros((8, n), dt)
    for ii in range(n):
        aff = aff_maps[:, ii]
        R = MA([[aff.item(0), aff.item(1),
                 aff.item(4)], [aff.item(2),
                                aff.item(3),
                                aff.item(5)]])
        P = MA([[1, r, 1, r], [1, 1, c, c], [1, 1, 1, 1]])
        Q = R * P
        boxes[:, ii] = Q.flatten(1)
    return boxes
Example #16
0
def binary(img1, level):
	img1 = MA(img1)
	tempIm = img1.flatten(1)
	for i in range(tempIm.shape[1]):
		if tempIm[0,i] < level:
			tempIm[0,i] = 0;
		else:
			tempIm[0,i] = 1;
	tempIm = (numpy.reshape(tempIm,(img1.shape[1],img1.shape[0]))).T
	return tempIm
Example #17
0
def corner2image(img, p, tsize):
    dt3 = numpy.dtype('float64')
    afnv_obj = corners2affine(p, tsize)
    map_afnv = afnv_obj['afnv']
    img = MA((img), dt3)
    img_map, blah = IMGaffine_c(img, map_afnv, tsize)
    crop, crop_mean, crop_std = whiten(
        img_map.flatten(1).T)  # reshape different from matlab
    crop_norm = linalg.norm(crop)
    crop = crop / crop_norm
    return crop, crop_norm, crop_mean, crop_std
def labels(x):
    (X, T) = scipy.ndimage.label(x)
    print X
    areastat = MA(numpy.zeros((T, 2)))
    for j in range(1, T + 1):
        # import pdb;pdb.set_trace()
        count = relacement(X, j)
        # import pdb;pdb.set_trace()
        areastat[j - 1, :] = [j, count]

    return areastat
def binary(img1, level):
    img1 = MA(img1)
    #import pdb;pdb.set_trace()
    tempIm = img1.flatten(1)
    for i in range(tempIm.shape[1]):
        if tempIm[0, i] < level:
            tempIm[0, i] = 0
        else:
            tempIm[0, i] = 1
    tempIm = (numpy.reshape(tempIm, (img1.shape[1], img.shape[0]))).T
    print tempIm
    return tempIm
Example #20
0
def  search_graph(query, nodes, DS, K):

    ''' Authors: Ankush Roy ([email protected])
                 Kiana Hajebi ([email protected])
    '''
    dt = numpy.dtype('f8')
    nodes = MA(nodes,dt)
    query = MA(query,dt)
    DS = MA(DS, dt)
    random.seed(100)
    k = nodes.shape[1]
    depth = 0
    flag  = 0
    parent_id = randd(1, nodes.shape[0], 1); # Check this
    visited = 1;
    while 1:
        parent_vec = MA(DS[int(parent_id),0:],dt)  # parent node
        parent_dist = numpy.sqrt((query - parent_vec) * numpy.transpose(query - parent_vec))
        child_ids = nodes[int(parent_id),0:];
        Val = numpy.zeros((child_ids.shape[1],DS.shape[1]),dt)
        I = 0 
        while I < child_ids.shape[1]:
            Val[I,0:] = DS[int(child_ids[0,I]),0:]
            I += 1
        Val = MA(Val)
        (nn1_ind, nn1_dist) = knnsearch(query,Val, K)
        visited = visited + k
        if (parent_dist <= nn1_dist):
            flag=1
            break
        parent_id = child_ids[0,nn1_ind]
        depth = depth+1
    if flag == 1:
        nn_id = parent_id
        nn_dist  = parent_dist
    else:
        nn_id = - 1
        nn_dist = -1
    return nn_id, nn_dist, visited
Example #21
0
def draw_sample(mean_afnv,std_afnv):
	dt = numpy.dtype('f8')
	mean_afnv = MA((mean_afnv),dt);
	nsamples = MA(mean_afnv.shape).item(0)
	MV_LEN=6
	mean_afnv[:,0] = numpy.log(mean_afnv[:,0])
	mean_afnv[:,3] = numpy.log(mean_afnv[:,3])
	outs= MA(numpy.zeros((nsamples,MV_LEN)),dt)
	flatdiagonal = MA((numpy.diagflat(std_afnv)),dt);
	outs[:,0:MV_LEN] = MA(numpy.random.randn(nsamples,MV_LEN),dt) * flatdiagonal + mean_afnv
	outs[:,0] = MA(numpy.exp(outs[:,0]),dt)
	outs[:,3] = MA(numpy.exp(outs[:,3]),dt)
	return outs
 def __init__(self):
     self.lambd = MA([[0.2, 0.001, 10]])
     self.angle_threshold = 50
     self.Lip = 8
     self.Maxit = 5
     self.nT = 10
     # number of templates for the sparse representation
     self.rel_std_afnv = MA([[0.005, 0.003, 0.005, 0.003, 1, 1]])
     # diviation of the sampling of particle filter
     self.n_sample = 100
     # No of particles
     self.sz_T = MA([[12, 15]])
     # Reshape each image so that they have the same image space representation
     self.init_pos = MA([[int(pts[0, 1]),
                          int(pts[1, 1]),
                          int(pts[3, 1])],
                         [int(pts[0, 0]),
                          int(pts[1, 0]),
                          int(pts[3, 0])]])
     self.path = 'G:/UofA/Thesis/#Code/Datasets/Human/nl_bookI_s3/img'
     self.results = 'ResultTracking'
     if not os.path.exists(self.results):
         os.makedirs(self.results)
     self.noZeros = '5'
Example #23
0
def resample2(curr_samples,prob):
        dt = numpy.dtype('f8')
        nsamples = MA(curr_samples.shape).item(0)
        afnv = 0
        #pdb.set_trace()
        if prob.sum() ==0 :
                #import pdb; pdb.set_trace()
                map_afnv = MA(numpy.ones(nsamples),dt).T*afnv
                count = MA(numpy.zeros((prob.shape),dt))
        else:
                prob = prob/prob.sum()
                N = nsamples
                Ninv = 1 / float(N)
                map_afnv = MA(numpy.zeros((N,6)),dt)
                c = pylab.cumsum(prob)
                u = pylab.rand()*Ninv
                i = 0
                #pdb.set_trace()
                for j1 in range(N):
                        uj = u + Ninv*j1
                        while uj > c[i]:
                                i += 1
                        map_afnv[j1,:] = curr_samples[i,:]
                return map_afnv
def build_graph(X, k):
    ''' Build a Connected graph using k neighbours
        Author: Ankush Roy ([email protected])
                Kiana Hajebi ([email protected])
    '''
    dt = numpy.dtype('f8')
    f = []
    nodes = numpy.zeros((X.shape[0], k), dt)

    for i in range(X.shape[0]):
        query = MA(X[(i - 1), 0:], dt)
        (nns_inds, nns_dists) = knnsearch(query, X, k + 1)
        I = 0
        f = []
        while I < len(nns_inds):
            if nns_inds[I] == i - 1:
                nns_inds.remove(i - 1)
                nodes[i - 1, 0:] = nns_inds
                break
            else:
                I += 1
    return nodes
Example #25
0
def build_graph(X, k):
    dt = numpy.dtype('f8')
    f = []
    nodes = numpy.zeros((X.shape[1], k), dt)
    i = 0
    while i <= X.shape[0]:
        #        print i
        query = MA(X[(i - 1), 0:], dt)
        (nns_inds, nns_dists) = knnsearch(query, X, k + 1)
        I = 0
        f = []
        #        import pdb;pdb.set_trace()
        while I < len(nns_inds):
            if nns_inds[I] == i - 1:
                nns_inds.remove(i - 1)
                print i
                nodes[i - 1, 0:] = nns_inds
                break
            else:
                I += 1
        i += 1
    print nodes
    return nodes
Example #26
0
        query = MA(X[(i - 1), 0:], dt)
        (nns_inds, nns_dists) = knnsearch(query, X, k + 1)
        I = 0
        f = []
        #        import pdb;pdb.set_trace()
        while I < len(nns_inds):
            if nns_inds[I] == i - 1:
                nns_inds.remove(i - 1)
                print i
                nodes[i - 1, 0:] = nns_inds
                break
            else:
                I += 1
        i += 1
    print nodes
    return nodes


if __name__ == '__main__':
    dt = numpy.dtype('f8')
    X = [range(10)]
    print X
    for i in range(10):
        Vect = [random.randint(0, 100) for r in range(10)]
        X = numpy.vstack([X, Vect])
    X = numpy.delete(X, (0), axis=0)
    X = MA(X, dt)
    K = 2
    print X
    build_graph(X, K)
Example #27
0
def affparam2geom(est):
    # !!!pay attention
    dt = np.dtype('f8')
    A = MA([[est[0,2],est[0,3]],[est[0,4],est[0,5]]])
    U,S,V = linalg.svd(A,full_matrices=True)
    temp = MA(np.zeros((2,2),dt))
    #temp[0,0] = S[0]
    #temp[1,1] = S[1]
    #S = temp
    #import pdb; pdb.set_trace()
    if(linalg.det(U) < 0):
        U = U[:,range(1,-1,-1)]
        V = V[:,range(1,-1,-1)]
        S = S[:,range(1,-1,-1)]
	temp[1,1] = S[1]
	temp[0,0] = S[0]
	S = temp
    else:
        temp[1,1] = S[0]
        temp[0,0] = S[1]
	S = temp	
    #import pdb; pdb.set_trace()
    q = MA(np.zeros((1,6)),dt)
    q[0,0] = est[0,0]
    q[0,1] = est[0,1]
    q[0,3] = np.arctan2(U[1,0]*V[0,0]+U[1,1]*V[0,1],U[0,0]*V[0,0]+U[0,1]*V[0,1])
    phi = np.arctan2(V[0,1],V[0,0])
    if phi <= -np.pi/2:
        c = np.cos(-np.pi/2)
        s = np.sin(-np.pi/2)
        R = MA([[c,-s],[s,c]])
        V = MA(V) * MA(R)
        S = R.T*MA(S)*R
    if phi > np.pi/2:
        c = np.cos(np.pi/2)
        s = np.sin(np.pi/2)
        R = MA([[c,-s],[s,c]])
        V = MA(V)*MA(R)
        S = R.T*MA(S)*R
    #import pdb; pdb.set_trace()
    q[0,2] = S[0,0]
    q[0,4] = S[1,1]/S[0,0]
    q[0,5] = np.arctan2(V[0,1],V[0,0])
    return q
Example #28
0
def L1TrackingBPR_APGupWebcam(paraT):
	dt = numpy.dtype('f8')
	framename = 'frameAPG';
	dt2 = numpy.dtype('uint8');
	dt3 = numpy.dtype('float64')
	camera =  cv2.VideoCapture(0);
	f,img = camera.read();
#	import pdb;pdb.set_trace()
	plt.imshow(img)
	pts = [];
	while len(pts) < 4:
	  tellme('Select 4 corners with mouse anticlockwise starting with top left')
	  pts = numpy.asarray( plt.ginput(4,timeout=-1) )
	  if len(pts) < 4:
	    tellme('Too few points, starting over')
	    time.sleep(1) # Wait a second
	plt.close()
	init_pos = MA([[int(pts[0,1]),int(pts[1,1]),int(pts[2,1])],[int(pts[0,0]),int(pts[1,0]),int(pts[2,0])]])
	# paraT is a structure.
	## Initialize templates T
	# Generate T from single image
	init_pos = init_pos;
	n_sample=paraT.n_sample;
	sz_T=paraT.sz_T;
	rel_std_afnv = paraT.rel_std_afnv;
	nT=paraT.nT;
	t = 0;
	# generate the initial templates for the 1st frame. The image has to be in GrayScale
	#Movie = cv2.VideoCapture("Videos/RobotNewSetup.avi")
	#cv2.namedWindow("input")
	#f,img = Movie.read()

	#camera1 =  cv2.VideoCapture(0);
	f,img = camera.read();
#	img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY);
	r1, g1, b1 = img[:,:,0], img[:,:,1], img[:,:,2]
	img = 0.2989*r1 + 0.5870 * g1 + 0.1140 * b1;
	img = MA(img);
	(T,T_norm,T_mean,T_std) = InitTemplates(sz_T,nT,img,init_pos);
	#print T.shape, T_norm.shape,T_mean.shape,T_std.shape
	norms = numpy.multiply(T_norm,T_std); # %template norms
	occlusionNf = 0;
	# L1 function settings
	angle_threshold = paraT.angle_threshold
	#print sz_T.shape
	# import pdb;pdb.set_trace()
	dim_T	= sz_T[0,0]*sz_T[0,1];	# number of elements in one template, sz_T(1)*sz_T(2)=12x15 = 180
	A = MA(numpy.concatenate((T,numpy.matrix(numpy.identity(dim_T))),axis = 1)) # data matrix is composed of T, positive trivial T.
	alpha = 50;# this parameter is used in the calculation of the likelihood of particle filter
	(aff_obj) = corners2affine(init_pos, sz_T); # get affine transformation parameters from the corner points in the first frame
	map_aff = aff_obj['afnv'];
	aff_samples = numpy.dot(numpy.ones((n_sample,1),dt),map_aff);
	T_id	= -numpy.arange(nT);	# % template IDs, for debugging
	fixT = T[:,0]/nT; #  first template is used as a fixed template  CHECK THIS
	# Temaplate Matrix
	Temp = numpy.concatenate((A, fixT), axis=1);
	Dict = numpy.dot(Temp.T,Temp);
	temp1 = numpy.concatenate((T,fixT),axis=1);
	Temp1 = temp1*numpy.linalg.pinv(temp1);
# % Tracking

# % initialization
# nframes = no of frames to be tracked
	nframes = 1340; # pass this as an argument or keyboard interrupt
	temp1 = numpy.concatenate((A,fixT),axis = 1); 
	colDim = MA(temp1.shape).item(1)
	Coeff = numpy.zeros((colDim,nframes),dt);
	count = numpy.zeros((nframes,1),dt);
	
	param = para1(paraT)
	while True:
		start_time= time.time();
		seq = '%05d' % t	
#		filename = '/home/ankush/Desktop/CleanCode/PythonTracker/Videos/Images/cliffbar/imgs/img'+str(seq)+'.png'
		f,img1 = camera.read();
		#		f,img1 = Movie.read() # After some minutes all frames returnes are empty and f is false
		t = t+1;
		print 'Frame number: (%f) \n'% (t);
		r1, g1, b1 = img1[:,:,0], img1[:,:,1], img1[:,:,2]
		img = 0.2989*r1 + 0.5870 * g1 + 0.1140 * b1;
		# Draw transformation samples from a Gaussian distribution
		temp_map_aff = numpy.sum(numpy.multiply(map_aff[0,0:4],map_aff[0,0:4]))/2;
		sc = numpy.sqrt(temp_map_aff);
		std_aff	= numpy.multiply(rel_std_afnv,MA([[1, sc, sc, 1, sc, sc]]));
		map_aff	= map_aff + 1e-14;
		(aff_samples) = draw_sample(aff_samples, std_aff); # draw transformation samples from a Gaussian distribution

		(Y, Y_inrange) = crop_candidates(img, aff_samples[:,0:6], sz_T);

		if numpy.sum(Y_inrange==0) == n_sample:
			print 'Target is out of the frame!\n';
		(Y,Y_crop_mean,Y_crop_std) = whiten(Y);	 # zero-mean-unit-variance
		(Y, Y_crop_norm) = normalizeTemplates(Y); # norm one
		#%-L1-LS for each candidate target
		eta_max	= float("-inf");
		q = numpy.zeros((n_sample,1),dt); #  % minimal error bound initialization
		# % first stage L2-norm bounding
		for j in range(n_sample):
			cond1=Y_inrange[0,j]
			temp_abs = numpy.absolute(Y[:,j])
			cond2 = numpy.sum(temp_abs)
			if cond1 ==0 and cond2==0:
				continue
			# L2 norm bounding
			temp_x_norm = Y[:,j]-Temp1*Y[:,j]
			q[j,0] = numpy.linalg.norm(temp_x_norm);
			q[j,0] = numpy.exp(-alpha*(q[j,0]*q[j,0]));
			
#-------------------------------------------------------------------------------------------------
		# sort samples according to descend order of q
		(qtemp1,indqtemp) = des_sort(q.T);
		q = qtemp1.T;
		indq = indqtemp.T;
    	#second stage
		p= numpy.zeros((n_sample),dt); #observation likelihood initialization
		n = 0;
		tau = 0;
		while (n<n_sample) and (q[n]>=tau):
			APG_arg1 = (Temp.T*Y[:,indq[n]])
			(c) = APGLASSOup(APG_arg1,Dict,param);
#			(c) = APGLASSOup_c(APG_arg1,Dict,param.Lambda, param.Lip, param.Maxit, param.nT)
			c = MA(c);
			Ele1=numpy.concatenate((A[:,0:nT], fixT),axis = 1);
			Ele2=numpy.concatenate((c[0:nT], c[-1]), axis =0 );
			D_s = (Y[:,indq[n-1]] - Ele1*Ele2); #reconstruction error
			D_s = numpy.multiply(D_s,D_s); #reconstruction error
			p[indq[n]] = numpy.exp(-alpha*(numpy.sum(D_s))); #  probability w.r.t samples
			tau = tau + p[indq[n]]/(2*n_sample-1);# update the threshold
			if(numpy.sum(c[1:nT]) < 0): # remove the inverse intensity patterns
				continue;
			elif (p[indq[n]]>eta_max): #******POssilbe Erro*****
				id_max	= indq[n];
				c_max	= c;
				eta_max = p[indq[n]]
			n = n+1;
			count[t-1] = n;
# resample according to probability
		map_aff = aff_samples[id_max,0:6]; # target transformation parameters with the maximum probability
		a_max	= c_max[0:nT,0];
		(aff_samples, _) = resample(aff_samples,p,map_aff); # resample the samples wrt. the probability
		indA = a_max.argmax();
		min_angle = images_angle(Y[:,id_max],A[:,indA]);
     # -Template update
		occlusionNf = occlusionNf-1;
		level = 0.05;
		Initialparameterlambda = MA(param.Lambda);

		if min_angle > angle_threshold and occlusionNf < 0:
			print ('Update!')
			trivial_coef = (numpy.reshape(c_max[nT:-1,0],(sz_T[0,1],sz_T[0,0]))).T;
			dst1 = MA(numpy.zeros((sz_T[0,0],sz_T[0,1])))
			trivial_coef = binary(trivial_coef,level)
			se = MA([[0,0,0,0,0],[0,0,1,0,0],[0,1,1,1,0],[0,0,1,0,0],[0,0,0,0,0]]);
			se = numpy.array(se.T)
			areastats,T1 = labels(trivial_coef)
			if T1> 0:
				Area = areastats[:,1];
				max_area = Area.max()
			Area_tolerance= 0.25*sz_T[0,0]*sz_T[0,1];
		# Occlusion Detection
			if T1>0 and max_area < numpy.rint(Area_tolerance):
			# find the template to be replaceed
				tempa_max = a_max[0:nT-1,0];
				indW = tempa_max.argmin();
			# insert new template
				T = MA(T);
				T_id = MA(T_id);
				T_mean = MA(T_mean);
				norms = MA(norms);
				T[:,indW] = Y[:,id_max];
				T_mean[indW,0] = Y_crop_mean[0,indW];
				T_id[0,indW] = t-1; # track the replaced template for debugging
				norms[indW,0] = Y_crop_std[0,id_max]*Y_crop_norm[0,id_max];
			
				(T,_) =  normalizeTemplates(T);
				A[:,0:nT] = T;
			
			# Template Matrix
				Temp = MA(numpy.concatenate((A,fixT),axis=1));
				Dict = Temp.T* Temp;
				tempInverse = numpy.concatenate((T,fixT),axis =1);
				Temp1 = tempInverse*numpy.linalg.pinv(tempInverse);
			else:
				occlusion = 5;
			# update L2 regularized  term
				param.Lambda = MA([[Initialparameterlambda[0,0], Initialparameterlambda[0,1] , 0]]);
				
		elif occlusionNf <0:
			param.Lambda = Initialparameterlambda;
		rect = numpy.rint(aff2image(map_aff.T, sz_T));
		inp	= (numpy.reshape(rect,(4,2))).T;
		
		#import pdb;pdb.set_trace()
		#topleft_r = inp[0,0];
		#topleft_c = inp[1,0];
		#botleft_r = inp[0,1];
		#botleft_c = inp[1,1];
		#topright_r = inp[0,2];
		#topright_c = inp[1,2];
		#botright_r = inp[0,3];
		#botright_c = inp[1,3];
		position = MA([[int(inp[1,0]),int(inp[0,0]),int(inp[1,3]-inp[1,0]),int(inp[0,3]-inp[0,0])]]);
		fdata = open("ResultTracking/L1.txt", "a")
		fdata.write( str(position) +"\n" )      # str() converts to string
		img = numpy.rint(img);
		point1 = (int(inp[1,0]),int(inp[0,0]));
		point2 =  (int(inp[1,2]),int(inp[0,2]));
		point3 = (int(inp[1,3]),int(inp[0,3]));
		point4 = (int(inp[1,1]),int(inp[0,1]));
		print time.time() - start_time
		#if not f:
		#	break
		try:
			cv2.line(img1, point1, point2, (0,0,255), 2)
			cv2.line(img1, point2, point3, (0,0,255), 2)
			cv2.line(img1, point3, point4, (0,0,255), 2)
			cv2.line(img1, point4, point1, (0,0,255), 2)
			cv2.imshow("preview", img1)
#			cv2.imwrite('ResultTracking/{0:05d}.jpg'.format(t),img1)
		except cv2.error as e:
			print e # print error: (-206) Unrecognized or unsupported array type
		k=cv2.waitKey(5)
		if k==27:
			break
	fdata.close()	
Example #29
0
def warpimg(img,p,sz):
    # arrays
    w = sz[0,0]
    h = sz[0,1]
    result = np.zeros((w*h,p.shape[0]))
    #May have problem    
    x = np.linspace(1-h/2,h/2,h)
    y = np.linspace(1-w/2,w/2,w)
    xv,yv = np.meshgrid(x,y)
    xcoord = np.linspace(1,img.shape[1],img.shape[1])
    ycoord = np.linspace(1,img.shape[0],img.shape[0])
    #f = interpolate.interp2d(xcoord,ycoord,img,kind='cubic')
    #import pdb; pdb.set_trace()
    for i in range(p.shape[0]):
        temp = np.concatenate((np.ones((w*h,1)),xv.reshape((w*h,1),order='F'),yv.reshape((w*h,1),order='F')),axis=1)*MA([[p[i,0],p[i,1]],[p[i,2],p[i,4]],[p[i,3],p[i,5]]])
	#import pdb;pdb.set_trace()
	#tempx = temp[:,0].reshape(h,w).T
	#tempy = temp[:,1].reshape(h,w).T
	#result[:,i] = f(tempx,tempy).reshape(w*h,order='F')
	result[:,i] = sample_region(img, np.array(temp))
    return result
from numpy import matrix as MA


def relacement(X, n):
    X = X.flatten(1)
    count = 0
    for i in range(X.shape[0]):
        if X[i] == n:
            count = count + 1
            X[i] = 0
    return count


def labels(x):
    (X, T) = scipy.ndimage.label(x)
    print X
    areastat = MA(numpy.zeros((T, 2)))
    for j in range(1, T + 1):
        # import pdb;pdb.set_trace()
        count = relacement(X, j)
        # import pdb;pdb.set_trace()
        areastat[j - 1, :] = [j, count]

    return areastat


if __name__ == '__main__':
    x = MA([[0, 0, 1, 0, 0, 1], [0, 0, 1, 1, 0, 1], [0, 0, 0, 0, 0, 0],
            [0, 0, 1, 0, 1, 0], [0, 1, 1, 1, 1, 0]])
    labels(x)