from __future__ import division %run NBCONFIG.ipynb c=Canvas() c.available() c.add('dimer', overlap=.3) #Default to image center (256, 256) c.add('dimer', center=(100,100), phi=25.) c.add('circle', center=(100,400), phi=60) c.add('ellipse', center=(400,100)) c.add('triangle', center=(200,300), length=50) c.add('ellipse', center=(400, 400), yradius=5, xradius=50) print c.particles c.show(); from skimage.morphology import label ax1, ax2, ax3 = splot(1, 3, figsize=(10,8)) # We can access grayimage immediately from canvas labels = label(c.grayimage) showim(c.image, ax1, title='RGB') showim(c.grayimage, 'gray', ax2, title='Grayscale') showim(labels, 'jet', ax3, title='skimage.morphology labels'); print labels.shape print np.unique(labels) labels print ' '.join(['INDEX', 'NAME', 'CSTART', 'CEND', 'CENTER'])+'\n' for idx, p in enumerate(c.particles): print idx, p.name, labels[p.rr_cc][0:3], labels[p.rr_cc][-3:], p.center ax1, ax2 = splot(1,2) c.show('pbinary', ax1, title='pbinary') showim(c.binaryimage, ax2, 'gray', title='%s' % c.threshfcn); print 'Canvas thresholding function is "%s()"' % c.threshfcn clab = c.from_labels(exclude='black') clab.show(title='Particles from labels') clab.particles[0:5] ax1, ax2, ax3 = splot(1, 3, figsize=(12,8)) clab = c.from_labels(prefix='FOOPREFIX', colorbynum=True, exclude='black') clab.show(ax1, title='Black to Yellow Ascending') clab.show('jet', ax2, title="1-Channel coloring") clab.background='black' clab.show('jet', ax3, title='Original Coloring'); clab.particles[0:5] len(c), len(clab), 'Dimer labeled as 2 particles' clab.sortby('eccentricity', inplace=True, ascending=False) clab.eccentricity from pyparty.data import lena_who ax1, ax2 = splot(1,2) c=Canvas(background=lena_who()) c.show('pbinary', ax1, title='No particles; pbinary is blank'); c.set_threshfcn('adaptive', block_size=199, method='gaussian') showim(c.binarybackground, ax2, 'gray', title='Adaptive threshold'); #This cell may take a bit to load ax1, ax2, ax3, ax4 = splot(2,2, figsize=(15,15)) c1=c.from_labels(pbinary=False) #background = None, neighbors = 4 c2=c.from_labels(neighbors=8, pbinary=False) #background = none c3=c.from_labels(exclude=0, pbinary=False) # neighbors = 4 c4=c.from_labels(exclude=1, pbinary=False) # neighbors = 4 c1.show(ax1, title='%s particles; no background; 4 neighbors' % len(c1)) c2.show(ax2, title='%s particles; no background; 8 neighbors' % len(c2)) c3.show(ax3, title='%s particles; black bg cut; 4 neighbors' % len(c3)) c4.show(ax4, title='%s particles; white bg cut; 4 neighbors' % len(c4)); ax1, ax2 = splot(1,2, figsize=(15,15)) c1=c.from_labels(pbinary=False, exclude='black', bgout='white') c2=c.from_labels(pbinary=False, exclude='black', bgout=c.graybackground) c1.show(ax1, title='Labels on a white background') c2.show(ax2, title='Labels on a gray image'); ax1, ax2 = splot(1,2, figsize=(15,15)) c1=c.from_labels(exclude='black', pmax=1000, pbinary=False) c2=c.from_labels(exclude='black', pmin=5000, pbinary=False) c1.show(ax1, title='Small labels (< 1000 pixels)') c2.show(ax2, title='Large labels (> 5000 pixels)'); #Sort particles by largest area; keep top 3 c2.sortby('area', inplace=True, ascending=False) #large to small c2.particles=c2.particles[0:3] # To set attributes on indivdual particles, access plist, not particles c2.plist[0].color = 'r' c2.plist[1].color = 'b' c2.plist[2].color = 'w' #Superimpose on a black background c2.background = 'black' c2.show(); def rotate(p): p.phi = p.phi + 45.0 return p c2.pmap(rotate) c2.show();