def drawGroundTruth( ebenFace ):
    plt.imshow( ebenFace )
    DrawFace( ebenShape, plt).drawBold()
    DrawFace( ebenShape, plt).drawPoints()
    plt.xlim( 200, 525)
    plt.ylim( 525, 225 )
    plt.show()
def drawIndices( ebenFace ):
    addFace( ebenFace)
    DrawFace( ebenShape, plt).drawBold()
    DrawFace( ebenShape, plt).labelIndices()
    plt.xlim( 200, 525)
    plt.ylim( 525, 225 )
    plt.show()
    
    addFace( ebenFace)
    plt.imshow( ebenFace )
    DrawFace( ebenShape, plt).drawBold()
    DrawFace( ebenShape, plt).labelIndices()
    plt.xlim( 300, 425)
    plt.ylim( 430, 380 )
Exemplo n.º 3
0
    def PCAresults(self):
        with open(
                os.path.join(
                    self.out, 'faces-results-%diters-%dtr.txt' %
                    (self.fh.nIters, self.fh.nTrain)), 'w') as outfile:
            for i in range(20):
                outfile.write(
                    "%d: %f, %f \n" % (i, self.b[i] / sum(self.b),
                                       sum(self.b[:i + 1]) / sum(self.b)))

        ## Reproject multiple PCS and vary evals
        for i in range(10):
            self.showVaryMultiplePCS(i + 1)
            self.showVary(i)
            self.exampleEvalEvecs(i)

        ### Draw all faces
        for sh in self.asm.allShapes:
            DrawFace(sh, plt).drawContrast()
        plt.gca().invert_yaxis()
        plt.savefig(
            os.path.join(
                self.out, 'faces-all-%diters-%dtr.png' %
                (self.fh.nIters, self.fh.nTrain)))
        plt.close()
Exemplo n.º 4
0
    def showVary(self, evIx):
        # Vary one eigenvalue
        f, axes = plt.subplots(1, self.nPlots, sharex=True, sharey=True)

        rs = np.linspace(-self.lim * math.sqrt(self.b[evIx]),
                         self.lim * math.sqrt(self.b[evIx]), self.nPlots)

        for m in range(len(rs)):
            s = self.projectOnePC(evIx, rs[m])
            DrawFace(s, axes[m]).drawBold()
            axes[m].set_xlim(self.xlim)
            axes[m].set_ylim(self.ylim)
            self.plotEigenvectors(evIx, axes[m])
            DrawFace(self.asm.meanShape, axes[m]).drawContrast()
        plt.gca().invert_yaxis()
        f.savefig(os.path.join(self.out, 'faces-PC-%d.png' % evIx))

        plt.close()
Exemplo n.º 5
0
 def showVaryMultiplePCS(self, numPCs):
     f, axes = plt.subplots(1, self.nPlots)
     bs = []
     for p in range(numPCs):
         rs = np.linspace(-self.lim * math.sqrt(self.b[p]),
                          self.lim * math.sqrt(self.b[p]), self.nPlots)
         bs.append(rs)
     P = self.P[:, 0:numPCs]
     for pl in range(self.nPlots):
         b = [bs[p][pl] for p in range(len(bs))]
         X = np.add(self.xbar, np.dot(P, b))
         s = ActiveShape.createShape(X)
         DrawFace(s, axes[pl]).drawBold()
         axes[pl].set_xlim(self.xlim)
         axes[pl].set_ylim(self.ylim)
         axes[pl].invert_yaxis()
     f.savefig(os.path.join(self.out, 'faces-%d-PCs-at-once.png' % numPCs))
     plt.close()
Exemplo n.º 6
0
    def exampleEvalEvecs(self, evIx):
        vecs = np.array(self.P)
        newPts = np.add(self.xbar,
                        np.dot(math.sqrt(self.b[evIx]), vecs[:, evIx]))

        newx, newy = ActiveShape.deravel(newPts)

        ## Mean Shape
        DrawFace(self.asm.meanShape, plt).drawBold()

        ## Eigenvectors
        plt.plot([self.asm.meanShape.xs,
                  np.add(self.asm.meanShape.xs, newx)],
                 [self.asm.meanShape.ys,
                  np.add(self.asm.meanShape.ys, newy)],
                 c='#A0A0A0',
                 lw=1)
        plt.xlim(self.xlim)
        plt.ylim(self.ylim)

        ## New Shape
        plt.gca().invert_yaxis()
        plt.savefig(os.path.join(self.out, 'faces-eigenvectors-%d.png' % evIx))
        plt.close()
