def __init__(self, parent): BasicMeshCanvas.__init__(self, parent) #Skeleton animation variables self.skeleton = Skeleton() self.animator = SkeletonAnimator(self.skeleton) self.animationState = 0 self.animating = False
def __init__(self, player, screen): """ :param player: Player object :param screen: Display window Creates skeleton boss level """ Level.__init__(self, player, screen) level = [[platforms.SAND_GROUND, -1, 670], [platforms.SAND_GROUND, 259, 670], [platforms.SAND_GROUND, 519, 670], [platforms.SAND_GROUND, 779, 670], [platforms.SAND_GROUND, -1, -81], [platforms.SAND_GROUND, 259, -81], [platforms.SAND_GROUND, 519, -81], [platforms.SAND_GROUND, 779, -81], [platforms.SAND_BIG, -231, 29], [platforms.SAND_BIG, constants.SCREEN_WIDTH -30, 29], [platforms.SAND_FLOAT, 140, 400], [platforms.SAND_FLOAT, 599, 400], [platforms.SAND_FLOAT, 140, 120], [platforms.SAND_FLOAT, 599, 120], ] for platform in level: block = platforms.Platform(platform[0]) block.rect.x = platform[1] block.rect.y = platform[2] block.player = self.player self.platform_list.add(block) background = platforms.backgroundSand() background.rect.x = 0 background.rect.y = 0 self.decor.add(background) ske = Skeleton.Skeleton(); ske.rect.x = 375 ske.rect.y = 0 ske.player = self.player ske.screen = screen ske.quick_sand = self.platform_quicksand self.attacks = ske.attacks ske.boss = self.behind_boss_man self.behind_boss_man.add(ske)
def OnLoadASFFile(self, evt): dlg = wx.FileDialog(self, "Choose a file", ".", "", "", wx.OPEN) if dlg.ShowModal() == wx.ID_OK: filename = dlg.GetFilename() dirname = dlg.GetDirectory() filepath = os.path.join(dirname, filename) self.asffilename = filepath self.glcanvas.skeleton = Skeleton() self.glcanvas.skeleton.initFromFile(filepath) self.glcanvas.Refresh() dlg.Destroy() return
def main(ifile_name, ofile_name1, bin_fn="bfile.bin", svg_file=None): im = imageio.imread(ifile_name, as_gray=True) skeleton = Skeleton.Skeleton(im) skeleton.segments.addInitialStartPt() skeleton.euclidMstOrder() skeleton.segments.concatSegments() #skeleton.segments.simplify() skeleton.segments.segment2grad(interior=True) skeleton.segments.renderGrad() im = skeleton.segments.grad imageio.imwrite(ofile_name1, im) skeleton.segments.scaleBin() skeleton.segments.binWrite(bin_fn)
def render(self, surface, player): ti = self.tmxdata.get_tile_image_by_gid for layer in self.tmxdata.visible_layers: if isinstance(layer, pytmx.TiledTileLayer): if layer.name == "background": for x, y, gid, in layer: tile = ti(gid) if tile: surface.blit(tile, (x * self.tmxdata.tilewidth, y * self.tmxdata.tileheight)) for object in self.tmxdata.objects: if object.name == "player": player.camera.x = object.x player.camera.y = object.y player.rect.x = object.x player.rect.y = object.y if object.name == "collidable": collidable = Collidable(object.x, object.y, object.width, object.height) collision_sprite.add(collidable) if object.name == "entrance": entrance = Entrance(object.x, object.y, object.width, object.height, object.location, object.backgroundMapfile, player) if object.name == "npc": if object.type == "shopkeeper": shopkeeper = Shopkeeper(object.x, object.y, self.event, self.fontrenderer) if object.name == "musicplayer": if object.type == "overworldTheme": pygame.mixer.stop() overworldTheme.play() if object.type == "dungeonTheme": pygame.mixer.stop() dungeonTheme.play() if object.type == "shopTheme": pygame.mixer.stop() shopTheme.play() if object.name == "skeleton": skelly = Skeleton(object.x, object.y) if object.name == "lava": lava = Lava(object.x, object.y, object.width, object.height) #add wall code here if object.name == "money": money = Money(object.x, object.y) if object.name == "staticEnemy": staticenemy = StaticEnemy(object.x, object.y)
def __init__(self, parent): attribs = (glcanvas.WX_GL_RGBA, glcanvas.WX_GL_DOUBLEBUFFER, glcanvas.WX_GL_DEPTH_SIZE, 24) glcanvas.GLCanvas.__init__(self, parent, -1, attribList = attribs) self.context = glcanvas.GLContext(self) self.parent = parent #Camera state variables self.size = self.GetClientSize() #self.camera = MouseSphericalCamera(self.size.x, self.size.y) self.camera = MousePolarCamera(self.size.width, self.size.height) #Skeleton animation variables self.skeleton = Skeleton() self.animator = SkeletonAnimator(self.skeleton) self.animationState = 0 self.animating = False #Main state variables self.MousePos = [0, 0] self.initiallyResized = False self.bbox = np.array([[-1, -1, -1], [1, 1, 1]]) random.seed() self.GLinitialized = False #GL-related events wx.EVT_ERASE_BACKGROUND(self, self.processEraseBackgroundEvent) wx.EVT_SIZE(self, self.processSizeEvent) wx.EVT_PAINT(self, self.processPaintEvent) #Mouse Events wx.EVT_LEFT_DOWN(self, self.MouseDown) wx.EVT_LEFT_UP(self, self.MouseUp) wx.EVT_RIGHT_DOWN(self, self.MouseDown) wx.EVT_RIGHT_UP(self, self.MouseUp) wx.EVT_MIDDLE_DOWN(self, self.MouseDown) wx.EVT_MIDDLE_UP(self, self.MouseUp) wx.EVT_MOTION(self, self.MouseMotion)
def threadCrown(filepath): global io rtpSkel = -1 crownT = OrderedDict() imgL = [] stemCorrection = bool(int(options[8][1])) print io.getHomePath() oldHome = io.getHomePath() os.chdir(io.getHomePath()) io.setHomePath('./Crown/') f = io.scanDir() for (counter, i) in enumerate(f): io.setFileName(os.path.basename(i)) io.setidIdx(imgID) print 'processing Crown file: ' + i xScale = allPara[counter][7] yScale = allPara[counter][8] analysis = Analysis.Analysis(io, (xScale + yScale) / 2) rtp = RootTipPaths.RootTipPaths(io) try: img = scipy.misc.imread(i, flatten=True) except: print 'Image not readable' img = -1 if len(img) > 0: seg = Segmentation.Segmentation(img, io) imgL = seg.label() print 'compute root profile' currT = time.time() if ifAnyKeyIsTrue([ 'AVG_DENSITY', 'WIDTH_MED', 'WIDTH_MAX', 'DIA_STM_SIMPLE', 'D10', 'D20', 'D30', 'D40', 'D50', 'D60', 'D70', 'D80', 'D90', 'DS10', 'DS20', 'DS30', 'DS40', 'DS50', 'DS60', 'DS70', 'DS80', 'DS90', 'AREA', 'ANG_TOP', 'ANG_BTM' ]): crownT['AVG_DENSITY'], crownT['WIDTH_MED'], crownT[ 'WIDTH_MAX'], crownT['D10'], crownT['D20'], crownT[ 'D30'], crownT['D40'], crownT['D50'], crownT[ 'D60'], crownT['D70'], crownT['D80'], crownT[ 'D90'], crownT['DS10'], crownT['DS20'], crownT[ 'DS30'], crownT['DS40'], crownT[ 'DS50'], crownT['DS60'], crownT[ 'DS70'], crownT['DS80'], crownT[ 'DS90'], crownT['AREA'], crownT[ 'DIA_STM_SIMPLE'], crownT[ 'ANG_TOP'], crownT[ 'ANG_BTM'] = analysis.getWidthOverHeight( imgL, xScale, yScale) print 'Mask traits computed ' + str(time.time() - currT) + 's' if ifAnyKeyIsTrue([ 'DIA_STM', 'TD_MED', 'TD_AVG', 'STA_RANGE', 'STA_DOM_I', 'STA_DOM_II', 'STA_25_I', 'STA_25_II', 'STA_50_I', 'STA_50_II', 'STA_75_I', 'STA_75_II', 'STA_90_I', 'STA_90_II', 'RTA_DOM_I', 'RTA_DOM_II', 'STA_MIN', 'STA_MAX', 'STA_MED', 'RTA_RANGE', 'RTA_MIN', 'RTA_MAX', 'RTA_MED', 'NR_RTP_SEG_I', 'NR_RTP_SEG_II', 'ADVT_COUNT', 'BASAL_COUNT', 'ADVT_ANG', 'BASAL_ANG', 'HYP_DIA', 'TAP_DIA', 'MAX_DIA_90', 'DROP_50', 'CP_DIA25', 'CP_DIA50', 'CP_DIA75', 'CP_DIA90', 'SKL_DEPTH', 'SKL_WIDTH' ]): currT = time.time() skel = Skeleton.Skeleton(imgL) testSkel, testDia = skel.skel(imgL) scipy.misc.imsave( io.getHomePath() + '/Skeleton/' + io.getFileName() + '_skel.png', testSkel) print 'Medial axis computed ' + str(time.time() - currT) + 's' currT = time.time() path, skelGraph, crownT[ 'DIA_STM'], skelSize = seg.findThickestPath( testSkel, testDia, xScale, yScale) allPara[counter][10] = skelSize print 'Central path computed ' + str(time.time() - currT) + 's' if ifAnyKeyIsTrue([ 'TD_MED', 'TD_AVG', 'STA_RANGE', 'STA_DOM_I', 'STA_DOM_II', 'STA_25_I', 'STA_25_II', 'STA_50_I', 'STA_50_II', 'STA_75_I', 'STA_75_II', 'STA_90_I', 'STA_90_II', 'RTA_DOM_I', 'RTA_DOM_II', 'STA_MIN', 'STA_MAX', 'STA_MED', 'RTA_RANGE', 'RTA_MIN', 'RTA_MAX', 'RTA_MED', 'NR_RTP_SEG_I', 'NR_RTP_SEG_II', 'ADVT_COUNT', 'BASAL_COUNT', 'ADVT_ANG', 'BASAL_ANG', 'HYP_DIA', 'TAP_DIA', 'MAX_DIA_90', 'DROP_50', 'CP_DIA25', 'CP_DIA50', 'CP_DIA75', 'CP_DIA90', 'SKL_DEPTH', 'SKL_WIDTH', 'RTP_COUNT' ]): print 'Compute RTP skeleton' currT = time.time() rtpSkel, crownT['RTP_COUNT'], crownT['TD_MED'], crownT[ 'TD_AVG'], crownT['MAX_DIA_90'], rtps, tips, crownT[ 'SKL_WIDTH'], crownT['SKL_DEPTH'] = rtp.getRTPSkeleton( path, skelGraph, True) seg.setTips(tips) print 'RTP Skeleton computed ' + str(time.time() - currT) + 's' allPara[len(allPara) - 1][2] = seg.getFail() if ifAnyKeyIsTrue(['RDISTR_X', 'RDISTR_Y']): print 'Compute spatial root distribution' currT = time.time() crownT['RDISTR_X'], crownT['RDISTR_Y'] = analysis.getSymmetry( rtps, rtpSkel) print 'Symmetry computed ' + str(time.time() - currT) + 's' if rtpSkel != -1: if ifAnyKeyIsTrue([ 'NR_RTP_SEG_I', 'NR_RTP_SEG_II', 'ADVT_COUNT', 'BASAL_COUNT', 'ADVT_ANG', 'BASAL_ANG', 'HYP_DIA', 'TAP_DIA' ]): print 'searching for hypocotyl' currT = time.time() branchRad, nrPaths = seg.findHypocotylCluster( path, rtpSkel) print 'hypocotyl computed ' + str(time.time() - currT) + 's' print 'starting kmeans' try: currT = time.time() c1x, c1y, c2x, c2y = analysis.plotDiaRadius( nrPaths, branchRad, path, 2) print '2 clusters computed in ' + str(time.time() - currT) + 's' currT = time.time() segImg = seg.makeSegmentationPicture( path, rtpSkel, img, xScale, yScale, c1x, c1y, c2x, c2y) scipy.misc.imsave( io.getHomePath() + '/Result/' + io.getFileName() + 'Seg2.png', segImg) crownT['ADVT_COUNT'], crownT['BASAL_COUNT'], crownT[ 'NR_RTP_SEG_I'], crownT['NR_RTP_SEG_II'], crownT[ 'HYP_DIA'], crownT[ 'TAP_DIA'] = analysis.countRootsPerSegment( c1y, c2y, c1x, c2x) except: c1x = None c1y = None c2x = None c2y = None pass crownT['DROP_50'] = analysis.RTPsOverDepth(path, rtpSkel) print 'count roots per segment' print 'Root classes computed in ' + str(time.time() - currT) + 's' if ifAnyKeyIsTrue([ 'ADVT_ANG', 'BASAL_ANG', 'STA_RANGE', 'STA_DOM_I', 'STA_DOM_II', 'STA_25_I', 'STA_25_II', 'STA_50_I', 'STA_50_II', 'STA_75_I', 'STA_75_II', 'STA_90_I', 'STA_90_II', 'RTA_DOM_I', 'RTA_DOM_II', 'STA_MIN', 'STA_MAX', 'STA_MED', 'RTA_RANGE', 'RTA_MIN', 'RTA_MAX', 'RTA_MED' ]): currT = time.time() lat, corrBranchpts = seg.findLaterals( rtps, rtpSkel, (xScale + yScale) / 2, None) print 'seg.findLaterals computed in ' + str(time.time() - currT) + 's' print 'Compute angles at 2cm' currT = time.time() if c1x != None and c1y != None and c2x != None and c2y != None: crownT['ADVT_ANG'], crownT[ 'BASAL_ANG'] = analysis.anglesPerClusterAtDist( c1y, c2y, rtpSkel, path, lat, corrBranchpts, (xScale + yScale) / 2, dist=20) else: crownT['ADVT_ANG'] = 'nan' crownT['BASAL_NG'] = 'nan' print 'angles at 2cm computed in ' + str(time.time() - currT) + 's' if ifAnyKeyIsTrue([ 'STA_25_I', 'STA_25_II', 'STA_50_I', 'STA_50_II', 'STA_75_I', 'STA_75_II', 'STA_90_I', 'STA_90_II' ]): try: print 'compute quantile angles' currT = time.time() a25, a50, a75, a90 = analysis.calculateAngleQuantiles( path, lat, corrBranchpts, rtpSkel) print 'angles computed in ' + str(time.time() - currT) + 's' except: a25 = ['nan'] a50 = ['nan'] a75 = ['nan'] a90 = ['nan'] print 'ERROR: No quantile angles calculated' if ifAnyKeyIsTrue( ['RTA_RANGE', 'RTA_MIN', 'RTA_MAX', 'RTA_MED']): try: print 'compute angles' currT = time.time() crownT['RTA_MED'], crownT['RTA_MIN'], crownT[ 'RTA_MAX'], crownT[ 'RTA_RANGE'], anglesN = analysis.calculateAngles( path, lat, corrBranchpts, rtpSkel) print 'RTA angle characteristics computed in ' + str( time.time() - currT) + 's' except: print 'ERROR: No RTA angles calculated' if ifAnyKeyIsTrue( ['STA_RANGE', 'STA_MIN', 'STA_MAX', 'STA_MED']): try: print 'compute STA angles' currT = time.time() crownT['STA_RANGE'], crownT['STA_MED'], crownT[ 'STA_MIN'], crownT[ 'STA_MAX'], angles = analysis.getLateralAngles( path, lat, corrBranchpts, rtpSkel) print 'STA angles characteristics computed in ' + str( time.time() - currT) + 's' except: print 'ERROR: No STA angles calculated' if ifAnyKeyIsTrue( ['CP_DIA25', 'CP_DIA50', 'CP_DIA75', 'CP_DIA90']): try: print 'compute diameter quantils' currT = time.time() crownT['CP_DIA25'], crownT['CP_DIA50'], crownT[ 'CP_DIA75'], crownT[ 'CP_DIA90'] = analysis.getDiameterQuantilesAlongSinglePath( path, rtpSkel) print 'Tap diameters computed in ' + str( time.time() - currT) + 's' except: print 'ERROR: No quantile diameters calculated' if ifAnyKeyIsTrue(['STA_DOM_I', 'STA_DOM_II']): try: print 'compute STA dominant angles' currT = time.time() crownT['STA_DOM_I'], crownT[ 'STA_DOM_II'] = analysis.findHistoPeaks(angles) print 'STA dominant angles computed in ' + str( time.time() - currT) + 's' except: print 'ERROR: No dominant angles calculated (STA)' if ifAnyKeyIsTrue(['STA_25_I', 'STA_25_II']): try: currT = time.time() crownT['STA_25_I'], crownT[ 'STA_25_II'] = analysis.findHistoPeaks(a25) print 'STA 25 angles computed in ' + str( time.time() - currT) + 's' except: print 'ERROR: No dominant angles25 calculated' if ifAnyKeyIsTrue(['STA_50_I', 'STA_50_II']): try: currT = time.time() crownT['STA_50_I'], crownT[ 'STA_50_II'] = analysis.findHistoPeaks(a50) print 'STA 50 angles computed in ' + str( time.time() - currT) + 's' except: print 'ERROR: No dominant angles50 calculated' if ifAnyKeyIsTrue(['STA_75_I', 'STA_75_II']): try: currT = time.time() crownT['STA_75_I'], crownT[ 'STA_75_II'] = analysis.findHistoPeaks(a75) print 'STA 75 angles computed in ' + str( time.time() - currT) + 's' except: print 'ERROR: No dominant angles75 calculated' if ifAnyKeyIsTrue(['STA_90_I', 'STA_90_II']): try: currT = time.time() crownT['STA_90_I'], crownT[ 'STA_90_II'] = analysis.findHistoPeaks(a90) print 'STA 90 angles computed in ' + str( time.time() - currT) + 's' except: print 'ERROR: No dominant angles90 calculated' if ifAnyKeyIsTrue(['RTA_DOM_I', 'RTA_DOM_II']): try: currT = time.time() crownT['RTA_DOM_I'], crownT[ 'RTA_DOM_II'] = analysis.findHistoPeaks( anglesN) print 'angles computed in ' + str(time.time() - currT) + 's' except: print 'ERROR: No dominant RTA angles calculated' io.setHomePath(oldHome) if maxExRoot >= 1: rtpSkel = -1 os.chdir(io.getHomePath()) io.setHomePath('./Lateral/') f = io.scanDir() for (counter, i) in enumerate(f): print 'processing lateral file: ' + i if maxExRoot > 0: xScale = allPara[counter / maxExRoot][7] yScale = allPara[counter / maxExRoot][8] io.setFileName(os.path.basename(i)) else: xScale = allPara[counter][7] yScale = allPara[counter][8] io.setFileName(os.path.basename(i)) io.setidIdx(counter) rtp = RootTipPaths.RootTipPaths(io) analysis = Analysis.Analysis(io, (xScale + yScale) / 2) try: img = scipy.misc.imread(i, flatten=True) except: print 'Image not readable' img = [] pass if len(img) > 0: seg = Segmentation.Segmentation(img, io=io) imgL = seg.label() if imgL != None: skel = Skeleton.Skeleton(imgL) testSkel, testDia = skel.skel(imgL) path, skelGraph = seg.findThickestPathLateral( testSkel, testDia, xScale, yScale) if ifAnyKeyIsTrue([ 'LT_AVG_LEN', 'NODAL_LEN', 'LT_BRA_FRQ', 'NODAL_AVG_DIA', 'LT_AVG_ANG', 'LT_ANG_RANGE', 'LT_MIN_ANG', 'LT_MAX_ANG', 'LT_DIST_FIRST', 'LT_MED_DIA', 'LT_AVG_DIA' ]): rtpSkel, _, crownT['LT_MED_DIA'], crownT[ 'LT_AVG_DIA'], _, rtps, _, _, _ = rtp.getRTPSkeleton( path, skelGraph, True) if rtpSkel != -1: if ifAnyKeyIsTrue(['LT_BRA_FRQ']): crownT[ 'LT_BRA_FRQ'] = analysis.getBranchingfrequencyAlongSinglePath( rtps, path) crownT[ 'NODAL_AVG_DIA'], _ = analysis.getDiametersAlongSinglePath( path, rtpSkel, (xScale + yScale) / 2) crownT['NODAL_LEN'] = analysis.getLengthOfPath( path) if ifAnyKeyIsTrue([ 'LT_DIST_FIRST', 'LT_AVG_LEN', 'LT_BRA_FRQ', 'LT_ANG_RANGE', 'LT_AVG_ANG', 'LT_MIN_ANG', 'LT_MAX_ANG' ]): lat, corrBranchpts, crownT[ 'LT_DIST_FIRST'] = seg.findLaterals( rtps, rtpSkel, (xScale + yScale) / 2, path) if ifAnyKeyIsTrue(['LT_AVG_LEN']): crownT[ 'LT_AVG_LEN'] = analysis.getLateralLength( lat, path, rtpSkel) if ifAnyKeyIsTrue([ 'LT_ANG_RANGE', 'LT_AVG_ANG', 'LT_MIN_ANG', 'LT_MAX_ANG' ]): crownT['LT_ANG_RANGE'], crownT[ 'LT_AVG_ANG'], crownT['LT_MIN_ANG'], crownT[ 'LT_MAX_ANG'], _ = analysis.getLateralAngles( path, lat, corrBranchpts, rtpSkel) allCrown.append(crownT.copy()) else: allCrown.append(crownT.copy()) io.setHomePath(oldHome)
def MOCAPJumpingJacksExample(doPartial = True, doInterpolated = False): #Load in MOCAP walking data skeleton = Skeleton() skeleton.initFromFile("MOCAP/22.asf") activity = SkeletonAnimator(skeleton) res = activity.initFromFileUsingOctave("MOCAP/22.asf", "MOCAP/22_16.amc") #Get quaternions XQ = res['XQ'] DQ = getQuaterionsSSM(XQ) DQS = getQuaterionsSSM(XQ, False) XQE = np.reshape(XQ, (XQ.shape[0]*XQ.shape[1], XQ.shape[2])) DQE = getSSM(XQE.T) #Load in Weizmann walking mask (I, IDims) = loadImageIOVideo("MOCAP/jumpingjackscropped.avi") I = I[25::, :] #(I, IDims) = loadImageIOVideo("MOCAP/jumpingjacks2mencropped.avi") #I = I[0:70] #I = I[0:int(I.shape[0]/2), :] if doInterpolated: I = getInterpolatedEuclideanTimeSeries(I, np.linspace(0, 1, 300)) DV = getSSM(I) print("DQ.shape = {}".format(DQ.shape)) print("DV.shape = {}".format(DV.shape)) plt.figure(figsize=(10, 10)) plt.subplot(221) plt.imshow(DQ, cmap = 'afmhot', interpolation = 'nearest') plt.title("Quaternions $\mathbb{R}P^3$ Embedding") plt.subplot(222) plt.imshow(DQS, cmap = 'afmhot', interpolation = 'nearest') plt.title("Quaternions $S^3$ Embedding") plt.subplot(223) plt.imshow(DQE, cmap = 'afmhot', interpolation = 'nearest') plt.title("Quaternions Euclidean Embedding") plt.subplot(224) plt.imshow(DV, cmap = 'afmhot', interpolation = 'nearest') plt.title("Jumping Jacks Video") plt.savefig("JumpingJacksEmbeddings.svg", bbox_inches = 'tight') L = 200 D1 = DQ D2 = DV (D1N1, D2N1) = matchSSMDist(D1, D2, L) (D2N2, D1N2) = matchSSMDist(D2, D1, L) if doPartial: matchfn = lambda x: x hvPenalty = -0.4 #Try 1 To 2 Normalization CSWM1 = doIBSMWatGPU(D1N1, D2N1, 0.3, True) CSWM1 = CSWM1 - np.median(CSWM1) CSWM1 = CSWM1/np.max(np.abs(CSWM1)) res1 = SMWat(CSWM1, matchfn, hvPenalty, backtrace = True) #Try 2 To 1 Normalization CSWM2 = doIBSMWatGPU(D1N2, D2N2, 0.3, True) CSWM2 = CSWM2 - np.median(CSWM2) CSWM2 = CSWM2/np.max(np.abs(CSWM2)) res2 = SMWat(CSWM2, matchfn, hvPenalty, backtrace = True) res = res1 CSWM = CSWM1 if res2['pathScore'] > res1['pathScore']: res = res2 CSWM = CSWM2 path = res['path'] else: CSWM1 = doIBDTWGPU(D1N1, D2N1, returnCSM = True) CSWM2 = doIBDTWGPU(D1N2, D2N2, returnCSM = True) (DAll, CSSM1, backpointers, path1) = DTWCSM(CSWM1) (DAll, CSSM2, backpointers, path2) = DTWCSM(CSWM2) CSWM = CSWM1 path = path1 if CSSM2[-1, -1] < CSSM1[-1, -1]: CSWM = CSWM2 path = path2 if not doPartial: #For better visualization for CSWM for IBDTW CSWM = np.log(0.001+CSWM) plt.clf() plt.subplot(2, 2, 1) plt.imshow(DQ, cmap = 'afmhot', interpolation = 'nearest') plt.xlabel("Frame Number") plt.ylabel("Frame Number") plt.title("MOCAP Quaternion SSM") plt.subplot(2, 2, 2) plt.imshow(DV, cmap = 'afmhot', interpolation = 'nearest') plt.xlabel("Frame Number") plt.ylabel("Frame Number") plt.title("Video Pixel SSM") plt.subplot(2, 2, 3) plt.imshow(CSWM, cmap = 'afmhot', interpolation = 'nearest', aspect = 'auto') plt.xlabel("MOCAP Frame Number") plt.ylabel("Video Frame Number") plt.title("CSWM") plt.subplot(2, 2, 4) plt.imshow(CSWM, cmap = 'afmhot', interpolation = 'nearest', aspect = 'auto') plt.scatter(path[:, 1], path[:, 0], 5, 'c', edgecolor = 'none') plt.xlabel("MOCAP Frame Number") plt.ylabel("Video Frame Number") plt.title("CSWM + Warping Path") path = projectPath(path, CSWM.shape[0], CSWM.shape[1], 1) plt.savefig("CSWM.svg") #Plot frames aligned to each other (IM, IMDims) = loadImageIOVideo("MOCAP/22_16.avi") plt.figure(figsize=(15, 5)) for i in range(path.shape[0]): plt.clf() plt.subplot(131) F = I[path[i, 1], :] F = np.reshape(F, IDims) plt.imshow(F) plt.title("Video Frame %i"%path[i, 1]) plt.axis("off") plt.subplot(132) F = IM[path[i, 0], :] F = np.reshape(F, IMDims) plt.imshow(F) plt.axis('off') plt.title("MOCAP Frame %i"%path[i, 0]) plt.subplot(133) plt.imshow(CSWM, aspect = 'auto', cmap = 'afmhot', interpolation = 'nearest') plt.scatter(path[:, 1], path[:, 0], 5, 'c', edgecolor = 'none') plt.scatter(path[i, 1], path[i, 0], 30, 'm') plt.xlabel("MOCAP Frame") plt.ylabel("Video Frame") plt.title("CSWM / Warping Path") plt.savefig("MOCAPAligned%i.png"%i, bbox_inches = 'tight')
def threadLateral(filepath): tipdiameter = 0. os.chdir(io.getHomePath()) io.setHomePath('./Lateral/') f = io.scanDir() for (counter, i) in enumerate(f): print 'processing lateral file: ' + i if maxExRoot > 0: xScale = allPara[counter / maxExRoot][7] yScale = allPara[counter / maxExRoot][8] io.setFileName(os.path.basename(i)) else: xScale = allPara[counter][7] yScale = allPara[counter][8] io.setFileName(os.path.basename(i)) io.setidIdx(counter) rtp = RootTipPaths.RootTipPaths(io, tipdiameter) rtp.setTipDiaFilter(tipdiameter) analysis = Analysis.Analysis(io, (xScale + yScale) / 2) lateralT = [] try: img = scipy.misc.imread(i, flatten=True) except: print 'Image not readable' img = [] pass if len(img) > 0: seg = Segmentation.Segmentation(img, io=io) imgL = seg.label() if imgL != None: skel = Skeleton.Skeleton(imgL) testSkel, testDia = skel.skel(imgL) path, skelGraph = seg.findThickestPathLateral( testSkel, testDia, xScale, yScale) rtpSkel, _, medianD, meanD, _, _, rtps, _, _, _ = rtp.getRTPSkeleton( path, skelGraph, True) if rtpSkel != -1: lBranchFreq = analysis.getBranchingfrequencyAlongSinglePath( rtps, path) avgLatDiameter, slope = analysis.getDiametersAlongSinglePath( path, rtpSkel, (xScale + yScale) / 2) lengthNodalRoot = analysis.getLengthOfPath(path) lat, corrBranchpts, distToFirst = seg.findLaterals( rtps, rtpSkel, (xScale + yScale) / 2) avgLLength = analysis.getLateralLength(lat, path, rtpSkel) angRange, avgAngle, minangle, maxAngle, _ = analysis.getLateralAngles( path, lat, corrBranchpts, rtpSkel) lateralT = [ avgLLength * ((xScale + yScale) / 2), float(lengthNodalRoot) * ((xScale + yScale) / 2), lBranchFreq, avgLatDiameter, slope, avgAngle, angRange, minangle, maxAngle, float(distToFirst) * ((xScale + yScale) / 2), medianD, meanD ] else: lateralT = [ 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan' ] else: lateralT = [ 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan' ] allLat.append(lateralT) if options[5][1] == '0': crownT = [ 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan' ] allCrown.append(crownT) io.setHomePath(os.getcwd())
def threadCrown(filepath): imgL = [] tipdiameter = float(options[8][1]) print io.getHomePath() os.chdir(io.getHomePath()) io.setHomePath('./Crown/') f = io.scanDir() for (counter, i) in enumerate(f): io.setFileName(os.path.basename(i)) io.setidIdx(imgID) print 'processing Crown file: ' + i xScale = allPara[counter][7] yScale = allPara[counter][8] analysis = Analysis.Analysis(io, (xScale + yScale) / 2) rtp = RootTipPaths.RootTipPaths(io, tp=tipdiameter) rtp.setTipDiaFilter(tipdiameter * (xScale + yScale) / 2) crownT = [] try: img = scipy.misc.imread(i, flatten=True) except: print 'Image not readable' img = -1 if len(img) > 0: seg = Segmentation.Segmentation(img, io) imgL = seg.label() print 'compute root profile' currT = time.time() rootDensity, medianWidth, maxWidth, D, DS, _, _, _, _ = analysis.getWidthOverHeight( imgL, xScale, yScale) print 'Mask traits computed ' + str(time.time() - currT) + 's' currT = time.time() skel = Skeleton.Skeleton(imgL) testSkel, testDia = skel.skel(imgL) print 'Medial axis computed ' + str(time.time() - currT) + 's' currT = time.time() path, skelGraph, stemDia, skelSize = seg.findThickestPath( testSkel, testDia, xScale, yScale) allPara[counter][10] = skelSize print 'Central path computed ' + str(time.time() - currT) + 's' print 'compute rtp skeleton' currT = time.time() rtpSkel, nrOfRTP, medianTipDiameter, meanDiameter, dia90, _, rtps, tips, _, _ = rtp.getRTPSkeleton( path, skelGraph, True) allPara[len(allPara) - 1][2] = seg.getFail() seg.setTips(tips) print 'RTP Skeleton computed ' + str(time.time() - currT) + 's' print 'compute symmetry' currT = time.time() vecSym = analysis.getSymmetry(rtps, rtpSkel) print 'Symmetry computed ' + str(time.time() - currT) + 's' if rtpSkel != -1: lat, corrBranchpts, _ = seg.findLaterals( rtps, rtpSkel, (xScale + yScale) / 2) try: print 'compute quantile angles' currT = time.time() a25, a50, a75, a90 = analysis.calculateAngleQuantiles( path, lat, corrBranchpts, rtpSkel) print 'angles computed in ' + str(time.time() - currT) + 's' except: raise a25 = ['nan'] a50 = ['nan'] a75 = ['nan'] a90 = ['nan'] print 'ERROR: No quantile angles calculated' try: print 'compute angles' currT = time.time() angRangeN, avgAngleN, minangleN, maxAngleN, anglesN = analysis.calculateAngles( path, lat, corrBranchpts, rtpSkel) print 'angles computed in ' + str(time.time() - currT) + 's' except: avgAngleN = 'nan' minangleN = 'nan' maxAngleN = 'nan' angRangeN = 'nan' anglesN = 'nan' print 'ERROR: No angles calculated' try: print 'compute RTA angles' currT = time.time() angRange, avgAngle, minangle, maxAngle, angles = analysis.getLateralAngles( path, lat, corrBranchpts, rtpSkel) print 'angles computed in ' + str(time.time() - currT) + 's' except: raise avgAngle = 'nan' minangle = 'nan' maxAngle = 'nan' angRange = 'nan' angles = 'nan' print 'ERROR: No RTA angles calculated' try: print 'compute diameter quantils' currT = time.time() d25, d50, d75, d90 = analysis.getDiameterQuantilesAlongSinglePath( path, rtpSkel) print 'diameters computed in ' + str(time.time() - currT) + 's' except: d25 = 'nan' d50 = 'nan' d75 = 'nan' d90 = 'nan' print 'ERROR: No quantile angles calculated' raise try: print 'compute dominant angles' currT = time.time() ang1, ang2 = analysis.findHistoPeaks(angles) print 'angles computed in ' + str(time.time() - currT) + 's' except: ang1 = 'nan' ang2 = 'nan' print 'ERROR: No dominant angles calculated' try: currT = time.time() ang25_1, ang25_2 = analysis.findHistoPeaks(a25) print 'angles computed in ' + str(time.time() - currT) + 's' except: ang25_1 = 'nan' ang25_2 = 'nan' print 'ERROR: No dominant angles25 calculated' try: currT = time.time() ang50_1, ang50_2 = analysis.findHistoPeaks(a50) print 'angles computed in ' + str(time.time() - currT) + 's' except: ang50_1 = 'nan' ang50_2 = 'nan' print 'ERROR: No dominant angles50 calculated' try: currT = time.time() ang75_1, ang75_2 = analysis.findHistoPeaks(a75) print 'angles computed in ' + str(time.time() - currT) + 's' except: ang75_1 = 'nan' ang75_2 = 'nan' print 'ERROR: No dominant angles75 calculated' try: currT = time.time() ang90_1, ang90_2 = analysis.findHistoPeaks(a90) print 'angles computed in ' + str(time.time() - currT) + 's' except: ang90_1 = 'nan' ang90_2 = 'nan' print 'ERROR: No dominant angles90 calculated' try: currT = time.time() angN_1, angN_2 = analysis.findHistoPeaks(anglesN) print 'angles computed in ' + str(time.time() - currT) + 's' except: angN_1 = 'nan' angN_2 = 'nan' print 'ERROR: No dominant angles90 calculated' crownT = [ stemDia, rootDensity, angRange, ang1, ang2, ang25_1, ang25_2, ang50_1, ang50_2, ang75_1, ang75_2, ang90_1, ang90_2, angN_1, angN_2, minangle, maxAngle, avgAngle, angRangeN, avgAngleN, minangleN, maxAngleN, nrOfRTP, medianTipDiameter, meanDiameter, dia90, medianWidth, maxWidth, D[0], D[1], D[2], D[3], D[4], D[5], D[6], D[7], D[8], DS[0], DS[1], DS[2], DS[3], DS[4], DS[5], DS[6], DS[7], DS[8], vecSym[0], vecSym[1], d25, d50, d75, d90 ] else: crownT = [ stemDia, rootDensity, 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', medianWidth, maxWidth, D[0], D[1], D[2], D[3], D[4], D[5], D[6], D[7], D[8], DS[0], DS[1], DS[2], DS[3], DS[4], DS[5], DS[6], DS[7], DS[8], vecSym[0], vecSym[1], d25, d50, d75, d90 ] if maxExRoot > 1: for i in range(maxExRoot): allCrown.append(crownT) else: allCrown.append(crownT) if options[4][1] == '0': lateralT = [ 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan' ] allLat.append(lateralT) io.setHomePath(os.getcwd())
from SceneCSV import * from VisualizerCSV import * from Skeleton import * from AnimatorCSV import * import sys, os import subprocess if __name__ == "__main__": joints = "./csv_dump.csv" screenShotFolder = "./save/" sk1 = Skeleton() sc1 = SceneCSV() an1 = AnimatorCSV(sk1, joints, screenShotFolder) viz = VisualizerCSV(sk1, sc1, an1, screenShotFolder)
def __init__(self, image_matrix, white=1, levels=4, init_shape=INIT_SKEL, maxQuant = 220): """ :param image_matrix: """ self.dnCount = 0 self.dnSum = 0. self.upCount = 0 self.upSum = 0. self.lenList = list() self.imin = image_matrix self.xmin = 0 self.ymin = 0 self.xmax = self.imin.shape[0] - 1 self.ymax = self.imin.shape[1] - 1 # whiten self.imin /= white self.imin += 255 - (255 // white) # processor count self.PROCESSORS = multiprocessing.cpu_count() if self.PROCESSORS > 1: self.PROCESSORS -= 1 # quantize self.centroids = Quantization.measCentroid(self.imin, levels) print("Centroids: ") print(self.centroids) levels = min(levels, len(self.centroids)) levels = max(2, levels) nq = np.array([[x * maxQuant / (levels - 1)] for x in range(0, levels)]) print(nq) self.imin = Quantization.quantMatrix(self.imin, nq, self.centroids) plt.imshow(self.imin, cmap=cm.gray) plt.savefig("figStartOrig.png") plt.clf() # self.R0_B = self.density(nq[-1][0]) # Initial segment if init_shape == self.INIT_MOORE: moore = [] m = [] n = 1 << 7 for i in range(0, n ** 2): x, y = Hilbert.d2xy(n, i, True) m.append((x, y)) moore.append(((self.imin.shape[0] * x) / (n - 1), (self.imin.shape[1] * y) / (n - 1))) ''' Ordinarily, the moore curve starts in the middle of one edge. Rotate the moore graph to start in the middle ''' m2q = len(moore) // 4 moore2 = moore[m2q:] moore2.extend(moore[:m2q]) ''' Add the first and last point to return to start ''' ptAlpha = np.multiply(np.array(self.imin.shape), 0.5) moore2.append(tuple(ptAlpha)) moore2.insert(0, tuple(ptAlpha)) moore3 = [(0.95 * x + 0.025 * self.imin.shape[0], 0.95 * y + 0.025 * self.imin.shape[1]) for x, y in moore2] self.maze_path = np.array(moore3) self.maze_path = TSPopt.simplify(self.maze_path) for i in range(10): self.resampling() self.maze_path = TSPopt.simplify(self.maze_path) while True: delta, seg1 = TSPopt.threeOptLocal(self.maze_path, 40) self.maze_path = seg1 if delta == 0.: break for i in range(10): self.resampling() ''' Have to add a brownian to thois because when you do the resample, you could end up with points on the same line, which will lead to a divb0 issue. ''' brownian = self.brownian() self.maze_path = np.add(self.maze_path, brownian) self.plotMazeImage("figStartMoore.png",superimpose=True) elif init_shape == self.INIT_FASS: """ FASS is for Filling, self-Avoiding, Simple, and self-Similar. This is one instance of a FASS system. This one starts in the center, which is why it is advantageous for us. """ import LSystem fass2 = LSystem.LSystem(axiom="FX", rules=[('X','Y-LFL-FRF-LFLFL-FRFR+F'), ('Y','X+RFR+FLF+RFRFR+FLFL-F'), ('L','LF+RFR+FL-F-LFLFL-FRFR+'), ('R','-LFLF+RFRFR+F+RF-LFL-FR')], angle = 90) fass2.iterate(5) path1=np.array(fass2.segment(initialpt=[0.0,0.0], d=1.0)) dim = path1.max() - path1.min() path2 = list() path1min = path1.min() for pt in path1: path2.append(((self.imin.shape[0] * (pt[0]-path1min)) / (dim - 1), (self.imin.shape[1] * (pt[1]-path1min)) / (dim - 1))) path3 = [(0.95 * x + 0.025 * self.imin.shape[0], 0.95 * y + 0.025 * self.imin.shape[1]) for x, y in path2] self.maze_path = path3 self.plotMazeImage("figStartFass0.png",superimpose=True) self.maze_path = TSPopt.simplify(self.maze_path) for _ in range(10): self.resampling() self.maze_path = TSPopt.simplify(self.maze_path) while True: delta, seg1 = TSPopt.threeOptLocal(self.maze_path, 40) self.maze_path = seg1 if delta == 0.: break for i in range(10): self.resampling() self.plotMazeImage("figStartFass.png",superimpose=True) elif init_shape == self.INIT_DIAG: # simple diagonal segListEnd = tuple([x - 1 for x in self.imin.shape]) self.maze_path = list() for i in range(20): self.maze_path.append((int(i*segListEnd[0]/20), int(i*segListEnd[1]/20))) self.maze_path.append(segListEnd) self.maze_path = np.array(self.maze_path) elif init_shape == self.INIT_SKEL: # use skeleton to cover most of dark image (>128) b = np.array([[0.], [128.]]) q = np.array([[0.], [1.]]) blacks = Quantization.quantMatrix(self.imin, q, b) skeleton = Skeleton.Skeleton(blacks) skeleton.segments.addInitialStartPt() skeleton.euclidMstOrder() skeleton.segments.concatSegments() oneD = skeleton.segments.segmentList.flatten() self.maze_path = np.reshape(oneD, (-1, 2)) brownian = self.brownian() self.maze_path = np.add(self.maze_path, brownian) self.maze_path = TSPopt.simplify(self.maze_path) size = 60 while True: delta, seg1 = TSPopt.threeOptLocal(self.maze_path, size) self.maze_path = seg1 if delta == 0.: break size = max(5,size-5) self.plotMazeImage("figStartSkeleton.png",superimpose=True) self.seg = Segments.Segments() factor = 0.5 delta = 0.0 self.bndry_xmax = self.xmax + factor * self.R0_B - delta self.bndry_ymax = self.ymax + factor * self.R0_B - delta self.bndry_xmin = self.xmin - factor * self.R0_B + delta self.bndry_ymin = self.ymin - factor * self.R0_B + delta pt_00 = (self.bndry_xmin, self.bndry_ymin) pt_01 = (self.bndry_xmin, self.bndry_ymax) pt_11 = (self.bndry_xmax, self.bndry_ymax) pt_10 = (self.bndry_xmax, self.bndry_ymin) self.boundary_seg = [pt_00, pt_01, pt_11, pt_10, pt_00] self.minDist = sys.float_info.max
def runGame(): active_sprite_list = pygame.sprite.Group() water_block_list = pygame.sprite.Group() trap_list = pygame.sprite.Group() skeleton_list = pygame.sprite.Group() player = Player(CELLWIDTH, CELLHEIGHT, CELLSIZE) active_sprite_list.add(player) chest = Chest(CELLWIDTH, CELLHEIGHT, CELLSIZE) confirmPlacement(chest, active_sprite_list, trap_list, 2) skeleton1 = Skeleton(CELLWIDTH, CELLHEIGHT, CELLSIZE) confirmPlacement(skeleton1, active_sprite_list, trap_list, 7) skeleton_list.add(skeleton1) # Initialise some random water positions for i in range(4): initialWaterBlock = WaterBlock(CELLWIDTH, CELLHEIGHT, CELLSIZE) confirmPlacement(initialWaterBlock, active_sprite_list, trap_list, 4) water_block_list.add(initialWaterBlock) # Add more water blocks adjacent to these for i in range(8): closePlacement(WaterBlock(CELLWIDTH, CELLHEIGHT, CELLSIZE), water_block_list, active_sprite_list, trap_list) while True: # main game loop for event in pygame.event.get(): # event handling loop if event.type == QUIT: terminate() elif event.type == KEYDOWN: if event.key == K_ESCAPE: terminate() else: if player.move(event.key, chest, active_sprite_list, trap_list): for skeleton in skeleton_list: skeleton.count() # One unit of time passes if chest.isCollected(): # 1/3 chance of nothing, 1/3 chance of treasure, 1/3 chance of getting traps numberOfNewTraps = random.choice([-1, -1, 0, 0, 2, 3]) if numberOfNewTraps == 0: player.incScore() else: player.incTraps(numberOfNewTraps) active_sprite_list.remove(chest) chest = Chest(CELLWIDTH, CELLHEIGHT, CELLSIZE) # set a new chest somewhere confirmPlacement(chest, active_sprite_list, trap_list, 4) randomNumber = random.random() if randomNumber < 0.33 or (randomNumber < 0.66 and len(skeleton_list) == 0): newSkeleton = Skeleton(CELLWIDTH, CELLHEIGHT, CELLSIZE) confirmPlacement(newSkeleton, active_sprite_list, trap_list, 3) skeleton_list.add(newSkeleton) for skeleton in skeleton_list: if skeleton.getClock() <= 0: drawToScreen(active_sprite_list, trap_list, player, skeleton_list) for i in range(3 - skeleton.getClock()): skeleton.move(player, active_sprite_list, trap_list) time.sleep(0.2) drawToScreen(active_sprite_list, trap_list, player, skeleton_list) if skeleton.isTrapHit(): active_sprite_list.remove(skeleton) skeleton_list.remove(skeleton) skeleton = None break if skeleton.isPlayerHit(): showGameOverScreen() if skeleton is not None: skeleton.resetClock() # Reset clock back to 3 drawToScreen(active_sprite_list, trap_list, player, skeleton_list)
from Scene import * from Visualizer import * from Skeleton import * from AnimatorCOCO import * # from AnimatorTemporal import * # from AnimatorCSV import * import sys, os import subprocess import numpy as np if __name__ == "__main__": cs = "tcp://localhost:5563" # os.system('mkdir -p ../MPII' + sys.argv[1]) c = b"B" sk1 = [Skeleton()] for i in range(int(sys.argv[1]) - 1): sk1 += [Skeleton()] sc1 = Scene() # an1 = AnimatorCSV(sk1, cs, c) an1 = AnimatorCSV(sk1, cs, c) if sys.argv[1]: viz = Visualizer(sk1, sc1, an1, sys.argv[1]) else: viz = Visualizer(sk1, sc1, an1, None)