Example #1
0
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
Example #2
0
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]