def remove_conn_components(pred_mask, num_cc):
labels = label(pred_mask)
if num_cc == 1:
maxArea = 0
for region in regionprops(labels):
if region.area > maxArea:
maxArea = region.area
print(maxArea)
mask = remove_small_objects(labels, maxArea - 1)
else:
mask = remove_small_objects(labels, 3000, connectivity=2)
return mask
python类remove_small_objects()的实例源码
def removeSmallObject(filledImg, minSize = 20000):
biImgRsize = filledImg.shape[0] * 0.1
biImgCsize = filledImg.shape[1] * 0.1
biImg = filledImg[biImgRsize:-biImgRsize, biImgCsize:-biImgCsize]
cleanedImg = morphology.remove_small_objects(biImg, minSize)
return cleanedImg
def localization(x, y):
"""Simple post-processing and get IVDs positons.
Return:
positons: calculated by `ndimage.measurements.center_of_mass`
y: after fill holes and remove small objects.
"""
labels, nums = label(y, return_num=True)
areas = np.array([prop.filled_area for prop in regionprops(labels)])
assert nums >= 7, 'Fail in this test, should detect at least seven regions.'
# Segment a joint region which should be separate (if any).
while np.max(areas) > 10000:
y = ndimage.binary_opening(y, structure=np.ones((3, 3, 3)))
areas = np.array([prop.filled_area for prop in regionprops(label(y))])
# Remove small objects.
threshold = sorted(areas, reverse=True)[7]
y = morphology.remove_small_objects(y, threshold + 1)
# Fill holes.
y = ndimage.binary_closing(y, structure=np.ones((3, 3, 3)))
y = morphology.remove_small_holes(y, min_size=512, connectivity=3)
positions = ndimage.measurements.center_of_mass(x, label(y), range(1, 8))
return np.array(positions), y
preprocessing.py 文件源码
项目:bird-species-classification
作者: johnmartinsson
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def compute_binary_mask_lasseck(spectrogram, threshold):
# normalize to [0, 1)
norm_spectrogram = normalize(spectrogram)
# median clipping
binary_image = median_clipping(norm_spectrogram, threshold)
# closing binary image (dilation followed by erosion)
binary_image = morphology.binary_closing(binary_image, selem=np.ones((4, 4)))
# dialate binary image
binary_image = morphology.binary_dilation(binary_image, selem=np.ones((4, 4)))
# apply median filter
binary_image = filters.median(binary_image, selem=np.ones((2, 2)))
# remove small objects
binary_image = morphology.remove_small_objects(binary_image, min_size=32, connectivity=1)
mask = np.array([np.max(col) for col in binary_image.T])
mask = smooth_mask(mask)
return mask
# TODO: This method needs some real testing
def find_peaks_regionprops(z, min_sigma=4, max_sigma=5, threshold=1,
min_size=50, return_props=False):
"""
Finds peaks using regionprops.
Uses the difference of two gaussian convolutions to separate signal from
background, and then uses the skimage.measure.regionprops function to find
connected islands (peaks). Small blobs can be rejected using `min_size`.
Parameters
----------
z : ndarray
Array of image intensities.
min_sigma : int, float
Standard deviation for the minimum gaussian convolution
max_sigma : int, float
Standard deviation for the maximum gaussian convolution
threshold : int, float
Minimum difference in intensity
min_size : int
Minimum size in pixels of blob
return_props : bool
Return skimage.measure.regionprops
Returns
-------
ndarray
(n_peaks, 2)
Array of peak coordinates.
"""
from skimage import morphology, measure
difference = ndi.gaussian_filter(z, min_sigma) - ndi.gaussian_filter(z, max_sigma)
labels, numlabels = ndi.label(difference > threshold)
labels = morphology.remove_small_objects(labels, min_size)
props = measure.regionprops(labels, z)
if return_props:
return props
else:
peaks = np.array([prop.centroid for prop in props])
return clean_peaks(peaks)