Ejemplo n.º 1
0
    def readInASM(self, asm):
        allLines = None
        #f = "C:\\Users\\Valerie\\Documents\\Visual Studio 2013\\Projects\\ActiveShapeModels\\ActiveShapeModels\\outputs\\outfile-ASM-100iters-500tr.txt"
        with open(
                os.path.join(
                    self.output, 'outfile-ASM-%diters-%dtr.txt' %
                    (self.nIters, self.nTrain)), "r") as infile:
            #with open( f, "r") as infile:
            allLines = infile.readlines()
            cleanLines = map(lambda x: x.strip().split(), allLines)

        s = []

        for tuple in cleanLines:
            if tuple[0] == '!!!':
                if s != []:
                    asm.meanShape = ActiveShape(s)
                    s = []
                else:
                    pass
            elif tuple[0] == '@@@':
                if s != []:
                    asm.addShape(ActiveShape(s))
                    s = []
                else:
                    pass
            else:
                s.append(Vector(float(tuple[0]), float(tuple[1])))
        return asm
 def calcNormScale( self, shape ):
     if self.n == 68:
         d = Point.dist( shape.shapePoints[self.leftEyeIx], shape.shapePoints[self.rightEyeIx] )
     else :
         rc = ActiveShape.centroid( ActiveShape( shape.shapePoints[ 31 : 35 ]  )   )
         lc = ActiveShape.centroid( ActiveShape( shape.shapePoints[ 27 : 31 ] ) )
         d = Point.dist( rc, lc )
     s = float(1)/float(d)
     return s
 def calcNormScale(self, shape):
     if self.n == 68:
         d = Point.dist(shape.shapePoints[self.leftEyeIx],
                        shape.shapePoints[self.rightEyeIx])
     else:
         rc = ActiveShape.centroid(ActiveShape(shape.shapePoints[31:35]))
         lc = ActiveShape.centroid(ActiveShape(shape.shapePoints[27:31]))
         d = Point.dist(rc, lc)
     s = float(1) / float(d)
     return s
Ejemplo n.º 4
0
    def readInPoints(self, asm):
        m = 0

        for f in self.pointFiles:
            ptList = self.readInOneDude(f)
            if self.doExclImgs:
                if m in (set(range(self.nTrain)) - set(self.exclImgs)):
                    asm.addShape(ActiveShape(ptList))
            else:
                asm.addShape(ActiveShape(ptList))

            m += 1

            if m > self.nTrain - 1:
                return asm
    def reProjectCumulative( self, numVals ) :
        P = self.P[:, 0:numVals]
        b = [ np.transpose(np.mat( self.b[ 0: numVals ] )) ] 

        X = np.add( self.xbar, np.dot( P, b ) )

        return ActiveShape.createShape( X )
Ejemplo n.º 6
0
    def reProjectCumulative(self, numVals):
        P = self.P[:, 0:numVals]
        b = [np.transpose(np.mat(self.b[0:numVals]))]

        X = np.add(self.xbar, np.dot(P, b))

        return ActiveShape.createShape(X)
    def calcMeanShape(self):
        xList = [el.xs for el in self.allShapes]
        yList = [el.ys for el in self.allShapes]
        meanPointsList = map(lambda x, y: Vector(x, y), np.mean(xList, 0),
                             np.mean(yList, 0))
        #        meanPointsList = zip( np.mean(xList, 0), np.mean(yList, 0) )

        return ActiveShape(meanPointsList)
 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()
Ejemplo n.º 9
0
    def calcdX(self):
        print "calcDX"
        #        print "CurrentShape as points"
        #        print self.X

        #        print "CurrentShape as ActiveShape"
        Xshape = ActiveShape.createShape(self.X)
        #       for p in Xshape.shapePoints:
        #           print p.x, p.y

        # for pt in Xshape.shapePoints:
        #    print pt.x, pt.y
        if self.method == "grad":
            PMC = pmc(self.img, self.maxPx)
            return np.ravel(map(lambda p: PMC.calcShift(p, "tx"), Xshape.shapePoints))
        else:
            TM = TemplateMatcher(self.method, self.fSize, self.fPts)
        return np.ravel(TM.performMatching(self.img, Xshape))