Exemplo n.º 7
0
    def alignEyes(self, eye1, eye2):

        x = ActiveShape.createShape(self.x)
        f, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2)
        # distance between eyes:
        d1 = Point.dist(eye1, eye2)
        rc = ActiveShape.centroid(ActiveShape(x.shapePoints[31:35]))
        lc = ActiveShape.centroid(ActiveShape(x.shapePoints[27:31]))

        if self.asm.n == 68:
            d2 = Point.dist(x.shapePoints[self.asm.rightEyeIx],
                            x.shapePoints[self.asm.leftEyeIx])
        else:
            d2 = Point.dist(rc, lc)
        s = float(d1 / d2)

        shape = copy.deepcopy(x)

        DrawFace(shape, ax1).drawBold()

        shape = shape.scale(s)
        DrawFace(shape, ax2).drawBold()

        rot, thetaRot = self.asm.calcNormRotateImg(shape)
        shape = shape.rotate(rot)
        DrawFace(shape, ax3).drawBold()

        ax1.invert_yaxis()
        ax2.invert_yaxis()
        ax3.invert_yaxis()

        rc = ActiveShape.centroid(ActiveShape(shape.shapePoints[31:35]))
        lc = ActiveShape.centroid(ActiveShape(shape.shapePoints[27:31]))

        if self.asm.n == 68:
            t = [[(eye2.x - shape.shapePoints[self.asm.leftEyeIx].x)],
                 [(eye2.y - shape.shapePoints[self.asm.leftEyeIx].y)]]
        else:
            t = [[(eye2.x - rc.x)], [(eye2.y - rc.y)]]
        shape = shape.translate(t)

        ### Check that initial shape is within image frame
        tempS = 0
        nr, nc = np.shape(self.img)
        for pt in shape.shapePoints:
            if pt.y > nr:
                #                print "y big"
                tempS += (pt.y - nr + 10) / nr
            if pt.x > nc:
                #                print "x big"
                tempS += (pt.x - nc + 10) / nc

        if tempS != 0:
            shape = shape.scale(1 - tempS)

            if self.asm.n == 68:
                t = [[(eye2.x - shape.shapePoints[self.asm.leftEyeIx].x)],
                     [(eye2.y - shape.shapePoints[self.asm.leftEyeIx].y)]]
            else:
                rc = ActiveShape.centroid(ActiveShape(
                    shape.shapePoints[31:35]))
                lc = ActiveShape.centroid(ActiveShape(
                    shape.shapePoints[27:31]))

                t = [[(eye2.x - rc.x)], [(eye2.y - rc.y)]]
            shape = shape.translate(t)

            #print "row: %d\tcol:%d" % ( pt.y, pt.x )
#            print np.shape(self.img )

        DrawFace(shape, ax4).drawBold()
        ax4.scatter(eye1.x, eye1.y, c='r')
        ax4.scatter(eye2.x, eye2.y, c='g')
        ax4.imshow(self.img, cmap='gray')
        f.show()
        plt.savefig(os.path.join(self.out, "deform-init.png"))
        plt.gca().invert_yaxis()
        plt.close()
        srot = np.dot(s, rot)
        transDict = {
            't': t,
            's': s,
            'rot': rot,
            'srot': srot,
            'theta': thetaRot
        }

        return shape, transDict
