def segmentation_and_detection(image, mask, sd_threshold=5, min_size=50, s1=12, s2=5, min_treshold=None): ''' function to perform all necessary steps for segmentation and detection. :param image: image: 2 dimensional array representing an image :param mask: optional area which to use for thresholding and segmentation :param sd_threshold: factor of how many standard deviations from the mean the threshold will be set :param min_size: minimal size of allowed objects. Any area below this size will not return a detection :param s1: lower standard deviation for difference of gaussian filter :param s2: higher standard deviation for difference of gaussian filter :param min_treshohld: optional minimal value for the threshold :return: detections: list of x,y positions of the center of detected objects ''' image1 = normalize(image, lb=0.1, ub=99.99) # normalizing img_gauss = gaussian(image1, sigma=s1) - gaussian( image1, sigma=s2) # broadband filter mask_bugs, thres = segementation_sd( img_gauss, f=sd_threshold, mask_area=mask, min_treshold=min_treshold) # segementation detections, labels = detection(mask_bugs, min_size=min_size) return detections
def copyGradDetails(a,b,e,scale=15): n = len(a) labels = labelConnected(e) out = a.astype(float).copy() for i in range(labels[-1]+1): mask = (labels==i).flatten() out[mask,:] = gaussian(b[mask,:], scale, mode="wrap", axis=0) + a[mask,:] - gaussian(a[mask,:], scale, mode="wrap", axis=0) return out
def smooth_gradient(self, sigma=40): #most fluctuations in gradient are of a frequency of 1 or two array elements. Want to smooth out these. #therefore we want a width of the array element space gaussian of much larger than this, but which is less than or equal to the width of peak we want to see self.gradient = gaussian(self.gradient, sigma)
def random_gaussian_blur(image, n, seed=None): blured = [] for i in range(n): x = np.random.randint(0, len(image)) blured += [x] if not x in blured: image[x] = gaussian(image[x], sigma=1) return image
def find_correct_gaussian_scale(self): #function convolves several gaussians with different sigma across the gradient line. #find sigma which fits gaussian best. This should give the largest peak in the convolution. #the peak value of this largest peak is taken as the time at which the concentration of the drug is half of its final concentration self.peak_max_val = -1 self.peak_max_arg = 0 self.peak_begin_frame = 0 for i in range(5, 30, 5): tic = time.time() gradient = gaussian(self.gradient[:-100], i) #clipped the gradient away from the last few frames as sometimes the intensity increases significantly at the end and the start of the experiment will not be this close to the end of the video anyway new_peak_val = np.max(gradient) new_peak_arg = np.argmax(gradient) if new_peak_val > self.peak_max_val: self.peak_max_val = new_peak_val self.peak_max_arg = new_peak_arg self.width = i toc = time.time() print('This has taken %.4f' % (toc - tic)) self.gradient = gaussian(self.gradient, self.width) #find minimum just before maximum, to find the frame at which we should look for vesicles delta = 0 grad_peak2firstval = self.gradient[:self.peak_max_arg][::-1] for num in range(grad_peak2firstval.shape[0] - 1): newdelta = grad_peak2firstval[num + 1] - grad_peak2firstval[num] print(newdelta) if abs(newdelta) < 0.01: firstmin = num self.peak_begin_frame = self.peak_max_arg - firstmin return
def create_image(pos, sigma, n_xyz): """ create_image(pos, sigma) Create Gaussian convoluted image from a set of bead positions Parameter --------- pos: array_like (float), shape=(n_bead) Bead positions along n_dim dimension sigma: float Standard deviation of Gaussian distribution n_xyz: tuple (int); shape(n_dim) Number of pixels in each image dimension Returns ------- histogram: array_like (int); shape=(n_x, n_y) Discretised distribution of pos_x and pos_y image: array_like (float); shape=(n_x, n_y) Convoluted SHG imitation image """ n_dim = len(n_xyz) "Discretise data" histogram, edges = np.histogramdd(pos.T, bins=n_xyz) if n_dim == 2: histogram = histogram.T elif n_dim == 3: histogram = ut.reorder_array(histogram) from skimage import filters import time image_shg = filters.gaussian(histogram, sigma=sigma, mode='wrap') image_shg /= np.max(image_shg) return histogram, image_shg
def plot_echo(return_value): emission_samples = return_value['emission_samples'] delays = return_value['delays'] echo_window = return_value['echo_window'] echo_sequence = return_value['echo_sequence'] windowed_echo_sequence = return_value['windowed_echo_sequence'] impulse_time = return_value['impulse_result']['impulse_time'] impulse_response = return_value['impulse_result']['impulse_response'] impulse_response_db = pa2db(impulse_response) max_value = numpy.max(numpy.abs(echo_sequence)) low_pass = gaussian(impulse_response_db, int(emission_samples/2)) low_pass = (low_pass/ numpy.max(low_pass)) * numpy.max(impulse_response_db) pyplot.figure() pyplot.subplot(3, 1, 1) pyplot.plot(impulse_time, impulse_response_db) pyplot.plot(impulse_time, low_pass) pyplot.title('Impulse response') pyplot.xlabel('Time') pyplot.ylabel('dB_spl') pyplot.subplot(3, 1, 2) pyplot.plot(impulse_time, echo_sequence) pyplot.vlines(delays, -max_value, max_value, 'red') pyplot.title('Echo Sequence') pyplot.xlabel('Time') pyplot.ylabel('Pa') pyplot.subplot(3, 1, 3) pyplot.plot(impulse_time, windowed_echo_sequence) pyplot.plot(impulse_time, echo_window * max_value, 'green') pyplot.title('Windowed echo') pyplot.xlabel('Time') pyplot.ylabel('Pa') pyplot.show()
sess, save_path= "/home/amax/SIAT/Full-Pixel-Deep-Learning-for-SMLM/Net_Model/DeepSTORM_v2_100.ckpt" ) xy = np.zeros([1, 3]) yy = 0 maxx = np.zeros(batch_count) count = 0 # for i in np.random.randint(0,batch_count,20): for i in range(batch_count): t0 = tm.time() start = i * batch_size end = (i + 1) * batch_size input = np.squeeze(test_images[start:end]) input_f = filters.gaussian(input, 1) shape_x, shape_y = input_f.shape input_x = np.reshape(input_f, newshape=[1, shape_x, shape_y, 1]) image = sess.run([yp], feed_dict={x: input_x, is_training: False}) image = np.squeeze(image[0]) yy = yy + image raw_output = sess.run([yp], feed_dict={ x: test_images[start:end], is_training: False }) raw_output = np.squeeze(raw_output[0]) # plt.subplot(221) # plt.imshow(input)
image = plt.imread("/home/andy/Desktop/rasperry_project_bugs/rec0014.jpeg") #mask=np.load("/home/andy/Desktop/rasperry_project_bugs/mask.npy") # grey scale conversion, could use some wights here though if len(image.shape) == 3: image = np.mean(image, axis=2) mask = np.load("/home/andy/Desktop/rasperry_project_bugs/mask.npy") image1 = normalize(image, lb=0.1, ub=99.9) #plt.figure() #plt.imshow(image) #plt.figure() #plt.imshow(image1) img_gauss = gaussian(image1, sigma=12) - gaussian( image1, sigma=5) ## note: this depends on the reesolution!!! #plt.figure() #plt.imshow(img_gauss) #plt.figure(); #plt.hist(img_gauss[mask],bins=100) #plt.figure(); #plt.imshow(mask) # segementation #thres=threshold_otsu(img_gauss[mask]) ## otsu not so great: to little signal values #mask_bugs=img_gauss>thres #plt.figure() #plt.imshow(mask_bugs) # this works reasonabbly well..
def filter_signals_gaussian(sigma=10): """Returns a function that smoothes signals using a Gaussian kernel with the specified sigma.""" from scipy.ndimage.filters import gaussian_filter1d as gaussian return lambda *signals: (gaussian(signal, sigma=sigma) for signal in signals)
def __call__(self): if self.ctx.params.background_model == 'median': bg_modelx = np.ma.median(self.ctx.tod_vx, axis=1)[:, np.newaxis] bg_modely = np.ma.median(self.ctx.tod_vy, axis=1)[:, np.newaxis] if self.ctx.params.background_model == 'smooth': # more aggressive settings chi_1 = 3 sm_kwars = sum_threshold.get_sm_kwargs(kernel_m = 12, kernel_n = 150, sigma_m = 2, sigma_n = 25) di_kwargs = sum_threshold.get_di_kwrags(self.ctx.params.struct_size_0, self.ctx.params.struct_size_1) mask_gal = self.ctx.simulation_mask mask_originalx = self.ctx.tod_vx.mask # to save time only run this once, the second polarization is the same as the first mask_newx2 = mask_originalx + mask_galaxy(self.ctx.params.nside, mask_originalx, mask_gal, self.ctx.coords.ra, self.ctx.coords.dec) mask_newy2 = mask_newx2.copy() # TODO: mask point sources # mask_newx = mask_point_source(mask_newx) # mask_newy = mask_point_source(mask_newy) mask_newx2 = sum_threshold.get_rfi_mask(self.ctx.tod_vx, mask=mask_newx2, chi_1 = chi_1, eta_i=[1], plotting=False, sm_kwargs=sm_kwars, di_kwargs=di_kwargs) mask_newy2 = mask_newx2.copy() # some of the time-axes are shorter than the TOD, need to fix this! if len(self.ctx.time_axis)==len(mask_newx2[0]): time_axis = self.ctx.time_axis.copy() else: print('wrong time-axis length!') time_axis = np.arange(mask_newx2.shape[1]) # this is a rough mask for the ocasional very low values, need to improve on this median_x = np.ma.median(self.ctx.tod_vx, axis=1)[:, np.newaxis] median_y = np.ma.median(self.ctx.tod_vx, axis=1)[:, np.newaxis] mask_newx2[(self.ctx.tod_vx.data < (median_x - 50))] = True mask_newy2[(self.ctx.tod_vy.data < (median_y - 50))] = True bg_modelx = np.zeros(mask_newx2.shape) bg_modely = np.zeros(mask_newy2.shape) for i in range(len(bg_modelx)): mask_size = np.sum(~mask_newx2[i]) if (mask_size > 100): y = np.interp(time_axis, time_axis[mask_newx2[i]], self.ctx.tod_vx.data[i][mask_newx2[i]], left=self.ctx.tod_vx.data[i][mask_newx2[i]][0], right=self.ctx.tod_vx.data[i][mask_newx2[i]][-1]) # smoothing around 1hr-time scale bg_modelx[i] = gaussian(y, sigma=600) bg_modely[i] = gaussian(y, sigma=600) elif (mask_size > 0): bg_modelx[i, :] = np.median(self.ctx.tod_vx.data[i][mask_newx2[i]]) bg_modely[i, :] = np.median(self.ctx.tod_vy.data[i][mask_newy2[i]]) # make diagnostic plot # import pylab as mplot # mplot.figure() # mplot.plot(self.ctx.tod_vx.data[0]) # temp = self.ctx.tod_vx.data[0] # temp[mask_newx2[0]==1] = 'nan' # mplot.plot(temp) # mplot.plot(bg_modelx[0]) # mplot.ylim(30,80) # mplot.title(str(self.ctx.file_paths[0][-20:])+'\n'+str(self.ctx.coords.ra[0]/np.pi*180)[:5]) # # mplot.show() # subtract background self.ctx.tod_vx.bg = bg_modelx self.ctx.tod_vy.bg = bg_modely self.ctx.tod_vx -= bg_modelx self.ctx.tod_vy = self.ctx.tod_vx.copy()
def __call__(self): if self.ctx.params.background_model == 'median': bg_modelx = np.ma.median(self.ctx.tod_vx, axis=1)[:, np.newaxis] bg_modely = np.ma.median(self.ctx.tod_vy, axis=1)[:, np.newaxis] if self.ctx.params.background_model == 'smooth': # more aggressive settings chi_1 = 3 sm_kwars = sum_threshold.get_sm_kwargs(kernel_m=12, kernel_n=150, sigma_m=2, sigma_n=25) di_kwargs = sum_threshold.get_di_kwrags( self.ctx.params.struct_size_0, self.ctx.params.struct_size_1) mask_gal = self.ctx.simulation_mask mask_originalx = self.ctx.tod_vx.mask # to save time only run this once, the second polarization is the same as the first mask_newx2 = mask_originalx + mask_galaxy( self.ctx.params.nside, mask_originalx, mask_gal, self.ctx.coords.ra, self.ctx.coords.dec) mask_newy2 = mask_newx2.copy() # TODO: mask point sources # mask_newx = mask_point_source(mask_newx) # mask_newy = mask_point_source(mask_newy) mask_newx2 = sum_threshold.get_rfi_mask(self.ctx.tod_vx, mask=mask_newx2, chi_1=chi_1, eta_i=[1], plotting=False, sm_kwargs=sm_kwars, di_kwargs=di_kwargs) mask_newy2 = mask_newx2.copy() # some of the time-axes are shorter than the TOD, need to fix this! if len(self.ctx.time_axis) == len(mask_newx2[0]): time_axis = self.ctx.time_axis.copy() else: print('wrong time-axis length!') time_axis = np.arange(mask_newx2.shape[1]) # this is a rough mask for the ocasional very low values, need to improve on this median_x = np.ma.median(self.ctx.tod_vx, axis=1)[:, np.newaxis] median_y = np.ma.median(self.ctx.tod_vx, axis=1)[:, np.newaxis] mask_newx2[(self.ctx.tod_vx.data < (median_x - 50))] = True mask_newy2[(self.ctx.tod_vy.data < (median_y - 50))] = True bg_modelx = np.zeros(mask_newx2.shape) bg_modely = np.zeros(mask_newy2.shape) for i in range(len(bg_modelx)): mask_size = np.sum(~mask_newx2[i]) if (mask_size > 100): y = np.interp( time_axis, time_axis[mask_newx2[i]], self.ctx.tod_vx.data[i][mask_newx2[i]], left=self.ctx.tod_vx.data[i][mask_newx2[i]][0], right=self.ctx.tod_vx.data[i][mask_newx2[i]][-1]) # smoothing around 1hr-time scale bg_modelx[i] = gaussian(y, sigma=600) bg_modely[i] = gaussian(y, sigma=600) elif (mask_size > 0): bg_modelx[i, :] = np.median( self.ctx.tod_vx.data[i][mask_newx2[i]]) bg_modely[i, :] = np.median( self.ctx.tod_vy.data[i][mask_newy2[i]]) # make diagnostic plot # import pylab as mplot # mplot.figure() # mplot.plot(self.ctx.tod_vx.data[0]) # temp = self.ctx.tod_vx.data[0] # temp[mask_newx2[0]==1] = 'nan' # mplot.plot(temp) # mplot.plot(bg_modelx[0]) # mplot.ylim(30,80) # mplot.title(str(self.ctx.file_paths[0][-20:])+'\n'+str(self.ctx.coords.ra[0]/np.pi*180)[:5]) # # mplot.show() # subtract background self.ctx.tod_vx.bg = bg_modelx self.ctx.tod_vy.bg = bg_modely self.ctx.tod_vx -= bg_modelx self.ctx.tod_vy = self.ctx.tod_vx.copy()