%run NBCONFIG.ipynb from pyparty import Canvas c=Canvas() print type(c.image), type(c.particles) print c.image.shape print c.shape print c.particles c.available() c.available('simple') c.add('circle') c.show() print c.particles # Randomize the centers (but avoid edges) from random import randint def cran(): return (randint(100, 400), randint(100,400)) c.add('circle', name='top_right', radius=75, center=(400,100), color='y') c.add('ellipse', name='bottom_left', center=(50,400), color='green', phi=0) c.add('circle', name='topleft_corner', radius=100, center=(0,0), color=(20,30,50) ) c.add('circle', name='off_image', radius=50, center=(900,200), color='teal') # Randomized centers c.add('line', color='purple', length=200, width=10, center=cran()) c.add('square', length=50, color='magenta', phi=10, center=cran()) c.add('square', color='honeydew', length=50, center=cran()) c.add('triangle', color='orange', length=50, center=cran()) print c.particles STORED_PARTICLES = c.particles c.area c.show(title='Example Plot'); from skimage.data import moon, lena MOON = moon() LENA = lena() c.background = MOON c.show(annotate=True); print c.particles BLACK_BACKGROUND = np.zeros( (200,200) ) c.background = BLACK_BACKGROUND c.show(); from pyparty import splot c.reset_background() ax1, ax2, ax3, ax4 = splot(2,2) c.show(ax1, title='(%s, %s)'%c.rez) # same as setting c.background = BLACK_BACKGROUND c2 = c.set_bg(BLACK_BACKGROUND, keepres=False) c2.show(ax2, title='(%s, %s)'%c2.rez) # black background; keep original res c3 = c.set_bg(BLACK_BACKGROUND, keepres=True) c3.show(ax3, title='(%s, %s)'%c3.rez) # set background to entirely different res c4 = c.set_bg(BLACK_BACKGROUND, keepres=(300,600)) c4.show(ax4, title='(%s, %s)'%c4.rez); ax1, ax2 = splot(1,2) c.reset_background() c.rez = (700,700) c.background='orange' c.show(ax1, title="Resolution set first") c.reset_background() c.background='orange' c.rez = (700,700) c.show(ax2, title="BG reset first"); c.background = LENA ax1, ax2 = splot(1,2) c.show(ax1, title='Full lena') c2 = c.zoom_bg(200,200,400,400) c2.show(ax2, title='Sup girl'); from pyparty import crop coords = (200,200,400,400) #Must be in list/tuple imshow(crop( c.image, coords ) ); from pyparty.data import lena_who c.background = lena_who() c.show() print 'newshape = ', c.shape c.rez = (1024, 1024) print 'newshape = ', c.shape c.show(); c.reset_background() c.show(); c.background=MOON ax1, ax2, ax3, ax4 = splot(2, 2, figsize = (10,10)) c.show(ax1, title='ax1') c.show(ax2, 'Blues', title='ax2 (colomapped)') c.show(ax3, 'Blues', bgonly=True, title='ax3 (only bg colormapped)'); #Change background color to pink c.background = 'pink' c.show(ax4, title='ax4 (pure background color)'); ax1, ax2 = splot(1,2) c.show(ax1, gcolor='r') c.grid.xdiv=10 #Ten divisions total c.show(ax2, gcolor='black') print c.grid; ax1, ax2, ax3, ax4 = splot(2,2) c.background = LENA c.show( ax1, alpha=.5, gcolor='white', title="Alpha applied to image") c.patchshow(ax2, alpha=.5, gcolor='white', edgecolor='r', title="Alpha on particles") c.show( ax3, 'Blues_r', bgonly=True, gunder=True, gcolor='white', title="cmap applied only to bg") c.patchshow(ax4, 'Blues_r', alpha=.7, bgonly=False, gunder=True, gcolor='white', gstyle='--', edgecolor='r', hatch='*', title='CMAP also applied to particles'); from skimage.filter import gaussian_filter, sobel from skimage.color import rgb2gray ax1, ax2 = splot(1,2) # These are ndimages, not canvases, so use imshow c_filtered = gaussian_filter(c.image, 10, multichannel=True) # Sobel requires gray image; we convert from rgb c_sobel = sobel(rgb2gray(c_filtered)) ax1.imshow(c_filtered) ax2.imshow(c_sobel, 'gray') ax1.set_title("Gaussian blur") ax2.set_title('Sobel gradient of blur'); type(c), type(c_filtered) from pyparty import showim ax1, ax2 = splot(1,2) showim(c_filtered, ax1, title="Blur again") showim(c_sobel, ax2, 'gray', title='Sobel again'); from pyparty.descriptors.api import ALL_DESCRIPTORS, CUSTOM_DESCRIPTORS ', '.join(sorted(ALL_DESCRIPTORS)) CUSTOM_DESCRIPTORS c.area areasorted = c.sortby('area') print 'No sorting:\n', c[0:4], '\n' print 'Area sorting:\n', areasorted[0:4] print '(in)\n', c.pin print '\n(edge)\n',c.pedge print '\n(out)\n', c.pout c.plist[0].color = 'r' c.show(); c.reset_background() def red_or_blue(p): if p.area > 2000: p.color = 'red' else: p.color = (0,0,1) #blue return p c_map = c.pmap(red_or_blue) c_map.show(); c.particles = STORED_PARTICLES def inflection(p): try: cx, cy = p.cx, p.cy p.cx, p.cy = cy, cx except Exception: return p return p c_map.pmap(inflection, inplace=True) c_map.show(); def half_pixels(pixel): """ Reduce the pixel value by half and square""" return (pixel/2.0)**2 phalf = c.pixelmap(half_pixels) showim(phalf); print c.particles c.clear_canvas() print c.particles c.show(); # Get our original particles back c.particles=STORED_PARTICLES c.show(); print c[0], '\n' print c[1:3] print c[0,1,2] print c['circle_0'], '\n' print c[0,'top_right', 1] print c.idx('circle_0'), c.idx('line_0', 'circle_0', 'top_right') print [c.area > 5000] print c[c.area > 5000] print c[(c.eccentricity > .1) & (c.ptype != 'ellipse')] c1 = Canvas() c2 = Canvas() for idx, i in enumerate(range(40,50)): j = (i * idx) if idx != 0: c1.add('circle', radius=i, center=(j,j), color=(.1*idx, 0, 0)) c2.add('dimer', radius_1=i/2, center=(512-j, j), \ overlap=.3, phi=15*idx, color=(0, 0, 1.0 -.1*idx)) ax1, ax2 = splot(1, 2) c1.show(ax1, title='c1') c2.show(ax2, title='c2'); c2.background = MOON c12 = c1 + c2 c21 = c2 + c1 ax1, ax2 = splot(1,2) c12.show(ax1, title="c1 + c2 (c2's background)") c21.show(ax2, title="c2 + c1 (c1's background)"); from pyparty import concat_canvas c12_alter = concat_canvas(c1, c2, alternate=True) c12_alter.show(); c1.particles = c1.particles + c2.particles c1.show(title='Addition of only particles (c2 appears on top)'); #Subtract first 3 particles from first 5 print c1.particles[0:5] - c1.particles[0:3] from skimage.color import gray2rgb from pyparty import any2rgb ax1, ax2, ax3 = splot(1,3, figsize=(10,7)) ax1.imshow( c1.image - gray2rgb(MOON), plt.cm.gray ) ax2.imshow( c1.image - any2rgb(MOON), plt.cm.gray ) ax3.imshow( moon(), plt.cm.gray ); # Cut LENA in half ; keep int version FLOAT = LENA.astype('float') HALFFLOAT = LENA/2.0 HALFINT = HALFFLOAT.astype('int32') HALFUINT = HALFFLOAT.astype('uint8') ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8, ax9, ax10 = \ splot(2,5, figsize=(10,5) ) showim(LENA, ax1, title='Int (max=255)') showim(FLOAT, ax2, title='Float (max=255.0)') showim(HALFINT, ax3, title='Int /2 (max=128)') showim(HALFFLOAT, ax4, title='Flat / 2 (max=128.0)') showim(HALFUINT, ax5, title='UInt / 2 (max=128)') showim(any2rgb(LENA), ax6) showim(any2rgb(FLOAT), ax7) showim(any2rgb(HALFINT), ax8) showim(any2rgb(HALFFLOAT), ax9) showim(any2rgb(HALFUINT), ax10); c.clear_particles() c.background=HALFINT ax1, ax2 = splot(1,2) c.show(ax1, title='half lena rgb') c.show(ax2, plt.cm.Blues, title='half lena gray / 1 channel'); from skimage import img_as_bool ax1, ax2 = splot(1, 2) c1.show('pbinary', ax1, gcolor='r', title="Masked area is %.1f percent" % ( c1.pixarea*100 )); c1.show('pbinary_r', ax2, gcolor='b', title='inverted'); p_area = sum(p.area for p in c1.particles) print round( 100. * (p_area / c1.pixcount), 1), 'percent' ax1, ax2, ax3, ax4, ax5, ax6 = splot(2, 3) def colormap(p, color): p.color = color return p c.reset_background() c.pmap(colormap, 'r').show(ax1, title='colorstring') c.pmap(colormap, '#00FF00').show(ax2, title='hex color') #green c.pmap(colormap, (0, 0, 1)).show(ax3, title='rgb tuple') #blue # These become the same color (.5, .5, .5) ... c.pmap(colormap, .5).show(ax4, title='0.5 --> (.5, .5, .5)') c.pmap(colormap, 128).show(ax5, title='128 --> (.5, .5, .5)') c.pmap(colormap, (255, 255, 1)).show(ax6, title='tuple (normalized)'); from pyparty import to_normrgb as rgb AXES = splot(1, 3) # Return (r,g,b) corresponding to yellow ry, gy, by = rgb('yellow') for idx, alpha in enumerate( (.3, .7, 1.0) ): shade = ry*alpha, gy*alpha, by*alpha c.pmap(colormap, shade).show(AXES[idx], title='Color %s'% str(shade)) ax1, ax2, ax3, ax4, ax5, ax6 = splot(2, 3, figsize=(10,6)) c.particles = STORED_PARTICLES c.background = LENA showim(c.image, ax1) showim(c.grayimage, ax2, plt.cm.gray) showim(c.binaryimage, ax3, plt.cm.gray) showim(c.background, ax4) showim(c.graybackground, ax5, plt.cm.gray) showim(c.binarybackground, ax6, plt.cm.gray); ax1, ax2, ax3, ax4 = splot(2,2, figsize=(8,8)) c.set_threshfcn('img2bool' ) showim(c.binaryimage, ax1, 'gray', title='img2bool (with particles)') c.set_threshfcn('adaptive', block_size=199, method='gaussian') showim(c.binarybackground, ax2, 'gray', title='local adaptive') c.set_threshfcn('otsu', invert=True) showim(c.binarybackground, ax3, 'gray', title='otsu inverted') c.set_threshfcn('double', nlow=100, nhigh=150) showim(c.binarybackground, ax4, 'gray', title='low-high range (100-150)'); from pyparty.tools.thresholding import invertible @invertible def foo_thresh(grayimage, **kwargs): return (grayimage >= 100) c.set_threshfcn(foo_thresh, invert=True) showim(c.binarybackground, 'gray', title='"%s"function (inverted)'%c.threshfcn);