Exemplo n.º 1
0
def srasm2(enhimgtf, edgeimgs, marks, orient, k, m, modes, maxiter):
    lms,_ = lm.procrustes(marks)
    mean, eigenvecs, eigenvals, lm_reduced = lm.pca_reduce(lms, modes)
    stdvar = np.std(lm_reduced, axis=1)
    
    croppedmarks = np.array([])
    for i in range(13):
        croppedmarks = np.append(croppedmarks,lm.transform_shape(np.reshape(marks[:,i],(marks[:,i].size,1)),-1150,-500,1,0))
    for i in range(13):
        croppedmarks = np.append(croppedmarks,lm.transform_shape(np.reshape(marks[:,i + 13],(marks[:,i + 13].size,1)),-1173,-500,1,0))
    croppedmarks = np.reshape(croppedmarks,(croppedmarks.size / 26,26),'F') 
    smallermarks = np.array([])
    for i in range(26):
        smallermarks = np.append(smallermarks,lm.transform_shape(np.reshape(croppedmarks[:,i],(croppedmarks[:,i].size,1)),0,0,1.0 / 2,0))
    smallermarks = np.reshape(smallermarks,(smallermarks.size / 26,26),'F') 
    
    itx, ity, isc, itheta = init.get_initial_transformation(enhimgtf,mean,orient)
    b = np.zeros((modes,1))
    
    imgtf = pp.apply_sobel(enhimgtf)
    imgtf = cv2.pyrDown(imgtf)
    edgies = np.array(map(lambda x: cv2.pyrDown(x),edgeimgs))
    means, covs = gl.get_statistical_model_new(edgies,smallermarks,k)
    b, ntx, nty, nsc, itheta = asm(imgtf, means, covs, b, itx / 2, ity / 2, isc / 2, itheta, k, m, stdvar, mean, eigenvecs, maxiter, orient)      
    return lm.transform_shape(lm.pca_reconstruct(b,mean,eigenvecs),ntx * 2,nty * 2,nsc * 2,itheta)
Exemplo n.º 2
0
def mrasm(enhimgtf, edgeimgs, marks, orient, k, m, modes, maxiter, resdepth = 1):
    itervec = np.array([0,0])
    lms,_ = lm.procrustes(marks)
    mean, eigenvecs, eigenvals, lm_reduced = lm.pca_reduce(np.copy(lms), 26)
    coverage, modes_needed = lm.get_num_eigenvecs_needed(eigenvals)
    mean, eigenvecs, eigenvals, lm_reduced = lm.pca_reduce(lms, modes_needed)
    stdvar = np.std(lm_reduced, axis=1)
    
    croppedmarks = np.array([])
    for i in range(13):
        croppedmarks = np.append(croppedmarks,lm.transform_shape(np.reshape(marks[:,i],(marks[:,i].size,1)),-1150,-500,1,0))
    for i in range(13):
        croppedmarks = np.append(croppedmarks,lm.transform_shape(np.reshape(marks[:,i + 13],(marks[:,i + 13].size,1)),-1173,-500,1,0))
    croppedmarks = np.reshape(croppedmarks,(croppedmarks.size / 26,26),'F') 
    
    itx, ity, isc, itheta = init.get_initial_transformation(enhimgtf,mean,orient)
    b = np.zeros((modes_needed,1))
    
    print ''
    print 'Starting multi-resolution fitting procedure for orientation ' + str(orient)
    print '--------'
    imgtf = pp.apply_sobel(enhimgtf)
    for i in range(resdepth + 1):
        print 'Step ' + str(i) + ':'
        edgies = edgeimgs
        limgtf =  imgtf
        j = 0
        while resdepth - i > j:
            limgtf = cv2.pyrDown(limgtf)
            edgies = np.array(map(lambda x: cv2.pyrDown(x),edgies))
            smallermarks = np.array([])
            for l in range(26):
                smallermarks = np.append(smallermarks,lm.transform_shape(np.reshape(croppedmarks[:,l],(croppedmarks[:,l].size,1)),0,0,1.0 / 2,0))
            smallermarks = np.reshape(smallermarks,(smallermarks.size / 26,26),'F') 
            j += 1 
        
        means, covs = gl.get_statistical_model_new(edgies,smallermarks,k)
        b, ntx, nty, nsc, itheta, itervec[i] = asm(limgtf, means, covs, b, float(itx) / math.pow(2.0,(resdepth-i)), float(ity) / math.pow(2.0,(resdepth-i)), float(isc) / math.pow(2.0,(resdepth-i)), itheta, k, m, stdvar, mean, eigenvecs, maxiter, orient)
        itx = ntx * math.pow(2.0,(resdepth-i))
        ity = nty * math.pow(2.0,(resdepth-i))
        isc = nsc * math.pow(2.0,(resdepth-i))
        
    return lm.transform_shape(lm.pca_reconstruct(b,mean,eigenvecs),itx,ity,isc,itheta), itervec
Exemplo n.º 3
0
def asm(imgtf, means, covs, b, tx, ty, s, theta, k, m, stdvar, mean, eigenvecs, maxiter, orient):
    numit = -1
    for i in range(maxiter):                 
        shapetf = lm.pca_reconstruct(b,mean,eigenvecs)
        shapetf = lm.transform_shape(shapetf, tx, ty, s, theta)   
        approx, stop = find_new_points(imgtf, shapetf, means, covs, k, m, orient)
        
        if stop:
            numit = i+1
            print '  Stopped at iteration ' + str(i) + ' out of ' + str(maxiter) + ' maximum.'
            break
        b, tx, ty, s, theta = match_model_to_target(approx, mean, eigenvecs)
        
        #Check for plausible shapes
        for p in range(b.size):
            b[p, 0] = max(min(b[p, 0],3*stdvar[p]),-3*stdvar[p])
        
        if i == maxiter - 1:
            numit = maxiter
            print '  Completed max number of iterations: ' + str(i + 1)
            
    return b, tx, ty, s, theta, numit
Exemplo n.º 4
0
def match_model_to_target(Y, xbar, P):
    conv_thresh = 0.000001 
    #1. 
    b = np.reshape(np.zeros(P[0].size),(P[0].size,1))
    lb = np.reshape(np.zeros(P[0].size),(P[0].size,1))
    tx = ty = theta = 0
    s = 1
    
    while True:
        #2.
        x = lm.pca_reconstruct(b, xbar, P)
    
        #3.
        ltx = tx
        lty = ty
        ls = s
        ltheta = theta
        tx, ty, s, theta = lm.get_align_params(x, Y)
    
        #4.
        y = lm.transform_shape_inv(Y,tx, ty, s, theta)
        
        #img = io.get_objectspace_img()
        #draw.draw_aligned_contours(img,x)
    
        #5
        yprime = y / ( np.dot(y.flatten(),xbar.flatten()))
        
        #draw.draw_aligned_contours(img,xbar)
        #io.show_on_screen(img,1)
    
        #6
        lb = b
        b = np.dot(P.T,(yprime - xbar))
        if ((abs(b - lb) < conv_thresh).all() and abs(tx - ltx) < conv_thresh and 
            abs(ty - lty) < conv_thresh and abs(s - ls) < conv_thresh and abs(theta - ltheta) < conv_thresh):
            break;
    
    return b, tx, ty, s, theta