Ejemplo n.º 10
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()
Ejemplo n.º 11
0
    def calcdX(self):
        print "calcDX"
        #        print "CurrentShape as points"
        #        print self.X

        #        print "CurrentShape as ActiveShape"
        Xshape = ActiveShape.createShape(self.X)
        #       for p in Xshape.shapePoints:
        #           print p.x, p.y

        #for pt in Xshape.shapePoints:
        #    print pt.x, pt.y
        if self.method == "grad":
            PMC = pmc(self.img, self.maxPx)
            return np.ravel(
                map(lambda p: PMC.calcShift(p, 'tx'), Xshape.shapePoints))
        else:
            TM = TemplateMatcher(self.method, self.fSize, self.fPts)
        return np.ravel(TM.performMatching(self.img, Xshape))
    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()
Ejemplo n.º 13
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()
Ejemplo n.º 14
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
Ejemplo n.º 15
0
 def y(self):
     shape = ActiveShape.createShape(self.x)
     shape = shape.M(self.s, self.theta)
     pts = np.add(shape.flatten(), self.dX)
     pts = np.subtract(pts, self.dXc)
     return pts
Ejemplo n.º 16
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
Ejemplo n.º 17
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
Ejemplo n.º 18
0
 def y(self):
     shape = ActiveShape.createShape(self.x)
     shape = shape.M(self.s, self.theta)
     pts = np.add(shape.flatten(), self.dX)
     pts = np.subtract(pts, self.dXc)
     return pts
Ejemplo n.º 19
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
def coordOffset( pt, n ):
    y = pt.y   #row
    x = pt.x   #col
    return x - ( n - 1)/2, y - (n-1)/1 

#def run( ):
i = 20
tr = 500
out =    "C:\\Users\\Valerie\\Desktop\\output\\20-500-1" 

fh = FileHelper( i, tr, out )

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

# Read in image
I = cv2.imread( "C:\Users\Valerie\Desktop\MicroExpress\CASME2\CASME2_RAW\CASME2-RAW\sub01\EP02_01f\img1.jpg")
I = cv2.cvtColor( I, cv2.COLOR_BGR2GRAY)

# Align shape
asm = ActiveShapeModel( [36,31] )
asm = fh.readInASM( asm )
asm.PCA()  
appASM = ApplyASM( asm, i, tr, out, I )
m, tdict = appASM.initialPosition( )

def genTemplateArr( ):
 def projectOnePC( self, evIx, b )  :
     X = np.add( self.xbar, np.multiply( self.P[:,evIx],  b ) )
     return ActiveShape.createShape( X )
Ejemplo n.º 22
0
 def projectOnePC(self, evIx, b):
     X = np.add(self.xbar, np.multiply(self.P[:, evIx], b))
     return ActiveShape.createShape(X)
s = 1.2
t = [[-2],[4] ]

rMat = [ [ s * math.cos( r ), - s* math.sin( r ) ] ,
        [ s * math.sin( r ), s * math.cos( r ) ]] 

def p( x, y ):
    return [[x],[y]]

p1 = p( 1,1 )
p2 = p( 3,4 )
p3 = p( 4,2 )


def trans( p, rMat, t ):
    return np.dot( rMat, p ) + t


n1 = trans( p1, rMat, t)
n2 = trans( p2, rMat, t)
n3 = trans( p3, rMat, t)


AS1 = ActiveShape( [ (1,1), (3,4), (4,2) ] )
AS2 = ActiveShape( [(-2, 5.697), (-2.849, 9.940), (-0.303, 9.091 ) ])
ASM = ActiveShapeModel( [0,1] )
SA = ShapeAligner( ASM, 100, "" )

tdict = SA.calcAlignTransBtwn( AS2, AS1, np.ones( 3 ) )