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
python类morphology()的实例源码
def __init__(self, data, morphology=None):
"""
This function will set up the underlying data structure. If no morphology is provided, the morphology array will
be generated as `numpy.max(data,axis=-1)`, i.e. a maximum projection over data along the time axis.
:param data:
:param morphology: This can either be a 2D numpy array with the same shape as the video, or None.
"""
self.postprocessor = self.no_postprocessor
# call the property setter which will initialize the mean data and threshold value
self.data = data
if morphology is None:
self.morphology = numpy.max(data, axis=-1)
else:
self.morphology = morphology
# todo: the active frame is merely a utility to synchronize widgets. maybe it should go to the gui...
self.active_frame = 0
def erosion(x, radius=3):
""" Return greyscale morphological erosion of an image,
see `skimage.morphology.erosion <http://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.erosion>`_.
Parameters
-----------
x : 2D array image.
radius : int for the radius of mask.
"""
from skimage.morphology import disk, dilation, erosion
mask = disk(radius)
x = erosion(x, selem=mask)
return x
## Object Detection
def morphology_changed(self):
"""This signal will be triggered when the morphology image changed."""
return Event()
def morphology(self):
"""
An image which describes the static morphology. A good choice is to use the maximum projection over the non
normalized data.
:getter: obtain the morphology image.
:setter: set the morphology image, will trigger the :py:attr:`samuroi.SamuROIData.morphology_changed` event.
:type: 2D numpy array with same image shape as data.
"""
return self.__morphology
def morphology(self, morphology):
if (morphology.shape != self.data.shape[0:2]):
raise Exception("Invalid morphology shape.")
self.__morphology = morphology
# choose some appropriate new threshold value
self.threshold = numpy.percentile(self.morphology.flatten(), q=90)
self.morphology_changed()
def threshold(self, t):
self.__threshold = t
self.threshold_changed()
elevation_map = skimage.filters.sobel(self.morphology)
markers = numpy.zeros_like(self.morphology)
markers[self.morphology < self.threshold] = 1
markers[self.morphology > self.threshold * 1.1] = 2
segmentation = skimage.morphology.watershed(elevation_map, markers)
self.overlay = segmentation == 2
def binary_dilation(x, radius=3):
""" Return fast binary morphological dilation of an image.
see `skimage.morphology.binary_dilation <http://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.binary_dilation>`_.
Parameters
-----------
x : 2D array image.
radius : int for the radius of mask.
"""
from skimage.morphology import disk, binary_dilation
mask = disk(radius)
x = binary_dilation(x, selem=mask)
return x
def dilation(x, radius=3):
""" Return greyscale morphological dilation of an image,
see `skimage.morphology.dilation <http://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.dilation>`_.
Parameters
-----------
x : 2D array image.
radius : int for the radius of mask.
"""
from skimage.morphology import disk, dilation
mask = disk(radius)
x = dilation(x, selem=mask)
return x
def binary_erosion(x, radius=3):
""" Return binary morphological erosion of an image,
see `skimage.morphology.binary_erosion <http://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.binary_erosion>`_.
Parameters
-----------
x : 2D array image.
radius : int for the radius of mask.
"""
from skimage.morphology import disk, dilation, binary_erosion
mask = disk(radius)
x = binary_erosion(x, selem=mask)
return x
def get_segmented_lungs(image):
#Creation of the markers as shown above:
marker_internal, marker_external, marker_watershed = generate_markers(image)
#Creation of the Sobel-Gradient
sobel_filtered_dx = ndimage.sobel(image, 1)
sobel_filtered_dy = ndimage.sobel(image, 0)
sobel_gradient = np.hypot(sobel_filtered_dx, sobel_filtered_dy)
sobel_gradient *= 255.0 / np.max(sobel_gradient)
#Watershed algorithm
watershed = morphology.watershed(sobel_gradient, marker_watershed)
#Reducing the image created by the Watershed algorithm to its outline
outline = ndimage.morphological_gradient(watershed, size=(3,3))
outline = outline.astype(bool)
#Performing Black-Tophat Morphology for reinclusion
#Creation of the disk-kernel and increasing its size a bit
blackhat_struct = [[0, 0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0, 0]]
#blackhat_struct = ndimage.iterate_structure(blackhat_struct, 8)
blackhat_struct = ndimage.iterate_structure(blackhat_struct, 14) # <- retains more of the area, 12 works well. Changed to 14, 12 still excluded some parts.
#Perform the Black-Hat
outline += ndimage.black_tophat(outline, structure=blackhat_struct)
#Use the internal marker and the Outline that was just created to generate the lungfilter
lungfilter = np.bitwise_or(marker_internal, outline)
#Close holes in the lungfilter
#fill_holes is not used here, since in some slices the heart would be reincluded by accident
lungfilter = ndimage.morphology.binary_closing(lungfilter, structure=np.ones((5,5)), iterations=3)
#Apply the lungfilter (note the filtered areas being assigned threshold_min HU)
segmented = np.where(lungfilter == 1, image, threshold_min*np.ones(image.shape))
#return segmented, lungfilter, outline, watershed, sobel_gradient, marker_internal, marker_external, marker_watershed
return segmented
def get_segmented_lungs(image):
#Creation of the markers as shown above:
marker_internal, marker_external, marker_watershed = generate_markers(image)
#Creation of the Sobel-Gradient
sobel_filtered_dx = ndimage.sobel(image, 1)
sobel_filtered_dy = ndimage.sobel(image, 0)
sobel_gradient = np.hypot(sobel_filtered_dx, sobel_filtered_dy)
sobel_gradient *= 255.0 / np.max(sobel_gradient)
#Watershed algorithm
watershed = morphology.watershed(sobel_gradient, marker_watershed)
#Reducing the image created by the Watershed algorithm to its outline
outline = ndimage.morphological_gradient(watershed, size=(3,3))
outline = outline.astype(bool)
#Performing Black-Tophat Morphology for reinclusion
#Creation of the disk-kernel and increasing its size a bit
blackhat_struct = [[0, 0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0, 0]]
#blackhat_struct = ndimage.iterate_structure(blackhat_struct, 8)
blackhat_struct = ndimage.iterate_structure(blackhat_struct, 14) # <- retains more of the area, 12 works well. Changed to 14, 12 still excluded some parts.
#Perform the Black-Hat
outline += ndimage.black_tophat(outline, structure=blackhat_struct)
#Use the internal marker and the Outline that was just created to generate the lungfilter
lungfilter = np.bitwise_or(marker_internal, outline)
#Close holes in the lungfilter
#fill_holes is not used here, since in some slices the heart would be reincluded by accident
lungfilter = ndimage.morphology.binary_closing(lungfilter, structure=np.ones((5,5)), iterations=3)
#Apply the lungfilter (note the filtered areas being assigned threshold_min HU)
segmented = np.where(lungfilter == 1, image, threshold_min*np.ones(image.shape))
#return segmented, lungfilter, outline, watershed, sobel_gradient, marker_internal, marker_external, marker_watershed
return segmented
def get_segmented_lungs(image):
#Creation of the markers as shown above:
marker_internal, marker_external, marker_watershed = generate_markers(image)
#Creation of the Sobel-Gradient
sobel_filtered_dx = ndimage.sobel(image, 1)
sobel_filtered_dy = ndimage.sobel(image, 0)
sobel_gradient = np.hypot(sobel_filtered_dx, sobel_filtered_dy)
sobel_gradient *= 255.0 / np.max(sobel_gradient)
#Watershed algorithm
watershed = morphology.watershed(sobel_gradient, marker_watershed)
#Reducing the image created by the Watershed algorithm to its outline
outline = ndimage.morphological_gradient(watershed, size=(3,3))
outline = outline.astype(bool)
#Performing Black-Tophat Morphology for reinclusion
#Creation of the disk-kernel and increasing its size a bit
blackhat_struct = [[0, 0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0, 0]]
#blackhat_struct = ndimage.iterate_structure(blackhat_struct, 8)
blackhat_struct = ndimage.iterate_structure(blackhat_struct, 14) # <- retains more of the area, 12 works well. Changed to 14, 12 still excluded some parts.
#Perform the Black-Hat
outline += ndimage.black_tophat(outline, structure=blackhat_struct)
#Use the internal marker and the Outline that was just created to generate the lungfilter
lungfilter = np.bitwise_or(marker_internal, outline)
#Close holes in the lungfilter
#fill_holes is not used here, since in some slices the heart would be reincluded by accident
lungfilter = ndimage.morphology.binary_closing(lungfilter, structure=np.ones((5,5)), iterations=3)
#Apply the lungfilter (note the filtered areas being assigned threshold_min HU)
segmented = np.where(lungfilter == 1, image, threshold_min*np.ones(image.shape))
#return segmented, lungfilter, outline, watershed, sobel_gradient, marker_internal, marker_external, marker_watershed
return segmented