def show_locations(self, show_traj=False, **locate_configs): # TODO: for some reason, locate config can be modified here # self.locate_configs has the highest priority locate_configs.update(self.locate_configs) # Calculate location if not exist if self.locations is None: self.locate(plot_progress=True, **locate_configs) configs = self.effective_locate_config df = self.locations # Link location if necessary if show_traj: if self.trajectories is None: self.link() configs = self.effective_link_config df = self.trajectories # Display df = df[df['frame'] == self.object_cursor] tp.annotate(df, self.raw_frames[self.object_cursor], ax=self.axes) # Set title title = None if self.show_titles: title = ', '.join( ['{} = {}'.format(k, v) for k, v in configs.items()]) title += ' (#{})'.format(df.size) self.set_im_axes(title=title) return self.locations
def method(): if self.Keys.locations not in self._pocket: # Calculate locations data_frames = [] tic = time.time() for i in range(self.n_frames): self.show_text('Calculating {}/{} ...'.format( i + 1, self.n_frames)) df = tp.locate(self.images[i], diameter, **kwargs) data_frames.append(df) console.print_progress(i + 1, self.n_frames, start_time=tic) self.put_into_pocket(self.Keys.locations, data_frames, exclusive=False) console.show_status('Locating completed. Configurations:') kwargs['diameter'] = diameter console.supplement(kwargs) # Clear status self.axes.cla() # Display dfs = self.get_from_pocket(self.Keys.locations) tp.annotate(dfs[self.cursor], self.raw_frames[self.cursor], ax=self.axes) # Set title title = ', '.join( ['{} = {}'.format(k, v) for k, v in kwargs.items()]) self.set_axes_style(title)
def Detect(estimateFeatureSize, CameraName, minMass = None, dynamicMinMass = False): ImagePath = 'E:/BubbleRisingUltimate/4mmGasBubbleRising/4mmGasBubbleRising' Path = os.path.join(ImagePath, CameraName) frames = pims.open(Path) print('Valid frames length is %d' %len(frames)) # find five brightest if not minMass: f = tp.locate(frames[testFrame], estimateFeatureSize) mass = list(f['mass']); mass.sort() minMass = int(mass[-3]*0.9 + mass[-1]*0.1) print(minMass) #TopTen = np.argsort(f['mass'])[-5:] #TopTenArray = f['mass'][TopTen] # show mass histogram # show subpixel accuracy of the detection #minMass = list(TopTenArray)[0] f = tp.locate(frames[testFrame], estimateFeatureSize, minmass= minMass) plt.figure() tp.annotate(f, frames[testFrame]); # run batch processing for all frames if dynamicMinMass: f = f[0:0] for i in range(len(frames)): f_ele = tp.locate(frames[i], estimateFeatureSize) mass = list(f_ele['mass']); mass.sort() minMass = int(mass[-2]*0.9 + mass[-1]*0.1) f_ele = tp.locate(frames[i], estimateFeatureSize, minmass = minMass) f = f.append(f_ele) else: f = tp.batch(frames, estimateFeatureSize, minmass = minMass) return f, frames
def test(): frames = gray(pims.open('data/png/crack_tip/*.png')) print(frames) print(frames[0][100, :]) plt.imshow(frames[0]) f = tp.locate(frames[0], diameter=15, invert=False, minmass=1) f.head() tp.annotate(f, frames[0])
def show_annotated_first_frame(data, img_dir_path): """ :param data: dataframe :param img_dir_path: string :return: """ frames = pims.ImageSequence('{}*.jpg'.format(img_dir_path)) plt.figure() tp.annotate(data[data['frame'] == 0], frames[0])
def identify(frame): '''Identify features in a frame''' if isinstance(frame, str): frame = cv2.imread(str(frame), cv2.IMREAD_GRAYSCALE) f = tp.locate(frame, 11, percentile=99, invert=True) f.head() tp.annotate(f, frame, color='r') return
def imageClassifier(frames, cell_size, min_mass, particle_separation): print("We have "+str(len(frames))+" images in this batch.\nWe will open them up one at a time.\n Feel free to save them and then exit the window to view the next one.") for frame in frames: f = tp.locate(frame, cell_size, invert=True, minmass=min_mass, separation=particle_separation, noise_size=4) #locationWeightHistogram(f) plt.figure("Filename") # make a new figure plt.xlabel('Number of cells: ' + str(len(f))) tp.annotate(f, frame) plt.savefig('./filename.png', format='png') # save the figure to file print(f.head()) return 1
def displayFrame(array, dataframe, frame=0): # Deal with multiple frame arrays if len(array.shape) == 3: array = array[frame] # Select all the particles on the given frame(s) if "frame" in dataframe: dataframe = dataframe[dataframe["frame"] == frame] tp.annotate(dataframe, array)
def upload(): #clear the DOWNLOAD directory for root, dirs, files in os.walk(app.config['DOWNLOAD_FOLDER']): for f in files: os.unlink(os.path.join(root, f)) for d in dirs: shutil.rmtree(os.path.join(root, d)) #clear the UPLOAD directory for root, dirs, files in os.walk(app.config['UPLOAD_FOLDER']): for f in files: os.unlink(os.path.join(root, f)) for d in dirs: shutil.rmtree(os.path.join(root, d)) # Get specifications uploaded_files = request.files.getlist("file[]") _invert = request.form['invert'] _diameter = int(request.form['diameter']) _min_mass = float(request.form['minmass']) _noise_size = float(request.form['noise_size']) _smoothing_size = float(request.form['smoothing_size']) _separation = float(request.form['separation']) #list of files for use on upload.html filenames = [] for file in uploaded_files: # Check if the file is one of the allowed types/extensions if file and allowed_file(file.filename): # Make the filename safe, remove unsupported chars filename = secure_filename(file.filename) #Save file to upload folder file.save(os.path.join(app.config['DOWNLOAD_FOLDER'], filename)) #load the image frames frames = pims.ImageSequence(os.path.join(app.config['DOWNLOAD_FOLDER'], filename), as_grey=True) #for loop of 1 to deal with PIMS bug. for frame in frames: #locate features f = tp.locate(frames, _diameter, minmass=_min_mass, separation=_separation, invert=_invert) #noise_size=_noise_size, smoothing_size=_smoothing_size, plt.ioff() #interactive mode = off plt.figure(filename) # make a new figure plt.title(filename) plt.xlabel('Number of cells: ' + str(len(f))) #label axis tp.annotate(f, frame) #display the iamge and the circle overlay #filename_png_extension = os.path.splitext(filename)[0] + ".png" plt.savefig(os.path.join(app.config['UPLOAD_FOLDER'], filename), format='png') # save the figure to filenames plt.close() #close figure # Save the filename into a list, we'll use it later filenames.append(filename) print(filenames, file=sys.stderr) # Load an html page with a link to each uploaded file return render_template('upload.html', filenames=filenames)
def tracking(video): print "running tracking" pimsFrames = pims.Video(video, as_grey = True) cells = [] track = [] for frame in pimsFrames[:]: f = tp.locate(frame, 301, invert=False, minmass = 2000) t = tp.link_df(f, 5) #remember cells after they left frame tp.annotate(f, frame) cells += f track += t print t.head() tp.plot_traj(t) return t.head()
def tracking(video): print "running tracking" pimsFrames = pims.Video(video, as_grey=True) cells = [] track = [] for frame in pimsFrames[:]: f = tp.locate(frame, 301, invert=False, minmass=2000) t = tp.link_df(f, 5) #remember cells after they left frame tp.annotate(f, frame) cells += f track += t print t.head() tp.plot_traj(t) return t.head()
def Detect(estimateFeatureSize, CameraName, minMass=None, dynamicMinMass=False, Crop=False): ImagePath = os.path.join('data', CaseName.split('-')[0], CaseName.split('-')[1]) Path = os.path.join(ImagePath, CameraName + '*.tif') frames = pims.open(Path) if Crop: frames = pims.process.crop(frames, ((400, 700), (0, 0))) print('Valid frames length is %d' % len(frames)) # check start frame and end frame with total frames number if len(frames) != (endFrame - startFrame + 1): print('Invalid frames length') return # find five brightest if not minMass: f = tp.locate(frames[testFrame], estimateFeatureSize) mass = list(f['mass']) mass.sort() minMass = int(mass[-2] * 0.9 + mass[-1] * 0.1) print(minMass) #TopTen = np.argsort(f['mass'])[-5:] #TopTenArray = f['mass'][TopTen] # show mass histogram # show subpixel accuracy of the detection #minMass = list(TopTenArray)[0] f = tp.locate(frames[testFrame], estimateFeatureSize, minmass=minMass) plt.figure() tp.annotate(f, frames[testFrame]) # run batch processing for all frames if dynamicMinMass: f = f[0:0] for i in range(len(frames)): f_ele = tp.locate(frames[i], estimateFeatureSize) mass = list(f_ele['mass']) mass.sort() minMass = int(mass[-2] * 0.9 + mass[-1] * 0.1) f_ele = tp.locate(frames[i], estimateFeatureSize, minmass=minMass) f = f.append(f_ele) else: f = tp.batch(frames, estimateFeatureSize, minmass=minMass) return f, frames
def cell_detect(file, var, opt): print('Detecting cells') # http://soft-matter.github.io/trackpy/v0.3.2/tutorial/walkthrough.html # load, run object detection and track (then clean up) raw_frames = pims.TiffStack(file, as_grey=True) # load # only analyse n frames if var['frames_keep'] is not 0: raw_frames = raw_frames[0:var['frames_keep']] # object detect cellsdf = tp.batch(raw_frames, var['diameter'], minmass=var['minFluroMass'], separation=var['separation'], engine='numba', max_iterations=1, characterize=False, noise_size=var['noise_smooth']) # remove brightest objects cellsdf = cellsdf.drop(cellsdf[cellsdf.mass > var['maxFluroMass']].index) if opt['plot_inter_static']: annotate_args = {"vmin": 0, "vmax": 200} # Tweak styles plt.ion() plt.show() fig_dims = np.multiply(0.01, raw_frames[0].shape) mpl.rc('figure', figsize=(fig_dims[1].astype(int), fig_dims[0].astype(int))) mpl.rc('image', cmap='gray') # plot final particles chosen plt.figure() tp.annotate(cellsdf[cellsdf.frame == var['frame_plot']], raw_frames[var['frame_plot']], imshow_style=annotate_args) plt.title('Particles included in analysis' '(at t=' + str(var['frame_plot']) + ')') plt.draw() plt.pause(0.001) return cellsdf, raw_frames
def OnBtnAnalystClickedSlot(self): """点击加载按钮""" # 容错 if self.__pictrureName is None or len(self.__pictrureName) == 0: return # 加载图片 frames = pims.ImageSequence(self.__pictrureName, as_grey=True) plt.imshow(frames[0]) # 查找特征点,返回一个DataFrame features = tp.locate(frames[0], 11, invert=True) features.head() # 分析显示 plt.figure() # make a new figure tp.annotate(features, frames[0])
def get_annotated_image(self, frame, features, highlight_index=None): self.log.debug("Annotating image with features") fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.axis('off') annotated = trackpy.annotate(features, frame, ax=ax) if highlight_index is not None: single = features.iloc[highlight_index] annotated = trackpy.annotate(single, frame, color='#3c4648', ax=annotated, plot_style={'markersize': 20}) figure = annotated.get_figure() buffer = io.BytesIO() figure.savefig(buffer, format='png', bbox_inches='tight') buffer.seek(0) plt.close(figure) return Image.open(buffer)
def get_frame(video_name, i, particle_size=None): """ The function that displays a frame of the video, optionally with its features circled. Parameters: video_name (String): The name of the video file in the Recordings folder to be processed i (int): The frame of the video to display. particle_size (int): The odd number average pixel size of a feature. Leave blank in order to only display the frame. """ video_frames = process_video(video_name) fig = plt.figure() if particle_size is not None: f = tp.locate(video_frames[i], particle_size) tp.annotate(f, video_frames[i]) plt.show() else: plt.imshow(video_frames[i])
def display3d(stack, pos=None, origin=None): _plot_style = dict(markersize=15, markeredgewidth=2, markerfacecolor='none', markeredgecolor='r', marker='o', linestyle='none') from matplotlib.pyplot import imshow from pandas import DataFrame from trackpy import annotate n, tile_y, tile_x = stack.shape[-3:] n_rows = int(np.sqrt(stack.size) // tile_y) n_cols = int(np.ceil(n / n_rows)) result = np.zeros((n_rows * tile_y, n_cols * tile_x), dtype=stack.dtype) if pos is not None and origin is not None: pos_rel = np.array(pos) - np.array(origin)[np.newaxis, :] elif pos is not None: pos_rel = np.array(pos) i_row = 0 i_col = 0 if pos is not None: plot_pos = np.empty((0, 2), dtype=np.float) for i in range(n): tile = stack.take(i, axis=stack.ndim - 3) result[i_row * tile_y: (i_row + 1) * tile_y, i_col * tile_x: (i_col + 1) * tile_x] = tile if i_col < n_cols - 1: i_col += 1 else: i_col = 0 i_row += 1 if pos is not None: mask = (pos_rel[:, 0] > (i - 0.5)) & (pos_rel[:, 0] < (i + 0.5)) if mask.sum() > 0: plot_pos = np.append(plot_pos, pos_rel[mask, 1:] + np.array([[i_row * tile_y, i_col * tile_x]]), axis=0) if pos is None: imshow(result) else: f = DataFrame(plot_pos, columns=['y', 'x']) annotate(f, result)
def particle_movement_frames(self, frames): track = self.chart_data.single_trajectory for frame, feature in self.__frame_track_iterator(track, frames): with self.plt_lock: fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.axis('off') annotated = tp.annotate(feature, frame, ax=ax) yield trackpy_fig_to_pil(annotated) fig.clear() plt.close(fig)
def main(): estimateFeatureSize = args.Size # must be odd numer path = args.Path minMass = args.MinMass separation = args.Seperation img = cv2.imread(path) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) f = tp.locate(img, estimateFeatureSize) fig, ax = plt.subplots() plt.ioff() ax.hist(f['mass'], bins=20) ax.set(xlabel='mass', ylabel='count'); fig.savefig('mass.png') h1 = plt.figure(figsize=(12,12)) f = tp.locate(img, estimateFeatureSize, minmass= minMass, separation =2) tp.annotate(f, img); h1.savefig('target.png') f.to_csv(r'./CenterPoints.csv')
def create_figure(self, frame_id): fig, ax = plt.subplots() start_time = timeit.default_timer() f_locate = tp.locate(self.frames[frame_id], self.radius + 2, minmass=600, invert=True) elapsed = timeit.default_timer() - start_time tp.annotate(f_locate, self.frames[frame_id], plot_style={'markersize': self.radius}, ax=ax) canvas = FigureCanvas(fig) output = BytesIO() canvas.print_png(output) return { "image": base64.b64encode(output.getvalue()).decode('utf8'), "time_elapsed": elapsed, "feature_count": len(f_locate.index) }
def locate_feature_calibrate(image_path, particle_size, minmass): #image = plt.imread(image_path, format='TIFF') image = cv.imread( image_path, -1 ) # using cv2 https://stackoverflow.com/questions/18446804/python-read-and-write-tiff-16-bit-three-channel-colour-images #image = image[8:-1, :] # crop out noise frame = pims.Frame(image) plt.figure(0) plt.imshow(image) f = tp.locate(frame, particle_size, minmass=minmass) plt.figure(1) tp.annotate(f, frame, plot_style={'markersize': 10, 'markeredgewidth': 1}) plt.show(block=True) fig, ax = plt.subplots() ax.hist(f['mass'], bins=20) plt.show(block=True) return f
def image_locate_particles(df1, frame, img_region, fig=None, ax=None, annotate_particles=False): import trackpy as tp fig = plt.figure(figsize=[20, 20]) if fig is None else fig ax = plt.subplot(111) if ax is None else ax ax = image_background(img_region=img_region, img=frame, ax=ax) ax = tp.annotate(df1, frame, ax=ax) if annotate_particles: _ = df1.apply(lambda x: ax.text( x['x'], x['y'], int(x['particle']), color='lime'), axis=1) # ax.grid(False) return ax
def get_params_locate(frame,diameter=15,minmass_percentile=92,out_fh=None,test=True,figsize=None): f = tp.locate(frame, diameter, invert=False) minmass=np.percentile(f['mass'],minmass_percentile) logging.info('feature count= %s, %spercentile= %s' % (len(f),minmass_percentile,minmass)) f = tp.locate(frame, diameter, invert=False, minmass=np.percentile(f['mass'],minmass_percentile)) logging.info('feature count= %s, %spercentile= %s' % (len(f),minmass_percentile, np.percentile(f['mass'],minmass_percentile))) if test: logging.info('getting plots annotate') # plt.clf() fig=plt.figure(figsize=figsize) ax=plt.subplot(111) ax=tp.annotate(f, frame,ax=ax) if not out_fh is None: # plt.savefig('%s.annotate.pdf' % out_fh,format='pdf') plt.savefig('%s.annotate.svg' % out_fh,format='svg') # plt.clf() logging.info('getting plots hist') cols=['mass','size','ecc','signal','raw_mass','ep'] fig=plt.figure() ax=plt.subplot(111) _=f.loc[:,cols].hist(ax=ax) if not out_fh is None: plt.savefig('%s.feature_props.svg' % out_fh,format='svg') # plt.clf() logging.info('getting plots bias') fig=plt.figure() tp.subpx_bias(f); if not out_fh is None: plt.savefig('%s.subpx_bias.svg' % out_fh,format='svg') # plt.clf() params_locate={'diameter':diameter, 'minmass':minmass} return params_locate
# -*- coding: utf-8 -*- """ Created on Thu Oct 31 10:13:30 2019 @author: labuser """ import trackpy as tp import pims import matplotlib.pyplot as plt images = pims.open(r'C:\Users\labuser\Documents\SEQconvert/*.png' ) # many PNGs with sequential names for i in range(50): plt.imshow(images[i]) plt.pause(0.1) f = tp.locate(images[0], 11) for i in range(30): g = tp.locate(images[i], 11, minmass=20) tp.annotate(g, images[i]) plt.pause(0.1) h = tp.batch(images[:], 11) h.head() plt.figure() tp.plot_traj(h)
FilePath = os.path.join(VideoFolder, FileName) frameNum = 0 flag = 0 blobSize = 51 minMass = 10000 while (flag == 0): features = tp.locate(frames[frameNum], particleSize, invert=False) features.head() # shows the first few rows of data plt.figure(2) # make a new figure tp.annotate(features, frames[frameNum]) fig, ax = plt.subplots() ax.hist(features['mass'], bins=20) # Optionally, label the axes. ax.set(xlabel='mass', ylabel='count') plt.figure(3) tp.subpx_bias(features) flag = int( input('Press 1 to use these settings, Press 0 to Enter new settings')) if (flag == 0): frameNum = int(
T = ufloat(293,1) eta = 1/1000 a = ufloat(0.00000099, 0.00000003) frames = pims.TiffStack('C:/Users/Ewout van der Velde/Downloads/Untitled4.tif', as_grey=False) f = tp.locate(frames[-250:], 21, invert = True, minmass=1000) f.head() plt.figure() tp.annotate(f, frames[0]) fig,ax = plt.subplots() ax.hist(f['mass'], bins=40) f = tp.batch(frames[-100:], 21, minmass = 1000, invert=True) t = tp.link_df(f, 5, memory=3) t.head() t1 = tp.filter_stubs(t, 25) print('before:', t['particle'].nunique()) print('after:', t1['particle'].nunique()) plt.figure()
_frames_n.append(f_number) _rois_n.append(_roi_number) _values.append(np.mean(mov[f_number])) imput_df = pd.DataFrame() imput_df['frame'] = _frames_n imput_df['roi'] = _rois_n imput_df['mass'] = 10 imput_df['imput'] = True _parts_filt['imput'] = False _parts_filt = pd.concat((_parts_filt, imput_df), sort=False) # keep only ONE spot per frame, impute if none available _parts_filt_ = _parts_filt.sort_values('imput').drop_duplicates( ['roi', 'frame'], keep='first') traces = _parts_filt_.pivot(index='roi', columns='frame', values='mass').values traces_s = smooth_traces(traces, smooth_win=4) plt.figure() sns.heatmap(traces_s) stackplot(traces_s) stackplot(test) _parts_track_filt = tp.filter_stubs(_parts_track, threshold=3) plt.figure() tp.annotate(_parts_filt[_parts_filt.frame == 75], mov[75]) plt.figure() tp.annotate(_parts_filt, proj) track_mov = tracking_movie(mov, _parts_filt) io.imsave('/Users/porfirio/Desktop/trackingmov.tif', track_mov)
#fig=tp.annotate(f, aa[0]) #fig.figure.savefig("./trackpyResult/trackpyAnnotation.jpg") #f = tp.batch(aa[:], 11, minmass=200, invert=True); #f = tp.batch(aa[:], 11, invert=True); fig, ax = plt.subplots() t = tp.link_df(f, 5, memory=3) t1 = tp.filter_stubs(t, 50) print(t1) t1.to_csv("./trackpyResult/t1.csv") # Compare the number of particles in the unfiltered and filtered data. print('Before:', t['particle'].nunique()) print('After:', t1['particle'].nunique()) #fig=plt.figure() #fig=tp.mass_size(t1.groupby('particle').mean()); # convenience function -- just plots size vs. mass #fig.figure.savefig("./trackpyResult/particle.jpg") fig=plt.figure() fig=tp.plot_traj(t1) fig.figure.savefig("./trackpyResult/trajectoryI.jpg") t2 = t1 fig=plt.figure() fig=tp.annotate(t2[t2['frame'] == 0], aa[0]); fig.figure.savefig("./trackpyResult/t2Annotation.jpg") d = tp.compute_drift(t2) fig=plt.figure() fig=d.plot() tm = tp.subtract_drift(t1.copy(), d) fig.figure.savefig("./trackpyResult/comDrift.jpg") fig=plt.figure() fig=tp.plot_traj(tm) fig.figure.savefig("./trackpyResult/traj.jpg")
def mark_picture(x, y, frame): data = [[x, y]] df = pd.DataFrame(data, columns=['x', 'y']) plt.xlabel('x ') tp.annotate(df, frame, plot_style={'markersize': 2})
pid_filt = [pid in goodparts for pid in pids_all] pid_filtwt = np.array(['tl092' in pid for pid in pids_all]) & np.array(pid_filt) pid_filt75 = np.array(['pQC75' in pid for pid in pids_all]) & np.array(pid_filt) pid_filt76 = np.array(['pQC76' in pid for pid in pids_all]) & np.array(pid_filt) vmin, vmax = rawims_all.min(), rawims_all.max() fig, axes = plt.subplots(3) for filt_ix, ax in zip((pid_filtwt, pid_filt75, pid_filt76), axes.ravel()): ax.imshow(im_block(rawims_all[filt_ix], cols=200, norm=True, sort=corr_widealspot), vmin=0, vmax=1, cmap='viridis') import trackpy as tp impath = '/Users/porfirio/lab/yeastEP/smFISH/data/TL47pQC7576/05052018/TL47pQC75/05052018_TL47pQC75_5.tif' impath = '/Users/porfirio/lab/yeastEP/smFISH/data/TL47pQC7576/04242018/TL47tl092/04242018_TL47tl092_11.tif' impath = '/Users/porfirio/lab/yeastEP/smFISH/data/TL47pQC7576/04242018/TL47pQC76/04242018_TL47pQC76_9.tif' im = io.imread(impath)[:, :, 0] imname = impath.split('/')[-1][:-4] plt.figure() tp.annotate( max_parts[(max_parts.mov_name == imname) & (max_parts.nucleus >= 0)], np.log(im))
def OnBtnAnalystAllClickedSlot(self): """测试所有的参数""" # 获取行号 rowCount = self.tableWidget.rowCount() if rowCount <= 0: return # 遍历循环 for currentRow in range(rowCount): # 显示当前测试的参数 self.tableWidget.setCurrentCell(currentRow, 0) # 获取参数 diameter = int(self.tableWidget.item(currentRow, 0).text()) \ if self.tableWidget.item(currentRow, 0) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else 11 minmass = float(self.tableWidget.item(currentRow, 1).text()) \ if self.tableWidget.item(currentRow, 1) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else None maxsize = float(self.tableWidget.item(currentRow, 2).text()) \ if self.tableWidget.item(currentRow, 2) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else None separation = float(self.tableWidget.item(currentRow, 3).text()) \ if self.tableWidget.item(currentRow, 3) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else None noise_size = float(self.tableWidget.item(currentRow, 4).text()) \ if self.tableWidget.item(currentRow, 4) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else None smoothing_size = float(self.tableWidget.item(currentRow, 5).text()) \ if self.tableWidget.item(currentRow, 5) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else None threshold = float(self.tableWidget.item(currentRow, 6).text()) \ if self.tableWidget.item(currentRow, 6) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else None invert = (self.tableWidget.item(currentRow, 7).text() == "True") \ if self.tableWidget.item(currentRow, 17) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else False percentile = float(self.tableWidget.item(currentRow, 8).text()) \ if self.tableWidget.item(currentRow, 8) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else 64 topn = int(self.tableWidget.item(currentRow, 9).text()) \ if self.tableWidget.item(currentRow, 9) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else None preprocess = (self.tableWidget.item(currentRow, 10).text() == "True") \ if self.tableWidget.item(currentRow, 10) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else True max_iterations = int(self.tableWidget.item(currentRow, 11).text()) \ if self.tableWidget.item(currentRow, 11) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else 10 filter_before = (self.tableWidget.item(currentRow, 12).text() == "True") \ if self.tableWidget.item(currentRow, 12) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else None filter_after = (self.tableWidget.item(currentRow, 13).text() == "True") \ if self.tableWidget.item(currentRow, 13) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else None characterize = (self.tableWidget.item(currentRow, 14).text() == "True") \ if self.tableWidget.item(currentRow, 14) is not None and len(self.tableWidget.item(currentRow, 0).text()) > 0 else True # 容错 if self.__pictrureName is None or len(self.__pictrureName) == 0: return # 加载图片 frames = pims.ImageSequence(self.__pictrureName, as_grey=True) plt.imshow(frames[0]) # 查找特征点,返回一个DataFrame features = tp.locate(frames[0], diameter=diameter, minmass=minmass, maxsize=maxsize, separation=separation, noise_size=noise_size, smoothing_size=smoothing_size, threshold=threshold, invert=invert, percentile=percentile, topn=topn, preprocess=preprocess, max_iterations=max_iterations, filter_before=filter_before, filter_after=filter_after, characterize=characterize) features.head() # 分析显示 plt.figure() # make a new figure tp.annotate(features, frames[0])
# plt.figure() # new figure # tp.annotate(f, frames[0]) # this has lots of false positives # plt.show() # let's see a histogram of total brightness of blobs # fig, ax = plt.subplots() # ax.hist(f['mass'], bins=20) # # label axes: # ax.set(xlabel='mass', ylabel='count') # plt.show() # now we're filtering by brightness (minmass): f = tp.locate(frames[63], 11, minmass=49000, max_iterations=20) plt.figure() tp.annotate(f, frames[63]) plt.show() # check for subpixel bias (are decimals evenly distributed) tp.subpx_bias(f) plt.show() # locate features in all frames # TODO return here time_cutoff = 419 f = tp.batch(frames[:time_cutoff], 11, minmass=49000, max_iterations=20) print("f length is:", time_cutoff - 2) # link features into particle trajectories # define max displacement max_disp = 5
#from __future__ import division, unicode_literals, print_function # for compatibility with Python 2 and 3 import matplotlib as mpl import matplotlib.pyplot as plt # the following line only works in an IPython notebook #%matplotlib notebo # Optionally, tweak styles. mpl.rc('figure', figsize=(10, 6)) mpl.rc('image', cmap='gray') import numpy as np import pandas as pd from pandas import DataFrame, Series # for convenience import pims import trackpy as tp frames = pims.ImageSequence('2.jpg', as_grey=True) print(frames) plt.imshow(frames[0]) #plt.show() #f = tp.locate(frames[0], 101, invert=True) f = tp.locate(frames[0], 101, engine='python') print(f.head()) plt.figure() # make a new figure tp.annotate(f, frames[0], plot_style=dict(marker='x')) plt.show() #features = tp.locate(bins_1.jpg,
def main(): for case_idx, case in enumerate(cases.values()): res_path = gen_path + case[1] frames = pims.ImageSequence(gen_path + case[0], as_grey=True) # Stores the unfiltered annotated image of a frame to local file path if plots["Annotate_unfiltered"]: k = tp.locate(frames[0], 11, invert=[case_idx in [0, 1]], minmass=200) fig = plt.figure("Annotated_unfiltered_image_" + case[2]) ax1 = fig.add_subplot() a = ["k", "w"][case_idx in [2, 3]] tp.annotate(k, frames[0], color=a, ax=ax1) #ax1.set_title("Annotated unfiltered image case: "+ case[2]) #ax1.set_xlabel("x[px]", fontsize=size) #ax1.set_ylabel("y[px]", fontsize=size) ax1.tick_params(axis="both", which="both", top=False, bottom=False, labelbottom=False, right=False, left=False, labelleft=False) fig.savefig(res_path + "Annotated_unfiltered_image_" + case[2] + ".png", bbox_inches="tight", pad_inches=0) plt.close(fig) # If True: Tracks all frames and stores .csv to locally, else: imports such .csv file from local if plots["Generate_batch"]: f = tp.batch(frames[:225], 11, minmass=100, invert=[case_idx in [0, 1]]) f.to_csv(res_path + "batch_" + case[2] + ".csv") if not plots["Generate_batch"]: f = pd.read_csv(res_path + "batch_" + case[2] + ".csv") # Linking and filtering t = tp.link_df(f, 5, memory=3) t1 = tp.filter_stubs(t, 50) # Plots the size vs mass profile and saves to local file path if plots["Size_vs_mass"]: fig = plt.figure("Size_vs_mass_" + case[2]) ax1 = fig.add_subplot() tp.mass_size( t1.groupby('particle').mean(), ax=ax1) # convenience function -- just plots size vs. mass #ax1.set_title("Size vs mass case: " + case[2]) ax1.set_xlabel("mass", fontsize=size) ax1.set_ylabel("Gyration radius [px]", fontsize=size) ax1.spines['top'].set_visible(False) ax1.spines['right'].set_visible(False) ax1.tick_params(axis="both", which="major", labelsize=size) ax1.tick_params(axis="both", which="minor", labelsize=size) fig.savefig(res_path + "Size_vs_mass_" + case[2] + ".png", bbox_inches="tight", pad_inches=0) plt.close(fig) if plots["Annotate_filtered"]: if case_idx in [0, 1]: # Set BF condition condition = lambda x: ( (x['mass'].mean() > 250) & (x['size'].mean() < 3.0) & (x['ecc'].mean() < 0.1)) elif case_idx in [2, 3]: # Set DF condition condition = lambda x: ( (x['mass'].mean() > 100) & (x['size'].mean() < 5.0) & (x['ecc'].mean() < 0.1)) t2 = tp.filter( t1, condition ) # a wrapper for pandas' filter that works around a bug in v 0.12 fig = plt.figure("Annotated_filtered_image_" + case[2]) ax1 = fig.add_subplot() k = ["k", "w"][case_idx in [2, 3]] tp.annotate(t2[t2['frame'] == 0], frames[0], color=k, ax=ax1) #ax1.set_title("Annotated filtered image case: " + case[2]) #ax1.set_xlabel("x[px]", fontsize=size) #ax1.set_ylabel("y[px]", fontsize=size) ax1.tick_params(axis="both", which="both", top=False, bottom=False, labelbottom=False, right=False, left=False, labelleft=False) fig.savefig(res_path + "Annotated_filtered_image_" + case[2] + ".png", bbox_inches="tight", pad_inches=0) plt.close(fig) if plots["Gyration_radius_filtered"]: size_dis_t1 = [i * ratio_μm_px for i in t1['size']] plt.figure("Gyration_radius_filtered_" + case[2]) plt.hist(size_dis_t1, bins=300, color="k", alpha=0.5) #plt.title("Gyration radius filtered case: "+ case[2]) plt.ylabel("Events", fontsize=size) plt.xlabel("Gyration radius [μm]", fontsize=size) plt.gca().spines['top'].set_visible(False) plt.gca().spines['right'].set_visible(False) plt.tick_params(axis="both", which="major", labelsize=size) plt.tick_params(axis="both", which="minor", labelsize=size) plt.savefig(res_path + "Gyration_radius_filtered" + case[2] + ".png", bbox_inches="tight", pad_inches=0) plt.close("all") if plots["Gyration_radius_unfiltered"]: size_dis_t = [i * ratio_μm_px for i in t['size']] plt.figure("Gyration_radius_unfiltered_" + case[2]) plt.hist(size_dis_t, bins=300, color="k", alpha=0.5) #plt.title("Gyration radius unfiltered case: " + case[2]) plt.ylabel("Events", fontsize=size) plt.xlabel("Gyration radius [μm]", fontsize=size) plt.gca().spines['top'].set_visible(False) plt.gca().spines['right'].set_visible(False) plt.tick_params(axis="both", which="major", labelsize=size) plt.tick_params(axis="both", which="minor", labelsize=size) plt.savefig(res_path + "Gyration_radius_unfiltered" + case[2] + ".png", bbox_inches="tight", pad_inches=0) plt.close("all") d = tp.compute_drift(t1) tm = tp.subtract_drift(t1, d) if plots["Trajectory_drift"]: fig = plt.figure("Trajectory_drift_subtracted_" + case[2]) ax1 = fig.add_subplot() ax1.tick_params(axis="both", which="major", labelsize=size) ax1.tick_params(axis="both", which="minor", labelsize=size) ax1.spines['top'].set_visible(False) ax1.spines['right'].set_visible(False) tp.plot_traj(tm, ax=ax1) #ax1.set_title("Trajectory with drift subtracted case: " + case[2]) plt.savefig(res_path + "Trajectory_drift_subtracted_" + case[2] + ".png", bbox_inches="tight", pad_inches=0) plt.close(fig) if plots["Variance_all_parts"]: im = tp.imsd( tm, ratio_μm_px, fps, max_lagtime=225 ) # microns per pixel = 100/285., frames per second = 24 plt.figure("Variance_for_all_particles_" + case[2]) #plt.title("Variance for all particles case: " + case[2]) plt.plot(im.index, im, 'k-', alpha=0.1) # black lines, semitransparent plt.ylabel(r'$\langle \Delta r^2 \rangle$ [$\mu$m$^2$]', fontsize=size), plt.xlabel('time $t$', fontsize=size) plt.gca().spines['top'].set_visible(False) plt.gca().spines['right'].set_visible(False) plt.xscale('log') plt.yscale('log') plt.savefig(res_path + "Variance_for_all_particles_" + case[2] + ".png", bbox_inches="tight", pad_inches=0) if plots["Variance_linear"] or plots["Variance_power_fit"] or plots[ "Return_A_and_D"]: em = tp.emsd(tm, ratio_μm_px, fps, max_lagtime=225) if plots["Variance_linear"]: plt.figure("Variance_linear_fit_" + case[2]) #plt.title("Variance linear fit case: " + case[2]) plt.plot(em.index, em, 'ko', alpha=0.5) plt.ylabel(r'$\langle \Delta r^2 \rangle$ [$\mu$m$^2$]', fontsize=size), plt.xlabel('time $t$ [s]', fontsize=size) plt.gca().spines['top'].set_visible(False) plt.gca().spines['right'].set_visible(False) plt.tick_params(axis="both", which="major", labelsize=size) plt.tick_params(axis="both", which="minor", labelsize=size) plt.xscale('log') plt.yscale('log') plt.ylim(1e-2, 50) plt.savefig(res_path + "Variance_linear_fit_" + case[2] + ".png", bbox_inches="tight", pad_inches=0) if plots["Variance_power_fit"]: fig = plt.figure("Variance_power_fit_" + case[2]) ax1 = fig.add_subplot() tp.utils.fit_powerlaw(em, ax=ax1, color="k", alpha=0.5) #ax1.set_title("Variance power fitted case: " + case[2]) ax1.set_ylabel(r'$\langle \Delta r^2 \rangle$ [$\mu$m$^2$]', fontsize=size) ax1.set_xlabel('time $t$ [s]', fontsize=size) plt.gca().spines['top'].set_visible(False) plt.gca().spines['right'].set_visible(False) ax1.tick_params(axis="both", which="major", labelsize=size) ax1.tick_params(axis="both", which="minor", labelsize=size) fig.savefig(res_path + "Variance_power_fit_" + case[2] + ".png", bbox_inches="tight", pad_inches=0) plt.close(fig) if plots["Hydrodynamic_radius_filtered"]: im = tp.imsd(tm, ratio_μm_px, fps, max_lagtime=225) r_h = [] count = 0 im = im.rename_axis("ID").values for index in range(1, len(im[0])): if isinstance(im[40][index], float) and isinstance( im[8][index], float): D = (im[40][index] - im[8][index]) / (4 * (40 - 8) / fps) * 10**(-12) if isinstance(D, float): r_h += [abs(10**6 * (k_b * T) / (6 * np.pi * μ * D))] if 0 < abs(10**6 * (k_b * T) / (6 * np.pi * μ * D)) < 6: count += 1 print("In interval: ", count, "Total: ", len(r_h), "Ratio: ", count / len(r_h)) plt.figure("Hydrodynamic_radius_filtered_" + case[2]) plt.hist(r_h, bins=int(count / 3), color="k", alpha=0.5, range=(0, 6)) #plt.title("Hydrodynamic radius filtered case: "+ case[2]) plt.ylabel("Trajectories", fontsize=size) plt.xlabel("Hydrodynamic radius [μm]", fontsize=size) plt.gca().spines['top'].set_visible(False) plt.gca().spines['right'].set_visible(False) plt.tick_params(axis="both", which="major", labelsize=size) plt.tick_params(axis="both", which="minor", labelsize=size) plt.savefig(res_path + "Hydrodynamic_radius_filtered" + case[2] + ".png", bbox_inches="tight", pad_inches=0) plt.close("all") if plots["Return_A_and_D"]: A = tp.utils.fit_powerlaw(em, ax=ax1, color="k", alpha=0.5)["A"] * 10**(-12) print(tp.utils.fit_powerlaw(em, ax=ax1, color="k", alpha=0.5)) print("For case ", case[2], " A=", A, ", and D=", A / 4, ".")
def main(arg): parser = argparse.ArgumentParser() parser.add_argument( "top", help="top directory where the tiff images are located: /data/") parser.add_argument("start", nargs='?', const=1, type=int, default=1, help="index of the first image: 10001 (default 1)") args = parser.parse_args() top = args.top index_start = int(args.start) # Total number of images to read nfile = len(fnmatch.filter(os.listdir(top), '*.tif')) # Read the raw data rdata = hspeed.load_raw(top, index_start) particle_bed_reference = hspeed.particle_bed_location(rdata[0], plot=False) print("Particle bed location: ", particle_bed_reference) # Cut the images to remove the particle bed cdata = rdata[:, 0:particle_bed_reference, :] # Find the image when the shutter starts to close dark_index = hspeed.shutter_off(rdata) print("Shutter CLOSED on image: ", dark_index) # Find the images when the laser is on laser_on_index = hspeed.laser_on(rdata, particle_bed_reference, alpha=1.00) print("Laser ON on image: ", laser_on_index) # Set the [start, end] index of the blocked images, flat and dark. laser_on_index = 47 flat_range = [0, 1] data_range = [laser_on_index, dark_index] dark_range = [dark_index, nfile] flat = cdata[flat_range[0]:flat_range[1], :, :] proj = cdata[data_range[0]:data_range[1], :, :] dark = np.zeros( (dark_range[1] - dark_range[0], proj.shape[1], proj.shape[2])) # if you want to use the shutter closed images as dark uncomment this: #dark = cdata[dark_range[0]:dark_range[1], :, :] # ndata = tomopy.normalize(proj, flat, dark) # ndata = tomopy.normalize_bg(ndata, air=ndata.shape[2]/2.5) # ndata = tomopy.minus_log(ndata) # hspeed.slider(ndata) # ndata = hspeed.scale_to_one(ndata) # ndata = hspeed.sobel_stack(ndata) # hspeed.slider(ndata) ndata = tomopy.normalize(proj, flat, dark) ndata = tomopy.normalize_bg(ndata, air=ndata.shape[2] / 2.5) ndata = tomopy.minus_log(ndata) blur_radius = 3.0 threshold = .04 nddata = hspeed.label(ndata, blur_radius, threshold) f = tp.locate(ndata[100, :, :], 41, invert=True) print(f.head) plt.figure() # make a new figure tp.annotate(f, ndata[100, :, :])