本文整理汇总了Python中skimage.morphology.remove_small_objects函数的典型用法代码示例。如果您正苦于以下问题:Python remove_small_objects函数的具体用法?Python remove_small_objects怎么用?Python remove_small_objects使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了remove_small_objects函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_rough_detection
def get_rough_detection(self, img, bigsize=40.0, smallsize=4.0, thresh = 0):
diff = self.difference_of_gaussian(-img, bigsize, smallsize)
diff[diff>thresh] = 1
se = morphology.square(4)
ero = morphology.erosion(diff, se)
labimage = label(ero)
#rec = morphology.reconstruction(ero, img, method='dilation').astype(np.dtype('uint8'))
# connectivity=1 corresponds to 4-connectivity.
morphology.remove_small_objects(labimage, min_size=600, connectivity=1, in_place=True)
#res = np.zeros(img.shape)
ero[labimage==0] = 0
ero = 1 - ero
labimage = label(ero)
morphology.remove_small_objects(labimage, min_size=400, connectivity=1, in_place=True)
ero[labimage==0] = 0
res = 1 - ero
res[res>0] = 255
#temp = 255 - temp
#temp = morphology.remove_small_objects(temp, min_size=400, connectivity=1, in_place=True)
#res = 255 - temp
return res
示例2: polylinesFromBinImage
def polylinesFromBinImage(img, minimum_cluster_size=6,
remove_small_obj_size=3,
reconnect_size=3,
max_n_contours=None, max_len_contour=None,
copy=True):
'''
return a list of arrays of un-branching contours
img -> (boolean) array
optional:
---------
minimum_cluster_size -> minimum number of pixels connected together to build a contour
##search_kernel_size -> TODO
##min_search_kernel_moment -> TODO
numeric:
-------------
max_n_contours -> maximum number of possible contours in img
max_len_contour -> maximum contour length
'''
assert minimum_cluster_size > 1
assert reconnect_size % 2, 'ksize needs to be odd'
# assert search_kernel_size == 0 or search_kernel_size > 2 and search_kernel_size%2, 'kernel size needs to be odd'
# assume array size parameters, is not given:
if max_n_contours is None:
max_n_contours = max(img.shape)
if max_len_contour is None:
max_len_contour = sum(img.shape[:2])
# array containing coord. of all contours:
contours = np.zeros(shape=(max_n_contours, max_len_contour, 2),
dtype=np.uint16) # if not search_kernel_size else np.float32)
if img.dtype != np.bool:
img = img.astype(bool)
elif copy:
img = img.copy()
if remove_small_obj_size:
remove_small_objects(img, remove_small_obj_size,
connectivity=2, in_place=True)
if reconnect_size:
# remove gaps
maximum_filter(img, reconnect_size, output=img)
# reduce contour width to 1
img = skeletonize(img)
n_contours = _populateContoursArray(img, contours, minimum_cluster_size)
contours = contours[:n_contours]
l = []
for c in contours:
ind = np.zeros(shape=len(c), dtype=bool)
_getValidInd(c, ind)
# remove all empty spaces:
l.append(c[ind])
return l
示例3: predict
def predict(self, times, frames, output_times):
nout = len(output_times)
nf = len(frames)
last_idx = np.argmax(times)
zt = frames[last_idx] < self.thresh
skmorph.remove_small_objects(zt, min_size=self.min_size, in_place=True)
d = morphology.distance_transform_edt(np.invert(zt))
return np.tile(d, (nout,1,1))
示例4: build_skeleton
def build_skeleton(frame):
"""
build a corner tree, skeletonize, dilate
"""
tree = trees.tree_corners(frame)
tree = morphology.skeletonize(tree)
# tree = morphology.binary_dilation(tree)
morphology.remove_small_objects(tree, min_size=20, connectivity=2, in_place=True)
tree = morphology.binary_dilation(tree)
return tree
示例5: distance_trend
def distance_trend(times, frames, threshold=25, min_size=12):
nf = len(frames)
zt = filters.gaussian_filter(frames,1.5) > threshold
d_outer = np.zeros((nf,) + frames[0].shape)
d_inner = np.zeros((nf,) + frames[0].shape)
for i in range(nf):
skmorph.remove_small_objects(zt, min_size=min_size, in_place=True)
d_outer[i] = morphology.distance_transform_edt(np.invert(zt[i]))
d_inner[i] = morphology.distance_transform_edt(zt[i])
return d_outer, d_inner
示例6: nuclei_regions
def nuclei_regions(comp_map):
"""
NUCLEI_REGIONS: extract "support regions" for nuclei. This function
expects as input a "tissue components map" (as returned, for example,
by segm.tissue_components) where values of 1 indicate pixels having
a color corresponding to nuclei.
It returns a set of compact support regions corresponding to the
nuclei.
:param comp_map: numpy.ndarray
A mask identifying different tissue components, as obtained
by classification in RGB space. The value 0
See segm.tissue.tissue_components()
:return:
"""
# Deprecated:...
# img_hem, _ = rgb2he(img0, normalize=True)
# img_hem = denoise_tv_bregman(img_hem, HE_OPTS['bregm'])
# Get a mask of nuclei regions by unsupervised clustering:
# Vector Quantization: background, mid-intensity Hem and high intensity Hem
# -train the quantizer for 3 levels
# vq = KMeans(n_clusters=3)
# vq.fit(img_hem.reshape((-1,1)))
# -the level of interest is the brightest:
# k = np.argsort(vq.cluster_centers_.squeeze())[2]
# mask_hem = (vq.labels_ == k).reshape(img_hem.shape)
# ...end deprecated
# Final mask:
mask = (comp_map == 1) # use the components classified by color
# mask = morph.closing(mask, selem=HE_OPTS['strel1'])
# mask = morph.opening(mask, selem=HE_OPTS['strel1'])
# morph.remove_small_objects(mask, in_place=True)
# mask = (mask > 0)
mask = mahotas.close_holes(mask)
morph.remove_small_objects(mask, in_place=True)
dst = mahotas.stretch(mahotas.distance(mask))
Bc=np.ones((9,9))
lmax = mahotas.regmax(dst, Bc=Bc)
spots, _ = mahotas.label(lmax, Bc=Bc)
regions = mahotas.cwatershed(lmax.max() - lmax, spots) * mask
return regions
# end NUCLEI_REGIONS
示例7: stk_to_rois
def stk_to_rois(stk, threshold, min_size, max_window=8, downscale_factor=2):
thresholded_stk = stk > threshold
thresholded_stk = remove_small_objects(thresholded_stk, min_size)
distance = ndi.distance_transform_edt(thresholded_stk)
cropped_stk = stk.copy()
cropped_stk[np.logical_not(thresholded_stk)] = 0
combined_stk = cropped_stk + distance/distance.max()
local_max = peak_local_max(combined_stk, indices=False,
footprint=np.ones((max_window, max_window)),
labels=thresholded_stk)
markers = ndi.label(local_max)[0]
labels = watershed(-combined_stk, markers, mask=thresholded_stk)
new_markers = markers.copy()
for i in set(labels.flatten()):
if i == 0: continue
if np.sum(labels==i) < min_size:
new_markers[markers==i] = 0
labels = watershed(-combined_stk, new_markers, mask=thresholded_stk)
labels_set = set(labels.flatten())
rois = []
for label in labels_set:
if label == 0: continue
if np.sum((labels==label).astype(int)) < min_size: continue
nroi = np.zeros((stk.shape[0], stk.shape[1]))
cx,cy = np.where(labels==label)
cx,cy = int(cx.mean()), int(cy.mean())
x,y = np.ogrid[0:nroi.shape[0], 0:nroi.shape[1]]
r = 4
mask = (cx-x)**2 + (cy-y)**2 <= r*r
nroi[mask] = 1
#nroi[labels==label] = 1
rois.append(zoom(nroi, downscale_factor, order=0))
rois = np.array(rois)
return rois, thresholded_stk, labels
示例8: segment_roi
def segment_roi(roi):
# step 1. phase congruency (edge detection)
Mm = phasecong_Mm(roi)
# step 2. hysteresis thresholding (of edges)
B = hysthresh(Mm,HT_T1,HT_T2)
# step 3. trim pixels off border
B[B[:,1]==0,0]=0
B[B[:,-2]==0,-1]=0
B[0,B[1,:]==0]=0
B[-1,B[-2,:]==0]=0
# step 4. threshold to find dark areas
dark = dark_threshold(roi, DARK_THRESHOLD_ADJUSTMENT)
# step 5. add dark areas back to blob
B = B | dark
# step 6. binary closing
B = binary_closing(B,SE3)
# step 7. binary dilation
B = binary_dilation(B,SE2)
# step 8. thinning
B = bwmorph_thin(B,3)
# step 9. fill holes
B = binary_fill_holes(B)
# step 10. remove blobs smaller than BLOB_MIN
B = remove_small_objects(B,BLOB_MIN,connectivity=2)
# done.
return B
示例9: get_bg_mask
def get_bg_mask(img):
#if img.ndim == 3:
# bg_mask = img.any(axis=-1)
# bg_mask = np.invert(bg_mask) # consistent with np.ma, True if masked
# # make multichannel (is it really this hard?)
# bg_mask = np.repeat(bg_mask[:,:,np.newaxis], 3, axis=2)
#
#else:
# bg_mask = (img != 0)
# bg_mask = np.invert(bg_mask) # see above
#bound = segmentation.find_boundaries(bg_mask, mode='inner', background=1)
#bg_mask[bound] = 1
#min_size = img.shape[0] * img.shape[1] // 4
#holes = morphology.remove_small_holes(bg_mask, min_size=min_size)
#bg_mask[holes] = 1
bg_mask = segmentation.find_boundaries(img)
bg_mask = morphology.remove_small_objects(bg_mask)
bg_mask = morphology.remove_small_holes(bg_mask)
bg_mask = np.invert(bg_mask)
return bg_mask
示例10: load_cell_image
def load_cell_image(self, sensitivity = 5., min_cell_size = 4000):
'''Load cell image and add cells to self'''
pic_nuclei = self.get_source_pic_nuclei()
self.shape = pic_nuclei.shape
nuclei = find_nuclei(pic_nuclei, sensitivity, min_cell_size)
self.cell_detect_params = (sensitivity, min_cell_size)
labels = measure_label(nuclei)
labelcount = np.bincount(labels.ravel())
bg = np.argmax(labelcount)
labels += 1
labels[labels == bg + 1] = 0
labels = remove_small_objects(labels, min_cell_size)
self.nuclei = labels
self.create_cells_from_nuclei(pic_nuclei)
self.rescale_nuclei()
示例11: pred_f
def pred_f(image, stepSize=stepSize, windowSize=windowSize, param=param,
marge=None, marge_cut_off=0, ClearSmallObjects=20, list_f=list_f):
caffe.set_mode_cpu()
cn_1 = "FCN_0.01_0.99_0.0005"
wd_1 = "/share/data40T_v2/Peter/pretrained_models"
net_1 = GetNet(cn_1, wd_1)
cn_2 = "DeconvNet_0.01_0.99_0.0005"
net_2 = GetNet(cn_2, wd_1)
prob_image, bin_image, thresh = pred_image_from_two_nets(image, net_1, net_2, stepSize, windowSize,
param=param, marge=marge, method="avg",
ClearBorder="Reconstruction")
segmentation_mask = DynamicWatershedAlias(prob_image, param)
segmentation_mask = remove_small_objects(segmentation_mask, ClearSmallObjects)
table = bin_analyser(image, segmentation_mask, list_f, marge_cut_off)
segmentation_mask[segmentation_mask > 0] = 1.
contours = dilation(segmentation_mask, disk(2)) - \
erosion(segmentation_mask, disk(2))
x, y = np.where(contours == 1)
image[x, y] = np.array([0, 0, 0])
segmentation_mask = img_as_ubyte(segmentation_mask)
segmentation_mask[segmentation_mask > 0] = 255
if marge_cut_off != 0:
c = marge_cut_off
image = image[c:-c, c:-c]
segmentation_mask = segmentation_mask[c:-c, c:-c]
prob_image = prob_image[c:-c, c:-c]
return image, table, segmentation_mask, prob_image
示例12: cleanImage
def cleanImage(img, min_size, scale_factor, img_otsu=None, saver=lambda n,x: x):
img, exposure_data = normalize_exposure2(img)
img = saver("01-exposure", img)
img_otsu = ski.filter.threshold_otsu(img) if not img_otsu else img_otsu
print('otsu:',img_otsu, ski.filter.threshold_otsu(img))
img = saver("02-zoom", scipy.ndimage.zoom(img, scale_factor, order=3))
print("shape after zoom:", img.shape)
# img_pil = PIL.Image.fromarray(img).resize((np.array(img.shape)*scale_factor).tolist()[:2], resample=PIL.Image.BICUBIC)
# img = saver("02-zoom", PIL2array(img_pil))
print("img:",img.shape,img.dtype)
img_cleaned = saver("03-bw", (img > img_otsu))
# img_cleaned = saver("03-bw", (img > 0.2))
dbg = DebugData()
img_cleaned = morphology.binary_erosion(img_cleaned,morphology.disk(int(2*scale_factor)))
img_cleaned = saver("04-erosion", img_cleaned, dbg=dbg)
img_cleaned = morphology.remove_small_objects(img_cleaned, min_size=int(min_size*scale_factor), connectivity=2)
img_cleaned = saver("05-remove", img_cleaned)
cleaned_sum = np.sum(img_cleaned)
print("img_cleaned size:",cleaned_sum)
# if cleaned_sum < 1000 or cleaned_sum > 300000:
# display(Image(str(dbg.saved_path)))
# raise Exception("Image not cleaned correctly"+str(locals()))
return img_cleaned, exposure_data
示例13: blobs
def blobs(image, remove_mb = None, val = 160, size = 100):
""" Convolve a kernel on the image and a gaussian filter to highligh blobs. Find blobs using the
Difference of Gaussian. Remove from the list of blobs the blobs that are at the membrane.
return 3 different list
"""
thresh = threshold_otsu(image)
#Find all the blobs in the image using Difference of Gaussian
blobs_in_image = feature.blob_dog(image, min_sigma=0.01,
max_sigma=3, threshold=thresh)
blob_list = []
for blob in blobs_in_image:
y, x, r = blob
blob_list.append((y, x))
if remove_mb == None:
blob_in_image_after_binary = set(blob_list)
else:
#Create a mask to remove blobs that are at the membrane and surrounded
#by bright big object
binary = image >= val*thresh/100
binary = dilation(binary, square(3))
binary = remove_small_objects(binary, min_size=size)
# Create a list of coordinate with the binary image
coor_binary = np.nonzero(binary)
list_blob_masked = zip(*coor_binary)
#Substract the list of coordinate from the binary image to the list of blobs
blob_in_image_after_binary = (set(blob_list) - set (list_blob_masked))
return blob_in_image_after_binary
示例14: label_nuclei
def label_nuclei(binary, min_size):
'''Label, watershed and remove small objects'''
distance = medial_axis(binary, return_distance=True)[1]
distance_blured = gaussian_filter(distance, 5)
local_maxi = peak_local_max(distance_blured, indices=False, labels=binary, min_distance = 30)
markers = measure_label(local_maxi)
# markers[~binary] = -1
# labels_rw = segmentation.random_walker(binary, markers)
# labels_rw[labels_rw == -1] = 0
# labels_rw = segmentation.relabel_sequential(labels_rw)
labels_ws = watershed(-distance, markers, mask=binary)
labels_large = remove_small_objects(labels_ws,min_size)
labels_clean_border = clear_border(labels_large)
labels_from_one = relabel_sequential(labels_clean_border)
# plt.imshow(ndimage.morphology.binary_dilation(markers))
# plt.show()
return labels_from_one[0]
示例15: func
def func(frame):
frame = frame.astype(bool)
binary = remove_small_objects(frame, smooth_size)
#binary = ndi.binary_fill_holes(binary)
#opened = binary_opening(frame, disk(smooth_size))
#opened = opened & frame
return binary