def update_image(self): if self.ShowCC: CC = cv_funcs.xcorr(self.template_data.get_data('imagedata'), self.get_active_image()) self.plotdata.set_data("imagedata", CC) self.plot = self.get_scatter_overlay_plot( array_plot_data=self.plotdata, title=self.get_active_name(), tools=['csr', 'zoom', 'pan', 'colorbar'], ) self.plot.aspect_ratio = (float(CC.shape[1]) / CC.shape[0]) self.set_plot_title("Cross correlation of " + self.get_active_name()) grid_data_source = self._base_plot.range2d.sources[0] grid_data_source.set_data(np.arange(CC.shape[1]), np.arange(CC.shape[0])) else: self.plotdata.set_data("imagedata", self.get_active_image()) self.plot = self.get_scatter_overlay_plot( array_plot_data=self.plotdata, title=self.get_active_name(), tools=['csr', 'zoom', 'pan', 'colorbar'], ) self.plot.aspect_ratio = (float(self.get_active_image().shape[1]) / self.get_active_image().shape[0]) self.set_plot_title(self.get_active_name()) grid_data_source = self._base_plot.range2d.sources[0] grid_data_source.set_data( np.arange(self.get_active_image().shape[1]), np.arange(self.get_active_image().shape[0]))
def update_image(self): if self.ShowCC: CC = cv_funcs.xcorr(self.template_data.get_data('imagedata'), self.get_active_image()) self.plotdata.set_data("imagedata",CC) self.plot = self.get_scatter_overlay_plot(array_plot_data=self.plotdata, title=self.get_active_name(), tools=['csr','zoom','pan', 'colorbar'], ) self.plot.aspect_ratio = (float(CC.shape[1])/ CC.shape[0]) self.set_plot_title("Cross correlation of " + self.get_active_name()) grid_data_source = self._base_plot.range2d.sources[0] grid_data_source.set_data(np.arange(CC.shape[1]), np.arange(CC.shape[0])) else: self.plotdata.set_data("imagedata", self.get_active_image()) self.plot = self.get_scatter_overlay_plot(array_plot_data=self.plotdata, title=self.get_active_name(), tools=['csr','zoom','pan', 'colorbar'], ) self.plot.aspect_ratio = (float(self.get_active_image().shape[1])/ self.get_active_image().shape[0]) self.set_plot_title(self.get_active_name()) grid_data_source = self._base_plot.range2d.sources[0] grid_data_source.set_data(np.arange(self.get_active_image().shape[1]), np.arange(self.get_active_image().shape[0]))
def locate_peaks(self): peaks = {} for idx in xrange(self.numfiles): self.set_active_index(idx) CC = cv_funcs.xcorr(self.template_data.get_data("imagedata"), self.get_active_image()) # pks = pc.two_dim_findpeaks((CC - CC.min()) * 255, xc_filter=False) pks[:, 2] = pks[:, 2] / 255 + CC.min() peaks[self.get_active_name()] = pks self.peaks = peaks
def locate_peaks(self): peaks={} for idx in xrange(self.numfiles): self.set_active_index(idx) CC = cv_funcs.xcorr(self.template_data.get_data("imagedata"), self.get_active_image()) # pks=pc.two_dim_findpeaks((CC-CC.min())*255, xc_filter=False) pks[:,2]=pks[:,2]/255+CC.min() peaks[self.get_active_name()]=pks self.peaks=peaks
def locate_peaks(self): peaks={} for idx in xrange(self.numfiles): self.set_active_index(idx) CC = cv_funcs.xcorr(self.template_data.get_data("imagedata"), self.get_active_image()) # pks=pc.two_dim_findpeaks((CC-CC.min())*255, medfilt_radius=None, alpha=1, coords_list=[], ) pks=pc.flatten_peak_list(pks) pks[:,2]=pks[:,2]/255+CC.min() peaks[self.get_active_name()]=pks self.peaks=peaks
def two_dim_findpeaks(image, peak_width=None, medfilt_radius=5, xc_filter=True, kill_edges=True, kill_duplicates=True): """ Takes an image and detect the peaks using the local maximum filter. Returns a boolean mask of the peaks (i.e. 1 when the pixel's value is the neighborhood maximum, 0 otherwise) Code by Ivan: http://stackoverflow.com/questions/3684484/peak-detection-in-a-2d-array Returns a 2D numpy array with one row per peak, two columns (X index first) """ from scipy.ndimage.filters import maximum_filter from scipy.ndimage.morphology import generate_binary_structure, binary_erosion, \ iterate_structure from scipy.ndimage import gaussian_filter from analyzarr.lib.cv.cv_funcs import xcorr if medfilt_radius is not None: image = medfilt(image, medfilt_radius) # blur it; sharp edges seem to mess up peak finding image = gaussian_filter(image, medfilt_radius) if peak_width is None: peak_width = estimate_peak_width(image, medfilt=medfilt_radius) if xc_filter: # the normal gaussian xg, yg = np.mgrid[0:peak_width, 0:peak_width] def gaussian(height, center_x, center_y, width_x, width_y): """Returns a gaussian function with the given parameters""" width_x = float(width_x) width_y = float(width_y) return lambda x, y: height * np.exp(-(( (center_x - x) / width_x)**2 + ( (center_y - y) / width_y)**2) / 2) templateImage = gaussian(255, (peak_width / 2) + 1, (peak_width / 2) + 1, (peak_width / 4) + 1, peak_width / 4 + 1)(xg, yg) cleaned_image = xcorr(templateImage, image) else: cleaned_image = image #medfilt(image, medfilt_radius) #peak_width=estimate_peak_width(cleaned_image,medfilt=None, # max_peak_width=max_peak_width) # define an 8-connected neighborhood neighborhood = generate_binary_structure(2, 1) neighborhood = iterate_structure(neighborhood, int(peak_width / 4)) #apply the local maximum filter; all pixel of maximal value #in their neighborhood are set to 1 local_max = maximum_filter(cleaned_image, footprint=neighborhood) == cleaned_image #local_max is a mask that contains the peaks we are #looking for, but also the background. #In order to isolate the peaks we must remove the background from the mask. #we create the mask of the background background = (cleaned_image == 0) #a little technicality: we must erode the background in order to #successfully subtract it form local_max, otherwise a line will #appear along the background border (artifact of the local maximum filter) eroded_background = binary_erosion(background, structure=neighborhood, border_value=1) #we obtain the final mask, containing only peaks, #by removing the background from the local_max mask detected_peaks = local_max - eroded_background # convert the mask to indices: detected_peaks = detected_peaks.nonzero() # format the two arrays into one detected_peaks = np.vstack((detected_peaks[0], detected_peaks[1])).T if kill_duplicates: detected_peaks = _kill_duplicates(detected_peaks) if kill_edges: detected_peaks = _kill_edges(image, detected_peaks, peak_width / 8) # translate to actual image peak, instead of cross correlation peak if xc_filter: detected_peaks = detected_peaks + peak_width / 2 # get peak heights as third column heights = np.array([image[pk[0], pk[1]] for pk in detected_peaks]) peaks = np.hstack((detected_peaks, heights.reshape((-1, 1)))) return peaks
def update_CC(self): if self.ShowCC: CC = cv_funcs.xcorr(self.template_data.get_data('imagedata'), self.get_active_image()) self.plotdata.set_data("imagedata",CC)
def update_CC(self): if self.ShowCC: CC = cv_funcs.xcorr(self.template_data.get_data('imagedata'), self.get_active_image()) self.plotdata.set_data("imagedata", CC)
def two_dim_findpeaks(image, peak_width=None, medfilt_radius=5, xc_filter=True, kill_edges=True, kill_duplicates=True): """ Takes an image and detect the peaks using the local maximum filter. Returns a boolean mask of the peaks (i.e. 1 when the pixel's value is the neighborhood maximum, 0 otherwise) Code by Ivan: http://stackoverflow.com/questions/3684484/peak-detection-in-a-2d-array Returns a 2D numpy array with one row per peak, two columns (X index first) """ from scipy.ndimage.filters import maximum_filter from scipy.ndimage.morphology import generate_binary_structure, binary_erosion, \ iterate_structure from scipy.ndimage import gaussian_filter from analyzarr.lib.cv.cv_funcs import xcorr if medfilt_radius is not None: image = medfilt(image, medfilt_radius) # blur it; sharp edges seem to mess up peak finding image = gaussian_filter(image, medfilt_radius) if peak_width is None: peak_width=estimate_peak_width(image,medfilt=medfilt_radius) if xc_filter: # the normal gaussian xg, yg = np.mgrid[0:peak_width, 0:peak_width] def gaussian(height, center_x, center_y, width_x, width_y): """Returns a gaussian function with the given parameters""" width_x = float(width_x) width_y = float(width_y) return lambda x,y: height*np.exp( -(((center_x-x)/width_x)**2+((center_y-y)/width_y)**2)/2) templateImage = gaussian(255, (peak_width/2)+1, (peak_width/2)+1, (peak_width/4)+1, peak_width/4+1)(xg, yg) cleaned_image = xcorr(templateImage, image) else: cleaned_image=image #medfilt(image, medfilt_radius) #peak_width=estimate_peak_width(cleaned_image,medfilt=None, # max_peak_width=max_peak_width) # define an 8-connected neighborhood neighborhood = generate_binary_structure(2,1) neighborhood = iterate_structure(neighborhood, int(peak_width/4)) #apply the local maximum filter; all pixel of maximal value #in their neighborhood are set to 1 local_max = maximum_filter(cleaned_image, footprint=neighborhood)==cleaned_image #local_max is a mask that contains the peaks we are #looking for, but also the background. #In order to isolate the peaks we must remove the background from the mask. #we create the mask of the background background = (cleaned_image==0) #a little technicality: we must erode the background in order to #successfully subtract it form local_max, otherwise a line will #appear along the background border (artifact of the local maximum filter) eroded_background = binary_erosion(background, structure=neighborhood, border_value=1) #we obtain the final mask, containing only peaks, #by removing the background from the local_max mask detected_peaks = local_max - eroded_background # convert the mask to indices: detected_peaks = detected_peaks.nonzero() # format the two arrays into one detected_peaks = np.vstack((detected_peaks[0],detected_peaks[1])).T if kill_duplicates: detected_peaks=_kill_duplicates(detected_peaks) if kill_edges: detected_peaks=_kill_edges(image, detected_peaks, peak_width/8) # translate to actual image peak, instead of cross correlation peak if xc_filter: detected_peaks=detected_peaks+peak_width/2 # get peak heights as third column heights = np.array([image[pk[0],pk[1]] for pk in detected_peaks]) peaks = np.hstack((detected_peaks, heights.reshape((-1,1)))) return peaks