magmap.cv.detector module#
Detects features within a 3D image stack.
Prunes duplicates and verifies detections against truth sets.
- ivar CONFIRMATION:
Dictionary of blob confirmation flags.
- ivar OVERLAP_FACTOR:
Pixel number multiplier for overlaps between adjacent ROIs.
- class magmap.cv.detector.Blobs(blobs=None, blob_matches=None, colocalizations=None, path=None, cols=None)[source]#
Bases:
objectBlob storage class.
- Variables:
blob_matches – Sequence of blob matches; defaults to None.
colocalizations – 2D Numpy array of same length as
blobswith a column for each channel, where 0 = no signal and 1 = signal at the corresponding blob’s location in the given and channel; defaults to None.path – Path from which blobs were loaded; defaults to None.
ver – Version number; defaults to
BLOBS_NP_VER.roi_offset – Offset in
z,y,xfrom ROI in which blobs were detected.roi_size – Size in
z,y,xfrom ROI in which blobs were detected.resolutions – Physical unit resolution sizes in
[[z,y,x], ...]; defaults to None.basename – Archive name, typically the filename without extension of the image file in which blobs were detected; defaults to None.
scaling – Scaling factor from the blobs’ space to the main image’s space, in
z,y,x; defaults to[1, 1, 1].
-
BLOBS_NP_VER:
int= 4# Current blobs Numpy archive version number. 0: initial version 1: added resolutions, basename, offset, roi_size fields 2: added archive version number 3: added colocs 4: added “columns” for column names
- class Cols(value)[source]#
Bases:
EnumBlob column names.
- ABS_X = 'abs_x'#
- ABS_Y = 'abs_y'#
- ABS_Z = 'abs_z'#
- CHANNEL = 'channel'#
- CONFIRMED = 'confirmed'#
- RADIUS = 'radius'#
- TRUTH = 'truth'#
- X = 'x'#
- Y = 'y'#
- Z = 'z'#
- class Keys(value)[source]#
Bases:
EnumNumpy archive metadata keys as enumerations.
- BASENAME = 'basename'#
- BLOBS = 'segments'#
- COLOCS = 'colocs'#
- COLS = 'columns'#
- RESOLUTIONS = 'resolutions'#
- ROI_OFFSET = 'offset'#
- ROI_SIZE = 'roi_size'#
- VER = 'ver'#
- classmethod blob_for_db(blob)[source]#
Convert blob to absolute coordinates.
Changes the blob format from that used within this module to that used in :module:`sqlite`, where coordinates are absolute rather than relative to the offset.
- classmethod blobs_in_channel(blobs, channel, return_mask=False)[source]#
Get blobs in the given channels.
- Parameters:
- Return type:
- Returns:
A view of the blobs in the channel, or all blobs if
channelis None. Ifreturn_maskis True, also return the mask of blobs in the given channels.
- format_blobs(channel=None)[source]#
Format blobs with the full set of fields.
Blobs in MagellanMapper can be assumed to start with
z, y, x, radiusbut should use this class’s functions to manipulate other fields to ensure that the correct columns are accessed. This function adds these fields:confirmation, truth, channel, abs z, abs y, abs x.“Confirmed” is given as -1 = unconfirmed, 0 = incorrect, 1 = correct.
“Truth” is given as -1 = not truth, 0 = not matched, 1 = matched, where a “matched” truth blob is one that has a detected blob within a given tolerance.
- load_blobs(path=None)[source]#
Load blobs from an archive.
Also loads associated metadata from the archive.
- classmethod remove_abs_blob_coords(blobs, remove_extra=False)[source]#
Remove blob absolute coordinate columns.
- classmethod replace_rel_with_abs_blob_coords(blobs)[source]#
Replace relative with absolute coordinates.
- save_archive(to_add=None, update=False)[source]#
Save the blobs Numpy archive file to
path.Backs up any existing file before saving.
- classmethod set_blob_abs_coords(blobs, coords, *args, **kwargs)[source]#
Set blob absolute coordinates.
- classmethod set_blob_channel(blob, *args, **kwargs)[source]#
Set the channel of a blob or blobs.
- Parameters:
blob (
ndarray) – 1D blob array or 2D array of blobs.args – Positional arguments passed to
set_blob_col().kwargs – Named arguments passed to
set_blob_col().
- Return type:
- Returns:
blobafter modifications.
- classmethod set_blob_col(blob, col, val, mask=slice(None, None, None), **kwargs)[source]#
Set the value for the given column of a blob or blobs.
- Parameters:
blob (
ndarray) – 1D blob array or 2D array of blobs.val (
Union[float,Sequence[float]]) – New value. Ifblobis 2D, can be an array the length ofblob.mask (
Union[ndarray,IndexExpression], default:slice(None, None, None)) – Mask for the first axis; defaults to an index expression for all values. Only used for multidimensional arrays.
- Return type:
- Returns:
blobafter modifications.
- classmethod set_blob_confirmed(blob, *args, **kwargs)[source]#
Set the confirmed flag of a blob or blobs.
- Parameters:
blob (
ndarray) – 1D blob array or 2D array of blobs.args – Positional arguments passed to
set_blob_col().kwargs – Named arguments passed to
set_blob_col().
- Return type:
- Returns:
blobafter modifications.
- classmethod set_blob_truth(blob, *args, **kwargs)[source]#
Set the truth flag of a blob or blobs.
- Parameters:
blob (
ndarray) – 1D blob array or 2D array of blobs.args – Positional arguments passed to
set_blob_col().kwargs – Named arguments passed to
set_blob_col().
- Return type:
- Returns:
blobafter modifications.
- classmethod shift_blobs(blob, cols, fn, vals, to_int=False)[source]#
Shift blob columns with a function.
- Parameters:
blob (
ndarray) – 1D blob array or 2D array of blobs.fn (
Callable) – Function that takes a subset ofblobandvals.vals (
Union[int,float,Sequence[Union[int,float]]]) – Values by which to shift the corresponding elements ofblob.to_int (
bool, default:False) – True to convert the shifted elements to int; defaults to False.
- Return type:
- Returns:
blobshifted in-place.
- magmap.cv.detector.calc_overlap(factor=None)[source]#
Calculate overlap based on scaling factor and a factor.
- magmap.cv.detector.calc_scaling_factor()[source]#
Calculates the tolerance based on the resolutions, using the first resolution.
- Returns:
Array of tolerance values in same shape as the first resolution.
:raises AttributeError` if the :attr:
config.resolutionsis None or ha: :raises less than one element.:
- magmap.cv.detector.detect_blobs(roi, channel, exclude_border=None)[source]#
Detects objects using 3D blob detection technique.
- Parameters:
- Return type:
- Returns:
Array of detected blobs, each given as
z, row, column, radius, confirmation.
- magmap.cv.detector.get_blobs_in_roi(blobs, offset, size, margin=(0, 0, 0), reverse=True)[source]#
Get blobs within an ROI based on offset and size.
Note that dimensions are in x,y,z for natural ordering but may change for consistency with z,y,x ordering used throughout MagellanMapper.
- Parameters:
blobs (
ndarray) – The blobs to retrieve, given as 2D array of[n, [z, row, column, radius, ...]].margin (
Sequence[int], default:(0, 0, 0)) – Additional space outside the ROI to include inx,y,z.reverse (
bool, default:True) – True to reverse the order ofoffsetandsize, assuming that they are inx,y,zrather thanz,y,xorder. Defaults to True for backward compatibility with the ROI convention used here.
- Returns:
``segs_all`: Blobs within the ROI
mask: the mask used to retrieve these blobs
- Return type:
Tuple of
- magmap.cv.detector.get_blobs_interior(blobs, shape, pad_start, pad_end)[source]#
Get blobs within the interior of a region based on padding.
- Parameters:
blobs – The blobs to retrieve, given as 2D array of
[n, [z, row, column, radius, ...]].shape – Shape of the region in z,y,x.
pad_start – Offset of interior region in z,y,x to include blobs.
pad_end – End offset in z,y,x.
- Returns:
Blobs within the given interior.
- magmap.cv.detector.meas_pruning_ratio(num_blobs_orig, num_blobs_after_pruning, num_blobs_next)[source]#
Measure blob pruning ratio.
- Parameters:
num_blobs_orig – Number of original blobs, before pruning.
num_blobs_after_pruning – Number of blobs after pruning.
num_blobs_next – Number of a blobs in an adjacent segment, presumably of similar size as that of the original blobs.
- Returns:
Pruning ratios as a tuple of the original number of blobs, blobs after pruning to original, and blobs after pruning to the next region.
- magmap.cv.detector.remove_close_blobs(blobs, blobs_master, tol, chunk_size=1000)[source]#
Removes blobs that are close to one another.
- Parameters:
blobs (
ndarray) – The blobs to be checked for closeness and pruning, given as 2D array of [n, [z, row, column, …]].blobs_master (
ndarray) – The list by which to check for close blobs, in the same format as blobs.tol (
float) – Tolerance to check for closeness, given in the same format as region. Blobs that are equal to or less than the absolute difference for all corresponding parameters will be pruned in the returned array.chunk_size (
int, default:1000) – Max size along first dimension for each blob array to minimize memory consumption; defaults to 1000.
- Returns:
pruned: blobs array after pruningblobs_master`: blobs with absolute coordinates updated with the average of any corresponding duplicates
- Return type:
Tuple of
- magmap.cv.detector.remove_close_blobs_within_array(blobs, region, tol)[source]#
Removes close blobs within a given array.
Uses remove_close_blobs() to detect blobs close to one another inside the master array.
- Parameters:
blobs – The blobs to add, given as 2D array of [n, [z, row, column, radius]].
region – Slice within each blob to check, such as slice(0, 2) to check for (z, row, column).
tol – Tolerance to check for closeness, given in the same format as region. Blobs that are equal to or less than the the absolute difference for all corresponding parameters will be pruned in the returned array.
- Returns:
The blobs array without blobs falling inside the tolerance range.
- magmap.cv.detector.remove_close_blobs_within_sorted_array(blobs, tol)[source]#
Removes close blobs within a given array, first sorting the array by z, y, x.
- Parameters:
blobs – The blobs to add, given as 2D array of [n, [z, row, column, radius]].
tol – Tolerance to check for closeness, given in the same format as region. Blobs that are equal to or less than the the absolute difference for all corresponding parameters will be pruned in the returned array.
- Returns:
Tuple of all blobs, a blobs array without blobs falling inside the tolerance range.
- magmap.cv.detector.remove_duplicate_blobs(blobs, region)[source]#
Removes duplicate blobs.
- Parameters:
blobs – The blobs, given as 2D array of [n, [z, row, column, radius]].
region – Slice within each blob to check, such as slice(0, 2) to check for (z, row, column).
- Returns:
The blobs array with only unique elements.