def process(fPts, fImgs, train, method, maxPx, fSize): params = { "nIters": 7, "nTrain": 500, "filterPts": fPts, "filterImgs": fImgs, "trainASM": train, "writePCA": False, "maxPx": maxPx, "fSize": fSize, "method": method } params = OrderedDict(sorted(params.items(), key=lambda t: len(t[0]))) ##Methods: "SSD", "NCorr", or "grad" ASMout = getASMfolder(params) appASMout = getMethodSubFolder(params, ASMout) fh = FileHelper(params["nIters"], params["nTrain"], ASMout, params["filterPts"], params["filterImgs"]) if params["filterPts"]: asm = ActiveShapeModel([43, 35]) else: asm = ActiveShapeModel([36, 31]) if params["trainASM"]: asm = fh.readInPoints(asm) asm = ShapeAligner(asm, params["nIters"], ASMout).alignTrainingSet() fh.writeOutASM(asm) #write out to read in later else: asm = fh.readInASM(asm) ### Calculate Principal components asm.PCA() if params["trainASM"] or params["writePCA"]: ### Draw PCA stuffs if not os.path.exists(os.path.join(ASMout, "PCA")): os.mkdir(os.path.join(ASMout, "PCA")) wu = WriteUp(asm, fh) wu.PCAresults() if not params["trainASM"]: ### Apply ASM to image img = cv2.imread( "C:\Users\Valerie\Desktop\MicroExpress\CASME2\CASME2_RAW\CASME2-RAW\sub01\EP02_01f\img1.jpg" ) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) appASM = ApplyASM(asm, params["nIters"], params["nTrain"], appASMout, img, params['method'], params['maxPx'], params['fSize'], params["filterPts"]) appASM.applyASM()
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 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
study = "%d-%d-%d" % (iters, training, i) if train or align: os.mkdir(os.path.join(output, study)) else: study = "%d-%d-%d" % (iters, training, i) output = os.path.join(output, study) fh = FileHelper(iters, training, output) if train: asm = ActiveShapeModel([36, 31]) asm = fh.readInPoints(asm) ### Align Shapes if align: asm = ShapeAligner(asm, iters, output).alignTrainingSet() fh.writeOutASM(asm) #write out to read in later d = map(lambda x: x.shapeDist(asm.normMeanShape), asm.allShapes) d1 = np.mean(d, 1) min = np.min(d1) mn = np.mean(d1) 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
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 ) )