def showmask(self, show_mask): MainDisplay.init_canvas(self) if self.skfilterButton.cget('text') == 'Hide': MainDisplay.show_filter(self) elif self.skfilterButton.cget('text') == 'Filter': MainDisplay.show_image(self) if show_mask == 0: if self.skmaskButton.cget('text') == 'Hide': self.skmaskButton.config(text='Mask') elif self.skmaskButton.cget('text') == 'Mask': MainDisplay.show_mask(self) self.skmaskButton.config(text='Hide') elif show_mask == 1: MainDisplay.show_mask(self) self._canvas.draw() self.eshowButton.config(text='Show') self.skskeletonButton.config(text='Skeleton') self.tshowlabelButton.config(text='Show Labels') self.tshowfitButton.config(text='Show Fit') self._overedge['enable'].set(0) self._overskel['enable'].set(0) self._overlabel['enable'].set(0) self._overfit['enable'].set(0)
def text_load(self, filename): with open(filename, 'r') as f: init_header = f.readline() if 'Channel' in init_header: self._img_info = FileOpen.initvar_img(self) self._img_info['type'].set('AFM') FileOpen.import_afm(self, filename) MainDisplay.init_canvas(self) MainDisplay.show_image(self)
def reset(self): self._mat_img = 1 * self._source_img self._img_info['xmin'].set(0) self._img_info['xmax'].set(int(self._img_info['sizepix_x'].get())) self._img_info['ymin'].set(0) self._img_info['ymax'].set(int(self._img_info['sizepix_y'].get())) self._img_info['vmin'].set(np.min(self._mat_img.flatten())) self._img_info['vmax'].set(np.max(self._mat_img.flatten())) MainDisplay.init_canvas(self) MainDisplay.show_image(self)
def crop_infostrip(self): if self._img_info['sizepix_y'].get() == 2207: size_infostrip = 175 elif self._img_info['sizepix_y'].get() == 1103: size_infostrip = 85 try: self._mat_img = self._source_img[:-size_infostrip, :] self._img_info['xmin'].set(0) self._img_info['xmax'].set(int(self._img_info['sizepix_x'].get())) self._img_info['ymin'].set(0) self._img_info['ymax'].set( int(int(self._img_info['sizepix_y'].get()) - size_infostrip)) self._img_info['vmin'].set(np.min(self._mat_img.flatten())) self._img_info['vmax'].set(np.max(self._mat_img.flatten())) self._tracking_step = 2 except NameError: self.imgcropinfoButton.config(text='Size not known') self.imgcropinfoButton.state(['disabled']) MainDisplay.init_canvas(self) MainDisplay.show_image(self)
def crop_selection(self): x1, x2 = self._a_img.get_xlim() y2, y1 = self._a_img.get_ylim() if int(x1) < 0: x1 = 0 if int(y1) < 0: y1 = 0 if int(x2) > self._mat_img.shape[1]: x2 = self._mat_img.shape[1] if int(y2) > self._mat_img.shape[0]: y2 = self._mat_img.shape[0] self._img_info['xmin'].set(int(x1)) self._img_info['xmax'].set(int(x2)) self._img_info['ymin'].set(int(y1)) self._img_info['ymax'].set(int(y2)) self._mat_img = self._mat_img[int(y1):int(y2), int(x1):int(x2)] self._img_info['vmin'].set(np.min(self._mat_img.flatten())) self._img_info['vmax'].set(np.max(self._mat_img.flatten())) MainDisplay.init_canvas(self) MainDisplay.show_image(self) self._tracking_step = 2
def image_load(self, filename): self._img_info = FileOpen.initvar_img(self) self._img_info['type'].set('STEM') source_image = np.array(Image.open(filename)) self._img_info['sizepix_x'].set(source_image.shape[1]) self._img_info['sizepix_y'].set(source_image.shape[0]) self._colormap_options['Source'] = 'gray' self._menucheckCO.set(0) try: self._source_img = source_image[:,:,0]; except IndexError: self._source_img = source_image[:,:] self._img_info['vmin'].set(np.min(source_image.flatten())) self._img_info['vmax'].set(np.max(source_image.flatten())) self._mat_img = 1*self._source_img MainDisplay.init_canvas(self) MainDisplay.show_image(self) self._canvas.draw()
def finalstep(self): MainDisplay.init_canvas(self) MainDisplay.show_image(self) self.progressBar.config(mode='determinate', maximum=np.max(self._im.flatten()) + 1) self.progressBar['value'] = 1 self.progressBar.update() ascale = self._tracking_settings['edge_ascale_current'].get() xm = 1 * self._xmm[self._im > 0] ym = 1 * self._ymm[self._im > 0] im = 1 * self._im[self._im > 0] m = self._m[self._im > 0] int_profile = [] pos_profile = [] full_profile = [] full_position = [] fiber_xc = [] fiber_yc = [] fiber_x1 = [] fiber_x2 = [] fiber_y1 = [] fiber_y2 = [] fiber_e1 = [] fiber_e2 = [] fiber_number = [] fiber_slope = [] xprofile = [] yprofile = [] pix_edge = int(self._tracking_settings['pixels_edge_img'].get()) pix_back = int( self._tracking_settings['pixels_background_profile'].get()) nc = 0 for icn in np.unique(im): self.progressBar['value'] = icn self.progressBar.update() xcn = xm[np.where(im == icn)] ycn = ym[np.where(im == icn)] mcn = m[np.where(im == icn)] nc = nc + 1 for ix in range(len(xcn)): print('Filament: ' + str(icn) + ' (' + str(np.unique(im)[-1]) + ') Profile: ' + str(ix + 1) + ' (' + str(len(xcn)) + ')') if (ycn[ix] >= pix_edge) and ( xcn[ix] >= pix_edge ): # if the point lies inside the confidence area determined by the picture edge to discard if mcn[ix] >= 1e6: # if the skeleton line is vertical direction = 0 x_interp = np.arange(0, self._mat_img.shape[1]) y_interp = np.array( [int(x) for x in ycn[ix] * np.ones(len(x_interp))]) offset_edge = int(ascale) + int(pix_back) elif np.abs(mcn[ix]) < 1 / self._mat_img.shape[ 1]: # if the skeleton line is horizontal direction = 1 y_interp = np.arange(0, self._mat_img.shape[0]) x_interp = np.array( [int(x) for x in xcn[ix] * np.ones(len(y_interp))]) offset_edge = int(ascale) + int(pix_back) else: # if the skeleton line is diagonal direction = 2 x_pline = np.arange(0, self._mat_img.shape[1]) y_pline = ycn[ix] + 1 / mcn[ix] * (xcn[ix] - x_pline) if y_pline[1] > y_pline[-1]: if np.max(y_pline) >= self._mat_img.shape[0]: ind_y1 = np.where(y_pline >= self._mat_img. shape[0])[0][-1] + 1 else: ind_y1 = 1 if np.min(y_pline) < 0: ind_y2 = np.where(y_pline < 0)[0][0] - 1 else: ind_y2 = len(y_pline) else: if np.max(y_pline) >= self._mat_img.shape[0]: ind_y2 = np.where(y_pline >= self._mat_img. shape[0])[0][0] - 1 else: ind_y2 = len(y_pline) if np.min(y_pline) < 0: ind_y1 = np.where(y_pline < 0)[0][-1] - 1 else: ind_y1 = 1 x_profile = [int(x) for x in x_pline[ind_y1:ind_y2]] y_profile = [int(x) for x in y_pline[ind_y1:ind_y2]] try: f_interp = interp1d(x_profile, y_profile) x_interp = np.arange(np.min(x_profile), np.max(x_profile), step=0.1) offset_edge = 10 * (int(ascale) + int(pix_back)) y_interp = f_interp(x_interp) except ValueError: x_interp = x_profile y_interp = y_profile offset_edge = int(ascale) + int(pix_back) x_profile_int = [int(x) for x in x_interp] y_profile_int = [int(x) for x in y_interp] x_profile = np.array(x_profile_int) y_profile = np.array(y_profile_int) try: xms = np.mean( np.where(np.abs(x_profile - xcn[ix]) < 2)) yms = np.mean( np.where(np.abs(y_profile - ycn[ix]) < 2)) if direction == 0: posx = int(xms) elif direction == 1: posx = int(yms) elif direction == 2: posx = int(np.mean([xms, yms])) sign_position = 0 * x_profile sign_position[:posx] = -1 sign_position[posx:] = 1 mask_profile = 0 * self._mask_edge mask_profile[y_profile[posx:], x_profile[posx:]] = 1 mask_profile[y_profile[:posx], x_profile[:posx]] = -1 mask_overlap = mask_profile * binary_dilation( self._mask_edge) try: ymatch = np.where(mask_overlap < 0)[0] xmatch = np.where(mask_overlap < 0)[1] dmatch = np.sqrt((xmatch - xcn[ix])**2 + (ymatch - ycn[ix])**2) ind_x1 = xmatch[np.argmin(dmatch)] ind_y1 = ymatch[np.argmin(dmatch)] ymatch = np.where(mask_overlap > 0)[0] xmatch = np.where(mask_overlap > 0)[1] dmatch = np.sqrt((xmatch - xcn[ix])**2 + (ymatch - ycn[ix])**2) ind_x2 = xmatch[np.argmin(dmatch)] ind_y2 = ymatch[np.argmin(dmatch)] ind_xmatch = [ind_x1, ind_x2] ind_ymatch = [ind_y1, ind_y2] try: if direction == 0: ind_1 = np.where(x_profile == ind_x1)[0][0] ind_2 = np.where(x_profile == ind_x2)[0][0] else: ind_1 = np.where(y_profile == ind_y1)[0][0] ind_2 = np.where(y_profile == ind_y2)[0][0] xmatch = x_profile[ind_1 - offset_edge:ind_2 + offset_edge] ymatch = y_profile[ind_1 - offset_edge:ind_2 + offset_edge] full_profile.append( np.array(self._mat_img[y_profile, x_profile])) int_profile.append( np.array(self._mat_img[ymatch, xmatch])) fiber_xc.append( np.mean( [x_profile[ind_1], x_profile[ind_2]])) fiber_yc.append( np.mean( [y_profile[ind_1], y_profile[ind_2]])) fiber_x1.append(x_profile[ind_1]) fiber_x2.append(x_profile[ind_2]) fiber_y1.append(y_profile[ind_1]) fiber_y2.append(y_profile[ind_2]) fiber_e1.append(self._WT_m1[fiber_y1[-1], fiber_x1[-1]]) fiber_e2.append(self._WT_m1[fiber_y2[-1], fiber_x2[-1]]) fiber_number.append(nc) fiber_slope.append(mcn[ix]) dist_full = np.sqrt( (x_profile - fiber_xc[-1])**2 + (y_profile - fiber_yc[-1])**2) * sign_position full_position.append(dist_full) pos_profile.append( dist_full[ind_1 - offset_edge:ind_2 + offset_edge]) except IndexError as e: print('error 182: ' + str(e)) except ValueError as e: print('error 183: ' + str(e)) except ValueError as e: print('error 184: ' + str(e)) mask_newlabel = 0 * self._mat_img fx = [int(x) for x in fiber_xc] fy = [int(x) for x in fiber_yc] mask_newlabel[np.array(fy), np.array(fx)] = 1 labelled_filaments = remove_small_objects(label(mask_newlabel), min_size=3) [yl, xl] = np.where(labelled_filaments) xy_l = [[xl[ix], yl[ix]] for ix in range(len(xl))] new_ind = [0 for x in fiber_xc] for ix in range(0, len(fiber_xc)): if [fx[ix], fy[ix]] in xy_l: new_ind[ix] = 1 new_ind = np.array(new_ind) ind_del = np.where(new_ind == 0)[0] self.progressBar['value'] = icn + 1 self.progressBar.update() self._full_int_profile = np.delete(np.array(full_profile), ind_del, axis=0) self._full_position = np.delete(np.array(full_position), ind_del, axis=0) self._int_profile = np.delete(np.array(int_profile), ind_del, axis=0) self._fiber_xc = np.delete(np.array(fiber_xc), ind_del, axis=0) self._fiber_yc = np.delete(np.array(fiber_yc), ind_del, axis=0) self._fiber_x1 = np.delete(np.array(fiber_x1), ind_del, axis=0) self._fiber_x2 = np.delete(np.array(fiber_x2), ind_del, axis=0) self._fiber_y1 = np.delete(np.array(fiber_y1), ind_del, axis=0) self._fiber_y2 = np.delete(np.array(fiber_y2), ind_del, axis=0) self._fiber_e1 = np.delete(np.array(fiber_e1), ind_del, axis=0) self._fiber_e2 = np.delete(np.array(fiber_e2), ind_del, axis=0) self._fiber_number = np.delete(np.array(fiber_number), ind_del, axis=0) self._fiber_slope = np.delete(np.array(fiber_slope), ind_del, axis=0) self._pos_profile = np.delete(np.array(pos_profile), ind_del, axis=0) self.progressBar['value'] = 0 self.progressBar.update()