def get_segmented_lungs(im, plot=False):
# Step 1: Convert into a binary image.
binary = im < -400
# Step 2: Remove the blobs connected to the border of the image.
cleared = clear_border(binary)
# Step 3: Label the image.
label_image = label(cleared)
# Step 4: Keep the labels with 2 largest areas.
areas = [r.area for r in regionprops(label_image)]
areas.sort()
if len(areas) > 2:
for region in regionprops(label_image):
if region.area < areas[-2]:
for coordinates in region.coords:
label_image[coordinates[0], coordinates[1]] = 0
binary = label_image > 0
# Step 5: Erosion operation with a disk of radius 2. This operation is seperate the lung nodules attached to the blood vessels.
selem = disk(2)
binary = binary_erosion(binary, selem)
# Step 6: Closure operation with a disk of radius 10. This operation is to keep nodules attached to the lung wall.
selem = disk(10) # CHANGE BACK TO 10
binary = binary_closing(binary, selem)
# Step 7: Fill in the small holes inside the binary mask of lungs.
edges = roberts(binary)
binary = ndi.binary_fill_holes(edges)
# Step 8: Superimpose the binary mask on the input image.
get_high_vals = binary == 0
im[get_high_vals] = -2000
return im, binary
python类binary_closing()的实例源码
def image_filter(img):
img2 = img.copy();
img2[img2 < 30] = 100;
img2 = exp.smooth_image(img2, sigma = 1.0);
#plt.figure(6); plt.clf();
#plt.imshow(img2);
# threshold image and take zero smaller components..
th = img2 < 92;
th2 = morph.binary_closing(th, morph.diamond(1))
label = meas.label(th2, background=0)
#plt.imshow(mask)
bs = meas.regionprops(label+1);
area = np.array([prop.area for prop in bs]);
if len(area) > 0:
mask = np.logical_and(label > -1, label != np.argsort(area)[-1]);
img2[mask] = 100;
img2[:2,:] = 100; img2[-2:,:] = 100;
img2[:,:2] = 100; img2[:,-2:] = 100;
#plt.figure(6); plt.clf();
#plt.subplot(1,2,1);
#plt.imshow(img2, vmin = 84, vmax = 92, cmap = plt.cm.gray)
#plt.subplot(1,2,2);
#plt.imshow(img2);
return img2;
def image_filter(img):
img2 = img.copy();
img2[img2 < 30] = 100;
img2 = exp.smooth_image(img2, sigma = 1.0);
#plt.figure(6); plt.clf();
#plt.imshow(img2);
# threshold image and take zero smaller components..
th = img2 < 92;
th2 = morph.binary_closing(th, morph.diamond(1))
label = meas.label(th2, background=0)
#plt.imshow(mask)
bs = meas.regionprops(label+1);
area = np.array([prop.area for prop in bs]);
if len(area) > 0:
mask = np.logical_and(label > -1, label != np.argsort(area)[-1]);
img2[mask] = 100;
img2[:2,:] = 100; img2[-2:,:] = 100;
img2[:,:2] = 100; img2[:,-2:] = 100;
#plt.figure(6); plt.clf();
#plt.subplot(1,2,1);
#plt.imshow(img2, vmin = 84, vmax = 92, cmap = plt.cm.gray)
#plt.subplot(1,2,2);
#plt.imshow(img2);
return img2;
def get_segmented_lungs(im, plot=False):
# Step 1: Convert into a binary image.
binary = im < -400
# Step 2: Remove the blobs connected to the border of the image.
cleared = clear_border(binary)
# Step 3: Label the image.
label_image = label(cleared)
# Step 4: Keep the labels with 2 largest areas.
areas = [r.area for r in regionprops(label_image)]
areas.sort()
if len(areas) > 2:
for region in regionprops(label_image):
if region.area < areas[-2]:
for coordinates in region.coords:
label_image[coordinates[0], coordinates[1]] = 0
binary = label_image > 0
# Step 5: Erosion operation with a disk of radius 2. This operation is seperate the lung nodules attached to the blood vessels.
selem = disk(2)
binary = binary_erosion(binary, selem)
# Step 6: Closure operation with a disk of radius 10. This operation is to keep nodules attached to the lung wall.
selem = disk(10) # CHANGE BACK TO 10
binary = binary_closing(binary, selem)
# Step 7: Fill in the small holes inside the binary mask of lungs.
edges = roberts(binary)
binary = ndi.binary_fill_holes(edges)
# Step 8: Superimpose the binary mask on the input image.
get_high_vals = binary == 0
im[get_high_vals] = -2000
return im, binary
preprocessing.py 文件源码
项目:bird-species-classification
作者: johnmartinsson
项目源码
文件源码
阅读 34
收藏 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