def contour_from_image(self, img, threshold = 75): cntrs = wgeo.contours_from_image(img, sigma = 1, absolute_threshold = threshold, verbose = False, save = None); nc = len(cntrs); if nc == 1: return ir.resample(cntrs[0], self.contour_size); else: return np.vstack([ir.resample(cntrs[0], self.contour_size-self.contour_inner), ir.resample(cntrs[1], self.contour_inner)]);
def cost_from_image(model, image, nsamples=100, nparameter=50): cntrs = wgeo.contours_from_image(image, sigma=1, absolute_threshold=None, threshold_factor=0.9, verbose=False, save=None) contour = Curve(resample(cntrs[0], nsamples), nparameter=nparameter) return cost_from_countour(model, contour)
def contour_from_image(self, img, threshold = 75): cntrs = wgeo.contours_from_image(img, sigma = 1, absolute_threshold = threshold, verbose = False, save = None); nc = len(cntrs); if nc == 1: cntrs = (ir.resample(cntrs[0], self.contour_size),); else: cntrs = (ir.resample(cntrs[0], self.contour_size-self.contour_inner), ir.resample(cntrs[1], self.contour_inner)); #calculate normals nrmls = [wgeo.normals_from_contour_discrete(c) for c in cntrs]; cntrs = tuple([c[:-1] for c in cntrs]); return np.vstack(cntrs), np.vstack(nrmls)
@author: ckirst """ #%% import worm.geometry as wgeo reload(wgeo) import interpolation.resampling as ir threshold = 75 cntrs = wgeo.contours_from_image(imgt, sigma=1, absolute_threshold=threshold, verbose=False, save=None) nc = len(cntrs) contour_size = 100 contour_inner = 20 if nc == 1: cntrs = (ir.resample(cntrs[0], contour_size), ) else: cntrs = (ir.resample(cntrs[0], contour_size - contour_inner), ir.resample(cntrs[1], contour_inner)) #calculate normals nrmls = [wgeo.normals_from_contour_discrete(c) for c in cntrs]
def test(): import numpy as np import tensorflow as tf import matplotlib.pyplot as plt; import worm.model as wm; import worm.machine_vision_2 as wmv import worm.geometry as wgeo reload(wgeo) reload(wmv) ### Prepare optimization task # work shape w = wm.WormModel(length = 80); ig = wmv.ImageGenerator(); net = wmv.WormVision(model = w, images = ig); ig.t_counter = 500000 + 25620 - 5; ig.wid_counter = 0; imgs, cntrs = ig.get_batch(nimages = 1); img = imgs[0,:,:,0]; w.from_image(img) plt.figure(20); plt.clf(); wgeo.skeleton_from_image_discrete(img, verbose = True, with_head_tail=True) w.plot(); # target ig.t_counter = 500000 + 25620 - 1; ig.wid_counter = 0; #imgs, skels, valids, hts, htvs = ig.get_batch(nimages = 1); imgs, skels, hts = ig.get_batch(nimages = 1); imgt = imgs[0,:,:,0]; plt.figure(21); plt.clf(); wgeo.skeleton_from_image_discrete(imgt, verbose = True, with_head_tail=True, absolute_threshold=75) w.plot() ### Cost functions wb = 2; ws = 1.0; par = net.create_variable(net.output.get_shape()); skel = tf.constant(skels, "float32"); cost = net.create_cost(par, skel, weight_bending = wb, weight_spacing = ws); cost_bend = net.create_cost_bending(par); cost_spacing = net.create_cost_spacing(par); cost_dist = net.create_cost_soft_min_distance(par, skel); grad = tf.gradients(cost, [par]); ### Tensoroflow ### Session sess = None; sess = tf.InteractiveSession() init = tf.initialize_all_variables(); sess.run(init) assign_op = par.assign(w.center[None,:,:]); sess.run(assign_op) ### Compare costs cb = wb * sess.run(cost_bend); cs = ws * sess.run(cost_spacing); cd = sess.run(cost_dist); c = sess.run(cost); print 'Costs: full: %f; dist: %f; bend: %f; spacing :%f' % (c, cd, cb, cs); ### Manual Gradient descent p1 = w.center; sg = .75; nsteps = 100; for i in range(nsteps): sess.run(par.assign(p1[None,:,:])); g = sess.run(grad)[0][0]; p1 = p1 - sg * g / np.sqrt(np.sum(g*g)); plt.figure(10); plt.clf(); #plt.subplot(1,2,1) #w.plot(image = img); w.center = p1; #plt.subplot(1,2,2); w.plot(image= imgt); plt.title('cost %f' % sess.run(cost)); plt.draw(); plt.pause(0.05); ### Tensorflow optimization #trainer = tf.train.AdadeltaOptimizer().minimize(cost, var_list=[par]); trainer = tf.train.GradientDescentOptimizer(learning_rate=2.0).minimize(cost); init = tf.initialize_all_variables(); sess.run(init) sess.run(assign_op) nsteps = 1000; for i in range(nsteps): trainer.run(session = sess, feed_dict={}); p1 = par.eval(session = sess); if i%10 == 0: plt.figure(10); plt.clf(); w.center = p1[0]; w.plot(image= imgt); plt.scatter(skels[0,:,0], skels[0,:,1], c= 'm') plt.xlim(0,151); plt.ylim(0,151) plt.draw(); plt.pause(0.1); import shapely.geometry as geom cntrs = wgeo.contours_from_image(imgt) poly = geom.Polygon(cntrs[0], [cntrs[i] for i in range(1,len(cntrs))]); poly2 = poly.buffer(-3) bdr = poly.boundary; from descartes.patch import PolygonPatch patch = PolygonPatch(poly, facecolor = 'b', edgecolor='k', alpha=0.5, zorder=2) patch2= PolygonPatch(poly2, facecolor = 'r', edgecolor='k', alpha=0.5, zorder=2) fig = plt.figure(28); ax = fig.add_subplot(111) ax.add_patch(patch) ax.add_patch(patch2) plt.xlim(0,151); plt.ylim(1,151) plt.show()
### Gradient descent t0 = 500000 img = exp.load_img(wid=80, t=t0) #imgs = filters.gaussian_filter(np.asarray(img, float), 1.0); w = wm.WormModel(npoints=10) w.from_image(img, verbose=False) w.move_forward(0.1) w.rotate(0.1) contours = wgeo.contours_from_image(img, sigma=1, absolute_threshold=None, threshold_factor=0.9, verbose=False, save=None) contour = Curve(resample_curve(contours[0], 100), nparameter=50) head_tail_xy = wgeo.head_tail_from_contour_discrete(contour, ncontour=all, delta=0.3, smooth=1.0, with_index=False, verbose=True, save=None, image=imgs) plt.figure(1) plt.clf()
m = wm.WormModel(npoints=31) m.from_image(img, verbose=True) l, r = m.shape() plt.subplot(2, 3, 5) plt.plot(l[:10, 0], l[:10, 1], 'w') #%% img2 = exp.load_img(wid=80, t=524273) plt.figure(2) plt.clf() cont = wg.contours_from_image(img2, verbose=True) cout = cont[0] if len(cont) > 1: cin = cont[1] else: cin = [] plt.figure(5) plt.clf() ht, idx = wg.head_tail_from_contour_discrete(cout, with_index=True, verbose=True, image=img2) i = idx[0]
def test(): import numpy as np import tensorflow as tf import matplotlib.pyplot as plt import worm.model as wm import worm.machine_vision_2 as wmv import worm.geometry as wgeo reload(wgeo) reload(wmv) ### Prepare optimization task # work shape w = wm.WormModel(length=80) ig = wmv.ImageGenerator() net = wmv.WormVision(model=w, images=ig) ig.t_counter = 500000 + 25620 - 5 ig.wid_counter = 0 imgs, cntrs = ig.get_batch(nimages=1) img = imgs[0, :, :, 0] w.from_image(img) plt.figure(20) plt.clf() wgeo.skeleton_from_image_discrete(img, verbose=True, with_head_tail=True) w.plot() # target ig.t_counter = 500000 + 25620 - 1 ig.wid_counter = 0 #imgs, skels, valids, hts, htvs = ig.get_batch(nimages = 1); imgs, skels, hts = ig.get_batch(nimages=1) imgt = imgs[0, :, :, 0] plt.figure(21) plt.clf() wgeo.skeleton_from_image_discrete(imgt, verbose=True, with_head_tail=True, absolute_threshold=75) w.plot() ### Cost functions wb = 2 ws = 1.0 par = net.create_variable(net.output.get_shape()) skel = tf.constant(skels, "float32") cost = net.create_cost(par, skel, weight_bending=wb, weight_spacing=ws) cost_bend = net.create_cost_bending(par) cost_spacing = net.create_cost_spacing(par) cost_dist = net.create_cost_soft_min_distance(par, skel) grad = tf.gradients(cost, [par]) ### Tensoroflow ### Session sess = None sess = tf.InteractiveSession() init = tf.initialize_all_variables() sess.run(init) assign_op = par.assign(w.center[None, :, :]) sess.run(assign_op) ### Compare costs cb = wb * sess.run(cost_bend) cs = ws * sess.run(cost_spacing) cd = sess.run(cost_dist) c = sess.run(cost) print 'Costs: full: %f; dist: %f; bend: %f; spacing :%f' % (c, cd, cb, cs) ### Manual Gradient descent p1 = w.center sg = .75 nsteps = 100 for i in range(nsteps): sess.run(par.assign(p1[None, :, :])) g = sess.run(grad)[0][0] p1 = p1 - sg * g / np.sqrt(np.sum(g * g)) plt.figure(10) plt.clf() #plt.subplot(1,2,1) #w.plot(image = img); w.center = p1 #plt.subplot(1,2,2); w.plot(image=imgt) plt.title('cost %f' % sess.run(cost)) plt.draw() plt.pause(0.05) ### Tensorflow optimization #trainer = tf.train.AdadeltaOptimizer().minimize(cost, var_list=[par]); trainer = tf.train.GradientDescentOptimizer( learning_rate=2.0).minimize(cost) init = tf.initialize_all_variables() sess.run(init) sess.run(assign_op) nsteps = 1000 for i in range(nsteps): trainer.run(session=sess, feed_dict={}) p1 = par.eval(session=sess) if i % 10 == 0: plt.figure(10) plt.clf() w.center = p1[0] w.plot(image=imgt) plt.scatter(skels[0, :, 0], skels[0, :, 1], c='m') plt.xlim(0, 151) plt.ylim(0, 151) plt.draw() plt.pause(0.1) import shapely.geometry as geom cntrs = wgeo.contours_from_image(imgt) poly = geom.Polygon(cntrs[0], [cntrs[i] for i in range(1, len(cntrs))]) poly2 = poly.buffer(-3) bdr = poly.boundary from descartes.patch import PolygonPatch patch = PolygonPatch(poly, facecolor='b', edgecolor='k', alpha=0.5, zorder=2) patch2 = PolygonPatch(poly2, facecolor='r', edgecolor='k', alpha=0.5, zorder=2) fig = plt.figure(28) ax = fig.add_subplot(111) ax.add_patch(patch) ax.add_patch(patch2) plt.xlim(0, 151) plt.ylim(1, 151) plt.show()
fig.savefig('pipeline_input.png', facecolor = 'white') #smooth fig = plt.figure(2); plt.clf(); img = exp.load_img(t= t0, sigma=1.0) plt.imshow(img, cmap = 'gray', interpolation = 'none') plt.axis('off') fig.savefig('pipeline_sigma.png', facecolor = 'white') #contour cts = wgeo.contours_from_image(img, verbose= False) img = exp.load_img(t= t0, sigma=1.0) fig = plt.figure(3); plt.clf(); plt.imshow(img, cmap = 'gray', interpolation = 'none') for c in cts: plt.plot(c[:,0], c[:,1]) plt.xlim(0, 151); plt.ylim(0,151) plt.axis('off') fig.savefig('pipeline_contour.png', facecolor = 'white') #curvature import numpy as np
plt.figure(3); plt.clf(); wgeo.center_from_image_skeleton(img, verbose= True, npoints=20, absolute_threshold = None, threshold_factor = 0.85) w = wm.WormModel(npoints = 15); plt.figure(1); plt.clf(); w.from_image(img, verbose = True, nneighbours=1); plt.figure(2); plt.clf(); w.plot(image = img); w.move_forward(0.1); w.rotate(0.1); contours = wgeo.contours_from_image(imgs, sigma = 1, absolute_threshold = None, threshold_factor = 0.9, verbose = False, save = None); contour = Curve(resample_curve(contours[0], 100), nparameter = 50); head_tail_xy = wgeo.head_tail_from_contour_discrete(contour, ncontour = all, delta = 0.3, smooth = 1.0, with_index = False, verbose = True, save = None, image = imgs); plt.figure(1); plt.clf(); w.plot(image = imgs) contour.plot(with_points=False); par = w.get_parameter() npar = par.shape[0] eps = 0.1 * np.ones(npar); ik = w.center.shape[0]; eps[:ik] = 0.5;
def test(): import numpy as np import tensorflow as tf import matplotlib.pyplot as plt; import worm.model as wm; import worm.machine_vision_3 as wmv import worm.geometry as wgeo reload(wgeo) reload(wmv) ### Prepare optimization task # work shape w = wm.WormModel(length = 80); ig = wmv.ImageGenerator(); net = wmv.WormVision(model = w, images = ig); ig.t_counter = 500000 + 25620 - 5; ig.wid_counter = 0; imgs, cntrs = ig.get_batch(nimages = 1); img = imgs[0,:,:,0]; cntr = cntrs[0]; w.from_image(img) plt.figure(20); plt.clf(); #wgeo.skeleton_from_image_discrete(img, verbose = True, with_head_tail=True) w.plot(image = img); plt.scatter(cntr[:,0], cntr[:,1]) # target ig.t_counter = 500000 + 25620 - 1; ig.wid_counter = 0; #imgs, skels, valids, hts, htvs = ig.get_batch(nimages = 1); imgs, cntrs = ig.get_batch(nimages = 1); imgt = imgs[0,:,:,0]; cntrt = cntrs[0]; plt.figure(21); plt.clf(); w.plot(image = imgt); plt.scatter(cntrt[:,0], cntrt[:,1]) ### Cost functions wb = 2; ws = 1.0; wd = 2.0 npts = w.npoints; left = net.create_variable([1, npts]); right = net.create_variable([1, npts]); width = tf.constant(w.width, "float32"); length = tf.constant(w.length, "float32"); contour = tf.constant(cntrs, "float32"); cost = net.create_cost(left, right, contour, width, weight_bending = wb, weight_spacing = ws); cost_bend = net.create_cost_bending(par); cost_spacing = net.create_cost_spacing(par); cost_dist = net.create_cost_soft_min_distance(par, skel); grad = tf.gradients(cost, [par]); ### Tensoroflow ### Session sess = None; sess = tf.InteractiveSession() init = tf.initialize_all_variables(); sess.run(init) assign_op = par.assign(w.center[None,:,:]); sess.run(assign_op) ### Compare costs cb = wb * sess.run(cost_bend); cs = ws * sess.run(cost_spacing); cd = sess.run(cost_dist); c = sess.run(cost); print 'Costs: full: %f; dist: %f; bend: %f; spacing :%f' % (c, cd, cb, cs); ### Manual Gradient descent p1 = w.center; sg = .75; nsteps = 100; for i in range(nsteps): sess.run(par.assign(p1[None,:,:])); g = sess.run(grad)[0][0]; p1 = p1 - sg * g / np.sqrt(np.sum(g*g)); plt.figure(10); plt.clf(); #plt.subplot(1,2,1) #w.plot(image = img); w.center = p1; #plt.subplot(1,2,2); w.plot(image= imgt); plt.title('cost %f' % sess.run(cost)); plt.draw(); plt.pause(0.05); ### Tensorflow optimization #trainer = tf.train.AdadeltaOptimizer().minimize(cost, var_list=[par]); trainer = tf.train.GradientDescentOptimizer(learning_rate=2.0).minimize(cost); init = tf.initialize_all_variables(); sess.run(init) sess.run(assign_op) nsteps = 1000; for i in range(nsteps): trainer.run(session = sess, feed_dict={}); p1 = par.eval(session = sess); if i%10 == 0: plt.figure(10); plt.clf(); w.center = p1[0]; w.plot(image= imgt); plt.scatter(skels[0,:,0], skels[0,:,1], c= 'm') plt.xlim(0,151); plt.ylim(0,151) plt.draw(); plt.pause(0.1); import shapely.geometry as geom cntrs = wgeo.contours_from_image(imgt) poly = geom.Polygon(cntrs[0], [cntrs[i] for i in range(1,len(cntrs))]); poly2 = poly.buffer(-3) bdr = poly.boundary; from descartes.patch import PolygonPatch patch = PolygonPatch(poly, facecolor = 'b', edgecolor='k', alpha=0.5, zorder=2) patch2= PolygonPatch(poly2, facecolor = 'r', edgecolor='k', alpha=0.5, zorder=2) fig = plt.figure(28); ax = fig.add_subplot(111) ax.add_patch(patch) ax.add_patch(patch2) plt.xlim(0,151); plt.ylim(1,151) plt.show() ### make boundary pts in tensorflow npoints = 21; width = tf.placeholder("float32", shape = [npoints,2]);
m = wm.WormModel(npoints = 31) m.from_image(img, verbose = True); l,r = m.shape(); plt.subplot(2,3,5) plt.plot(l[:10,0], l[:10,1], 'w') #%% img2 = exp.load_img(wid = 80, t = 524273 ); plt.figure(2); plt.clf(); cont = wg.contours_from_image(img2, verbose = True) cout = cont[0]; if len(cont) > 1: cin = cont[1]; else: cin = []; plt.figure(5); plt.clf() ht, idx = wg.head_tail_from_contour_discrete(cout, with_index = True, verbose = True, image = img2); i = idx[0]; cout0 = np.vstack([cout[i:], cout[1:(i+1)]]);