Skip to content
Snippets Groups Projects

3d blob detection - Difference of Gaussian (DoG) method

1 file
+ 60
0
Compare changes
  • Side-by-side
  • Inline
from skimage.feature import blob_dog
import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
class blob_detection():
def __init__(self, volume, background=0, min_sigma=1, max_sigma=50, sigma_ratio=1.6, threshold=0.5, overlap=0.5, threshold_rel=None, exclude_border=False):
"""
Initialize the blob detection object
Args:
volume: The volume to detect blobs in
background: 0 if background is darker than the blobs, 1 if background is lighter than the blobs
min_sigma: The minimum standard deviation for Gaussian kernel
max_sigma: The maximum standard deviation for Gaussian kernel
num_sigma: The number of standard deviations between min and max
threshold: The absolute lower bound for scale space maxima
overlap: The fraction of area of two blobs that overlap
"""
self.volume = volume
if background != 0:
self.volume = np.invert(self.volume)
self.volume = img_as_float(self.volume)
self.min_sigma = min_sigma
self.max_sigma = max_sigma
self.sigma_ratio = sigma_ratio
self.threshold = threshold
self.overlap = overlap
self.threshold_rel = threshold_rel
self.exclude_border = exclude_border
def detect(self):
"""
Detect blobs in the volume
Args:
None
Returns:
blobs: The blobs found in the volume
"""
blobs = blob_dog(self.volume, min_sigma=self.min_sigma, max_sigma=self.max_sigma, sigma_ratio=self.sigma_ratio, threshold=self.threshold, overlap=self.overlap, threshold_rel=self.threshold_rel, exclude_border=self.exclude_border)
blobs[:, 3] = blobs[:, 3] * np.sqrt(3) # Change sigma to radius
return blobs
def plot(self, blobs, z_slice=64):
"""
Plot the blobs found on a slice of the volume
Args:
blobs: The blobs found in the volume (p, r, c, radius)
z_slice: The slice to plot
"""
plt.imshow(self.volume[z_slice], cmap='gray')
for blob in blobs:
z, y, x, s = blob
if abs(z - z_slice) < s: # The blob is in the slice
c = plt.Circle((x, y), s, color='red', linewidth=2, fill=False)
plt.gca().add_patch(c)
plt.show()
return
\ No newline at end of file
Loading