def initialShapeFilter(self): # openRedMIP = self.openImage_runnable(self.cellData.stack_channel_images[self.cellData.channels[1]][0]) #runs an opening to amplify separation between cells gaussianredmip = trackpy.bandpass( self.cellData.stack_channel_images[self.cellData.channels[1]][0], 1, 27) gaussianredmip = gaussian( self.cellData.stack_channel_images[self.cellData.channels[1]][0], sigma=7) gaussianredmip = laplace(gaussianredmip) #gaussian smoothing for binary mask binary_gaussian_red = shapeFilter.getBinary_runnable( gaussianredmip, use_percentile=True, percentile=0.5) self.cellData.saveImages(binary_gaussian_red.astype(numpy.uint16), self.cellData.basedir, 'Labeled_Binary_Red', 'binary_mip') #creates binary mask using otsu binary_gaussian_red = shapeFilter.labelBinaryImage_runnable( binary_gaussian_red) self.cellData.saveImages(binary_gaussian_red.astype(numpy.uint16), self.cellData.basedir, 'Labeled_Binary_Red', 'initial_labeling') binary_gaussian_red = shapeFilter.areaFilter_runnable( binary_gaussian_red) self.cellData.saveImages(binary_gaussian_red.astype(numpy.uint16), self.cellData.basedir, 'Labeled_Binary_Red', 'binary_opened_mip') #removes small objects from binary mask Image_properties, binary_gaussian_red = shapeFilter.getImageCoordinates_runnable( binary_gaussian_red) # gets properties of labeled binary objects for i in range(len(Image_properties)): props = Image_properties[i] if int(props.equivalent_diameter) > 40: pass else: self.cellData.labeled_properties[i] = { 'bbox': list(props.bbox), 'area': int(props.filled_area), 'y': int(props.centroid[1]), 'x': int(props.centroid[0]), 'diameter': int(props.equivalent_diameter), 'label': int(props.label) } self.cellData.processed_stack_images[self.cellData.channels[1]][ 'Labeled Binary Red'] = binary_gaussian_red self.cellData.saveImages(binary_gaussian_red.astype(numpy.uint16), self.cellData.basedir, 'Labeled_Binary_Red', 'binary_mip_labeled') self.saveMetaData(foldername='Labeled_Binary_Red')
def _bandpass(image: xr.DataArray, lshort: Number, llong: int, threshold: Number, truncate: Number) -> xr.DataArray: """Apply a bandpass filter to remove noise and background variation Parameters ---------- image : xr.DataArray lshort : float filter frequencies below this value llong : int filter frequencies above this odd integer value threshold : float zero any spots below this intensity value after background subtraction (default 0) truncate : float truncate the gaussian kernel, used by the gaussian filter, at this many standard deviations Returns ------- xr.DataArray : bandpassed image """ bandpassed = bandpass(image, lshort=lshort, llong=llong, threshold=threshold, truncate=truncate) return bandpassed
def bandpass(image, lshort, llong, threshold, truncate) -> np.ndarray: """Apply a bandpass filter to remove noise and background variation Parameters ---------- image : np.ndarray lshort : float filter frequencies below this value llong : int filter frequencies above this odd integer value threshold : float zero any pixels below this intensity value truncate : float # todo document Returns ------- np.ndarray : bandpassed image """ bandpassed = bandpass( image, lshort=lshort, llong=llong, threshold=threshold, truncate=truncate ) return bandpassed
def _bandpass(image: np.ndarray, lshort: Number, llong: int, threshold: Number, truncate: Number) -> np.ndarray: """Apply a bandpass filter to remove noise and background variation Parameters ---------- image : np.ndarray lshort : float filter frequencies below this value llong : int filter frequencies above this odd integer value threshold : float zero any pixels below this intensity value truncate : float truncate the gaussian kernel, used by the gaussian filter, at this many standard deviations Returns ------- np.ndarray : bandpassed image """ bandpassed: np.ndarray = bandpass(image, lshort=lshort, llong=llong, threshold=threshold, truncate=truncate) return preserve_float_range(bandpassed)
def test_refine(self): coords_v0 = self.expected_find_bp image_bp = tp.bandpass(self.v0_inverted, **self.bandpass_params) df = tp.refine_com(self.v0_inverted, image_bp, coords=coords_v0, **self.refine_params) actual = df[df['mass'] >= self.minmass][self.pos_columns].values assert_allclose(actual, self.expected_refine)
import trackpy import trackpy.diag trackpy.__version__ #trackpy.diag.dependencies() # In[ ]: shot = 235 pic = 18333 v = pims.TiffStack( '/Users/pinghanchu/Documents/Git/Data/Shot{}/Shot{}_Cam_{}.tif'.format( shot, shot, pic)) size = len(v) v0 = tp.bandpass(v[0], 0, 300, threshold=5, truncate=4) imsave( "/Users/pinghanchu/Documents/Git/Data/Shot{}/Clean_Data_Shot{}_Cam_{}/FrameL0.tif" .format(shot, shot, pic), v0) zero = tp.bandpass(v[300], 0, 300, threshold=5, truncate=10) - v0 zero[zero > 0] = 0 zero[zero < 0] = 0 a = zero b = zero comb_index = 1 run_range = int(size / comb_index) for iv1 in range(0, run_range): b = zero for iv2 in range(0, comb_index): iv = iv1 * comb_index + iv2 vi = tp.bandpass(v[iv], 0, 300, threshold=5, truncate=10) - v0
def test_find_bp(self): image_bp = tp.bandpass(self.v0_inverted, **self.bandpass_params) actual = tp.grey_dilation(image_bp, **self.find_params) assert_array_equal(actual, self.expected_find_bp)
import pims version = 'VERSION' # adjust this pos_columns = ['y', 'x'] char_columns = ['mass', 'size', 'ecc', 'signal', 'raw_mass', 'ep'] testpath = os.path.join(os.path.dirname(tp.__file__), 'tests') impath = os.path.join(testpath, 'video', 'image_sequence', '*.png') npzpath = os.path.join(testpath, 'data', 'reproducibility_v{}.npz'.format(version)) v = pims.ImageSequence(impath) # take reader that provides uint8! assert np.issubdtype(v.dtype, np.uint8) v0 = tp.invert_image(v[0]) v0_bp = tp.bandpass(v0, lshort=1, llong=9) expected_find = tp.grey_dilation(v0, separation=9) expected_find_bandpass = tp.grey_dilation(v0_bp, separation=9) expected_refine = tp.refine_com(v0, v0_bp, radius=4, coords=expected_find_bandpass) expected_refine = expected_refine[expected_refine['mass'] >= 140] expected_refine_coords = expected_refine[pos_columns].values expected_locate = tp.locate(v0, diameter=9, minmass=140) expected_locate_coords = expected_locate[pos_columns].values df = tp.locate(v0, diameter=9) df = df[(df['x'] < 64) & (df['y'] < 64)] expected_characterize = df[pos_columns + char_columns].values f = tp.batch(tp.invert_image(v), 9, minmass=140)
import pims version = 'VERSION' # adjust this pos_columns = ['y', 'x'] char_columns = ['mass', 'size', 'ecc', 'signal', 'raw_mass', 'ep'] testpath = os.path.join(os.path.dirname(tp.__file__), 'tests') impath = os.path.join(testpath, 'video', 'image_sequence', '*.png') npzpath = os.path.join(testpath, 'data', 'reproducibility_v{}.npz'.format(version)) v = pims.ImageSequence(impath) # take reader that provides uint8! assert np.issubdtype(v.dtype, np.uint8) v0 = tp.invert_image(v[0]) v0_bp = tp.bandpass(v0, lshort=1, llong=9) expected_find = tp.grey_dilation(v0, separation=9) expected_find_bandpass = tp.grey_dilation(v0_bp, separation=9) expected_refine = tp.refine_com(v0, v0_bp, radius=4, coords=expected_find_bandpass) expected_refine = expected_refine[expected_refine['mass'] >= 140] expected_refine_coords = expected_refine[pos_columns].values expected_locate = tp.locate(v0, diameter=9, minmass=140) expected_locate_coords = expected_locate[pos_columns].values df = tp.locate(v0, diameter=9) df = df[(df['x'] < 64) & (df['y'] < 64)] expected_characterize = df[pos_columns + char_columns].values f = tp.batch(tp.invert_image(v), 9, minmass=140) f_crop = f[(f['x'] < 320) & (f['x'] > 280) & (f['y'] < 280) & (f['x'] > 240)] f_linked = tp.link(f_crop, search_range=5, memory=0)