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
 def calcR( self ):
     """
     Calculates distance matrix between all points for a given shape
     sets global variable 
     """
     sp = self.shapePoints
     ## For every point in shapePoints, calculate distance to other points
     R = [ [Point.dist( sp[k], sp[l] ) for k in range( self.n )] for l in range( self.n ) ]
     return R
Exemple #4
0
 def calcR(self):
     """
     Calculates distance matrix between all points for a given shape
     sets global variable 
     """
     sp = self.shapePoints
     ## For every point in shapePoints, calculate distance to other points
     R = [[Point.dist(sp[k], sp[l]) for k in range(self.n)]
          for l in range(self.n)]
     return R
Exemple #5
0
    def crop(img, fh,
             fw):  ## In terms of x, y not h, w !!!   # or the other way>>>

        # Using eyes
        eye1, eye2 = PreProcessing.eyeDetection(img)
        d = Point.dist(eye1, eye2)

        newH = int(
            fh *
            d)  #set more firmly for other types of images for consistent size
        newW = int(fw * d)

        y0, x0 = PreProcessing.newEyeLoc(eye1, eye2, newH, newW, d)

        return img[y0:y0 + newH, x0:x0 + newW]
Exemple #6
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
    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
 def shapeDist( self, shape ):
     d = map( lambda x, y : Point.dist( x,y ), self.shapePoints, shape.shapePoints )
     return d
Exemple #9
0
 def shapeDist(self, shape):
     d = map(lambda x, y: Point.dist(x, y), self.shapePoints,
             shape.shapePoints)
     return d