Exemplo n.º 8
0
    def applyASM(self):  ## Main
        SA = ShapeAligner(self.asm, 0, self.out)

        XShape, _ = self.initialPosition()

        xShape = ActiveShape.createShape(self.x)

        modelParams = SA.calcAlignTransBtwn(XShape, xShape,
                                            np.ones(self.asm.n))
        #        print "params after init"
        #        print modelParams
        #modelParams = SA.calcAlignTransBtwn(  XShape , xShape , np.ones( self.asm.n ) )
        self.s = modelParams['s']

        self.theta = modelParams['theta']

        self.Xc = self.genXc(modelParams)

        self.X = np.add(xShape.M(self.s, self.theta).flatten(), self.Xc)

        #        print "Initial Current shape"
        #        print self.X

        i = 0

        while np.mean(self.calcdX()) > 0.000001 and i < 150:
            print i

            f, (ax1, ax2) = plt.subplots(1,
                                         2)  #, sharex = True, sharey = True )

            xShape = ActiveShape.createShape(self.x)
            self.X = np.add(xShape.M(self.s, self.theta).flatten(), self.Xc)

            # Calculate point shifts
            self.dX = self.calcdX()
            #            "dX result"
            #print self.dX

            ## X + dX
            self.XdX = np.add(self.X, self.dX)

            ax1.imshow(self.img)
            DrawFace(self.X, ax1).drawContrast()

            DrawFace(self.XdX, ax1).drawBold()

            DrawFace(self.X, ax2).drawContrast()
            DrawFace(self.XdX, ax2).drawBold()
            ax1.set_xlim(0, np.shape(self.img)[1])
            ax1.set_ylim(np.shape(self.img)[0], 0)
            ax2.invert_yaxis()
            f.suptitle(
                "Original Shape (Contrast) and Gradient Suggested Shape (Bold)"
            )
            f.savefig(os.path.join(self.out, "deformation-iter-%d.png" % i))

            ## Find X --> X + dX
            XShape = ActiveShape.createShape(self.X)
            XdXShape = ActiveShape.createShape(self.XdX)
            deltaParams = SA.calcAlignTransBtwn(XdXShape, XShape,
                                                np.ones(self.asm.n))

            ## Get transformation constrained delta parameters
            self.d0 = deltaParams['theta']
            self.ds = deltaParams['s']
            self.dXc = self.genXc(deltaParams)

            ## Calculate dx
            yShape = ActiveShape.createShape(self.y)
            f, (ax1, ax2) = plt.subplots(1, 2)
            yt = yShape.M(1 / (self.s * (1 + self.ds)),
                          -(self.theta + self.d0))

            DrawFace(yShape, ax1).drawBold()
            DrawFace(yt, ax2).drawBold()

            f.suptitle("Transformed contour")
            ax1.set_title("Original")
            ax2.set_title("Delta'd")
            ax1.invert_yaxis()
            ax2.invert_yaxis()
            f.savefig(os.path.join(self.out, "y-%d.png" % i))
            plt.close()

            self.dx = np.subtract(yt.flatten(), self.x)

            ## Calculate db
            self.db = np.dot(np.transpose(self.P), self.dx)

            ## Update
            self.theta += self.d0
            self.s = self.s * (1 + self.ds)
            self.Xc = np.add(self.Xc, self.dXc)

            self.b = np.add(self.b, self.db)

            f.clear()
            plt.close()

            i += 1
        print "It took you %d iterations" % i
black = [[0,0,0],[0,0,0],[0,0,0]]
white = [[255,255,255],[255,255,255],[255,255,255]]
big = np.reshape( range( 25 ), (5,5 ) )
## max SSD = 255 ^ 2 * number of tiles ( w/o sub mean)






    


addFace(ebenFace)
DrawFace( m, plt ).drawBold()
DrawFace( m, plt ).labelIndices()

                              

### Slicing testing

def sliceTest( big ) :
    nr, nc = np.shape( big )
    for i in range( nr ):
        for j in range( nc ):
            print i, j 
            print slice( big, 3, Vector( j, i ) )
            print slice( big, 5, Vector(j, i ) )
    
                                  
import numpy as np
import math
from image_processing.TemplateMatcher import TemplateMatcher

i = 20
tr = 500
out =    "C:\\Users\\Valerie\\Desktop\\output\\ASMTraining-MessingAround\\20-500-1" 

fh = FileHelper( i, tr, out, False, False )

ebenFace  =  fh.readInImage()
ebenPoints = fh.readInOneDude( '000_1_1.pts')  
ebenShape = ActiveShape( ebenPoints )

## draw indices
DrawFace( ebenShape, plt).labelIndices()
plt.imshow( ebenFace )
plt.set_cmap( "gray" )
plt.gca().axes.xaxis.set_ticks([])
plt.gca().axes.yaxis.set_ticks([])
plt.show()


fh = FileHelper( i, tr, out, True, False )

ebenFace  =  fh.readInImage()
ebenPoints = fh.readInOneDude( '000_1_1.pts')  
ebenShape = ActiveShape( ebenPoints 

## draw indices
DrawFace( ebenShape, plt).labelIndices()
q1 = np.percentile(d1, 0.25)
sd = np.std(d1)
max = np.max(d1)
q3 = np.percentile(d1, 0.75)
iqr = q3 - q1

cu1 = mn + 2 * sd
cu2 = mn + 3 * sd

i1 = np.where(d1 > cu1)[0]
i2 = np.where(d1 > cu2)[0]

f, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)

for ix in list(i1):
    DrawFace(asm.allShapes[ix], ax1).drawContrast()
for ix in list(i2):
    DrawFace(asm.allShapes[ix], ax2).drawContrast()

DrawFace(asm.meanShape, ax1).drawBold()
DrawFace(asm.meanShape, ax2).drawBold()

plt.show()
plt.close()

f, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
for ix in (set(range(500)) - set(i1)):
    DrawFace(asm.allShapes[ix], ax1).drawContrast()
for ix in (set(range(500)) - set(i2)):
    DrawFace(asm.allShapes[ix], ax2).drawContrast()
DrawFace(asm.meanShape, ax1).drawBold()