def from_image(self, image, sigma=1, absolute_threshold=None, threshold_factor=0.95, ncontour=100, delta=0.3, smooth=1.0, nneighbours=4, verbose=False, save=None): success, center, left, right, width = wormgeo.shape_from_image( image, sigma=sigma, absolute_threshold=absolute_threshold, threshold_factor=threshold_factor, ncontour=ncontour, delta=delta, smooth=smooth, nneighbours=nneighbours, npoints=self.npoints, verbose=verbose, save=save) if success: #self.from_lines(shape[1], shape[2], shape[3]); self.from_center(center, width) else: raise RuntimeWarning('failed inferring worm from image')
def analyze_shape(fid): #load memmaps fid2 = min(fid+frame_batch, n_frames_total); #images = np.lib.format.open_memmap(data_image_file, mode = 'r'); images = turn; #shape = pmm.open_memmap(data_shape_file, arange = (fid, fid2), mode = 'r+'); #shape_info = pmm.open_memmap(data_shape_info_file, arange = (fid, fid2), mode = 'r+'); #smooth print('processing %d / %d' % (fid, n_frames_total)); for i,f in enumerate(range(fid, fid2)): #print('success status: %d' % shape_info[i]['success']); blur = cv2.GaussianBlur(images[f], ksize = (5,5), sigmaX = 0); if verbose: plt.clf(); try: res = wgeo.shape_from_image(blur, absolute_threshold = 135, sigma = None, verbose = verbose, smooth = 20, smooth_center = 2.5, npoints = n_points, ncontour = 80, center_offset = 3) #print(res[0]); except: print('something went wrong'); #res = (-1, np.zeros((n_points,2)), 0, 0, np.zeros(n_points)); if verbose: plt.draw(); plt.pause(0.5);
def from_image(self, image, sigma = 1, absolute_threshold = None, threshold_factor = 0.95, ncontour = 100, delta = 0.3, smooth = 1.0, nneighbours = 4, verbose = False, save = None): success, center, left, right, width = wormgeo.shape_from_image(image, sigma = sigma, absolute_threshold = absolute_threshold, threshold_factor = threshold_factor, ncontour = ncontour, delta = delta, smooth = smooth, nneighbours = nneighbours, npoints = self.npoints, verbose = verbose, save = save); if success: #self.from_lines(shape[1], shape[2], shape[3]); self.from_center(center, width); else: raise RuntimeWarning('failed inferring worm from image');
img = exp.load_img(t=t0 + i) plt.figure(71) plt.clf() plt.imshow(img[0]) plr.figure(100) plt.clf() w.from_image(img[0], verbose=True) import worm.geometry as wgeo reload(wgeo) plt.figure(71) plt.clf() wgeo.shape_from_image(img[3], verbose=True) from multiprocessing import Pool, cpu_count pool = Pool(processes=cpu_count() - 2) pool.map(process, range(ntimes)) data = [ np.load(os.path.join(data_path, 'analysis_2016_10_25_' + n + '.npy'), mmap_mode='r+') for n in data_names ] ### Failed frames nfailed = np.sum(data[3] == -1) print 'percentage failed %f' % (100.0 * nfailed / data[3].shape[0])
#% # detect worm shape n_contours = len(contours); #%% f_id = 80 import worm.geometry as wgeo reload(wgeo); plt.clf() res = wgeo.shape_from_image(blur[f_id], threshold_factor = 0.975, sigma = None, verbose = True, smooth = 15, smooth_center = 2.5, npoints = 45, ncontour = 80, center_offset = 3) #%% import worm.model as wmod wm = wmod.WormModel(npoints = 21) f_id = 5; plt.figure(1); plt.clf(); wm.from_image(blur[f_id], sigma = None, verbose = True, smooth = 10) plt.figure(19); plt.clf(); plt.subplot(1,2,1); wm.plot(blur[f_id])
fid = 31; n_points = 45; cnts, hrchy = wgeo.detect_contour(blur[fid], 135, with_hierarchy=True) plt.figure(13); plt.clf(); plt.imshow(blur[fid], interpolation = 'none'); for c in cnts: plt.plot(c[:,0], c[:,1]); #%% get previous center line / shape status_prev, center_prev, _, _, width_prev = wgeo.shape_from_image(blur[fid-1], absolute_threshold = 135, sigma = None, verbose = True, smooth = 20, smooth_center = 2.5, npoints = n_points, ncontour = 80, center_offset = 3) #%% decide inner vs outer n_cnts = len(cnts); print('Number of contours: %d' % n_cnts); # check if inner outer contour # find children of each contour children = []; for cid in range(n_cnts): children.append(np.where(hrchy[:,-1]==cid)[0]); # outer vs inner nc = [len(c) for c in children]
npoints = 21; nobs = npoints*2-2; #d differences from the sides / head tail only once worm = wmod.WormModel(npoints = npoints); nparameter = worm.nparameter; #full number of parameter nobs = worm.ndistances; t0 = 500000; threshold_factor = 0.9; absolute_threshold = None; img = exp.load_img(wid = 80, t = t0); imgs = filters.gaussian_filter(np.asarray(img, float), 1.0); worm.from_image(img, absolute_threshold = absolute_threshold, threshold_factor = threshold_factor, verbose = False); success, center, left, right, width = wgeo.shape_from_image(img, absolute_threshold = absolute_threshold, threshold_factor = threshold_factor, npoints = npoints, verbose = False, save = False); plt.figure(1); plt.clf(); #worm.plot(image = imgs) worm.plot(image = imgs) plot(center, 'r'); ### Compare Worm to Contour cntrs = wgeo.contour_from_image(img, sigma = 1, absolute_threshold = absolute_threshold, threshold_factor = threshold_factor, verbose = False, save = None); cntr = resample_curve(cntrs[0], 100); #assume outer contour is first, only match to this contour = Curve(cntr, nparameter = 50); contour.plot(with_points=False);
u2 = np.linspace(us[imax[0]], us[imax[1]]-1, npoints); u2 = np.mod(u2,1); x2, y2 = splev(u2, cinterp, der = 0); right = np.vstack([x2,y2]).T; fig = plt.figure(5); plt.clf(); plt.imshow(img, cmap = 'gray'); plt.plot(left[:,0], left[:,1], c = 'r', linewidth = 1.5); plt.plot(right[:,0], right[:,1], c = 'g', linewidth = 1.5); plt.xlim(0,151); plt.ylim(0,151) plt.axis('off') fig.savefig('pipeline_left_right.png', facecolor = 'white') s,center,l,r,width = wgeo.shape_from_image(img) fig = plt.figure(6); plt.clf(); plt.imshow(img, cmap = 'gray'); plt.plot(left[:,0], left[:,1], c = 'r', linewidth = 1.5); plt.plot(right[:,0], right[:,1], c = 'g', linewidth = 1.5); plt.plot(center[:,0], center[:,1], c = 'b', linewidth = 2); plt.xlim(0,151); plt.ylim(0,151) plt.axis('off') fig.savefig('pipeline_center.png', facecolor = 'white') # theta angles , width profile etc theta = wgeo.theta_from_center(center)
plt.plot(dd) i = np.where(dd < 5)[0]; img = exp.load_img(t = t0 + i); plt.figure(71); plt.clf(); plt.imshow(img[0]) plr.figure(100); plt.clf(); w.from_image(img[0], verbose = True) import worm.geometry as wgeo reload(wgeo) plt.figure(71); plt.clf(); wgeo.shape_from_image(img[3], verbose = True); from multiprocessing import Pool, cpu_count; pool = Pool(processes = cpu_count()-2); pool.map(process, range(ntimes)); data = [np.load(os.path.join(data_path, 'analysis_2016_10_25_' + n + '.npy'), mmap_mode = 'r+') for n in data_names]; ### Failed frames nfailed = np.sum(data[3] == -1);
t0 = 500000 threshold_factor = 0.9 absolute_threshold = None img = exp.load_img(wid=80, t=t0) imgs = filters.gaussian_filter(np.asarray(img, float), 1.0) worm.from_image(img, absolute_threshold=absolute_threshold, threshold_factor=threshold_factor, verbose=False) success, center, left, right, width = wgeo.shape_from_image( img, absolute_threshold=absolute_threshold, threshold_factor=threshold_factor, npoints=npoints, verbose=False, save=False) plt.figure(1) plt.clf() #worm.plot(image = imgs) worm.plot(image=imgs) plot(center, 'r') ### Compare Worm to Contour cntrs = wgeo.contour_from_image(img, sigma=1, absolute_threshold=absolute_threshold,