Ejemplo n.º 1
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.º 2
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
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 ) )