def findfits(X,gmods,img,k,m): wdth = len(img[0]) hgth = len(img) X = np.append(np.array(X[:len(X)/2])*wdth,np.array(X[len(X)/2:])*hgth) X = np.trunc(X).astype(int) Xn = zip(X[:len(X)/2],X[len(X)/2:]) fits = np.zeros(shape=(len(X))) for i in range(len(Xn)): co,s = gmods[i] # get profile to be sampled btwn current point i and its neighbour # also returns normal perpendicular to profile profile, n = prfl.get(Xn,i) # During search we sample a profile m pixels either side of the current point # 2(m − k) + 1 possible positions candidates = prfl.getpositionsalong(profile[0],n,(m-k)) # We then test the quality of fit of the corresponding grey-level model # at each of the 2(m − k) + 1 possible positions along the sample (Figure 10) and # choose the one which gives the best match (lowest value of f(gs)). # where f(gs) = mahalanobis distance dmin, best = np.inf, 0 for j in range(2*(m-k)+1): # get distance from candidate to greymodel (co,s) d = prfl.greylvldistancemetric(candidates[j], s, co, k ,n,img) # assign new best fit if d < dmin: dmin = d best = j fits[i], fits[len(fits)/2+i] = candidates[best,0]/float(wdth), candidates[best,1]/float(hgth) return fits
def autoinit(mean, marks, trainimgs, testimg, k, m, gmod): imgw, imgh = testimg.shape[1],testimg.shape[0] # get appearance model a_mean,mean_shape = appearance(mean,marks,trainimgs) # get TL model (mean and standard dev) for top-left corners of landmarks TL, TLmin, TLmax = TLmodel(marks,trainimgs, imgw, imgh) # get radiograph as gradient image (also equalizes for better edges) gradimg = rg.togradient(testimg) # xmean, ymean = mean[:len(mean)/2], mean[len(mean)/2:] min_x,min_y = np.min(xmean),np.min(ymean) initshape = zip([(x-min_x)*imgw for x in xmean],[(y-min_y)*imgh for y in ymean]) min_x,min_y = np.min(mean[:len(mean)/2]),np.min(mean[len(mean)/2:]) pts = zip(mean[:len(mean)/2],mean[len(mean)/2:]) pts = [((point[0]-min_x),(point[1]-min_y)) for point in pts] initshape = [(int(y[0]*imgw),int(y[1]*imgh)) for y in pts] # set of angles and scales to nudge new shapes in angles = np.array(range(-8,9,1))/2.0 scales = range(8,-1,-1) # to start, use template equal to bounding box around mean shape of appearance model template = a_mean.reshape((mean_shape[0],mean_shape[1])) temph, tempw = template.shape chosenangle = 0 chosenscalex = 0 chosenscaley = 0 chosenlocation = (0,0) dmin = np.inf # scale shape over select amount of scaling factors- x axis for sx in [0.05*s+0.1 for s in scales]: xtempl = cv2.resize(template,(int(tempw*sx),temph)) # scale shape over select amount of scaling factors - y axis for sy in [0.05*s+0.1 for s in scales]: X,Y,dTL = gettemplateparam(xtempl,sy, TLmin, TLmax, TL, gradimg) # rotate shape over select amount of rotations, find best fit for a in angles: angle = a*0.1 # get a new shape by 'nudging' the current shape # = scale, rotate, translate nudgedshape = nudgeshape(sx,sy,angle,X,Y,initshape,1.0,1.0) points = np.array(zip(*nudgedshape)).flatten().tolist() d = 0 if not(np.max(points[:len(points)/2])+k > imgw-1 or np.max(points[len(points)/2:])+k > imgh-1): for l in range(len(nudgedshape)): # get model, gradients, normal and profile for point l profile, n = prfl.get(nudgedshape,l) cov,mean = gmod[l] # Get mahalanobis distance from profile[0]'s greylvl model to provided greymodel (co,mean) d = d + prfl.greylvldistancemetric(profile[0], mean, cov, k ,n,testimg) # calculate full metric d = d/len(nudgedshape) dtot = d+0.01*np.linalg.norm(dTL) # update if dtot < dmin: dmin = dtot chosenlocation = X,Y chosenscalex = sx chosenscaley = sy chosenangle = angle print "Estimate found:" print "Location = ("+ ",".join([str(c).split(".")[0] for c in chosenlocation]) + ")" print "Angle = " + str(chosenangle) print "Scale X = " + str(chosenscalex) print "Scale Y = " + str(chosenscaley) # nudge initial shape by chosen angle, scale and position result = nudgeshape(chosenscalex,chosenscaley,chosenangle,chosenlocation[0],chosenlocation[1],initshape,(1.0/float(imgw)),(1.0/float(imgh))) return [el for point in zip(*result) for el in point]
# Program to get total number of badges and number of points of specific subject from teamtreehouse # Import statements import profile # Module to get profile data import sys # Module for reading in command line args from itertools import izip # Module to help loop through list two at a time # Functions # Function to turn (1, 2, 3, 4) to ((1, 2), (3, 4)) def pairwise(iterable): "s -> (s0,s1), (s2,s3), (s4, s5), ..." a = iter(iterable) return izip(a, a) # Global Variables people = [] # List of people to get data from # Cycle through every two arguments and append to people list for name, subject in pairwise(sys.argv[1:]): people.append({"username": name, "subject": subject}) # Cycle through people and get and output the profile data for person in people: profile.get(person["username"], person["subject"])
def test_profile(self): self.assertFalse(profile.get(self.session, u"/agasper") is None)