Untitled
unknown
python
2 years ago
5.8 kB
10
Indexable
from modules.utils.create_folder_not_exists import create_folder_not_exists
def get_segmented_lungs(imagePath, num, save=False, plot=False, show_on_window=False, crop_percentage=0.05):
#This funtion segments the lungs from the given 2D slice.
im = cv.imread(imagePath, cv.IMREAD_GRAYSCALE)
im = cv.resize(im, (528, 528))
crop = im.copy()
path_save=os.path.dirname(imagePath)
if show_on_window:
height,width=im.shape[:2]
start_row,start_col=int(height*crop_percentage),int(width*crop_percentage)
end_row,end_col=int(height*(1-crop_percentage)),int(width*(1-crop_percentage))
crop=crop[start_row:end_row,start_col:end_col]
else:
if num == 161 or (num >= 173 and num <= 174) or (num == 758):
height,width=im.shape[:2]
start_row,start_col=int(height*0.20),int(width*0.20)
end_row,end_col=int(height*0.80),int(width*0.80)
crop=crop[start_row:end_row,start_col:end_col]
elif num >= 756 and num <= 767:
#Step 1: Crop the image
height,width=im.shape[:2]
start_row,start_col=int(height*0),int(width*0)
end_row,end_col=int(height*1),int(width*1)
crop=crop[start_row:end_row,start_col:end_col]
elif num == 1320 or num == 1219 or (num >= 712 and num <= 767) or (num >= 779 and num <= 799) or (num >= 688 and num <= 699) or (num >= 648 and num <= 664) or (num >= 225 and num <= 234):
#Step 1: Crop the image
height,width=im.shape[:2]
start_row,start_col=int(height*0.03),int(width*0.03)
end_row,end_col=int(height*0.97),int(width*0.97)
crop=crop[start_row:end_row,start_col:end_col]
else:
#Step 1: Crop the image
height,width=im.shape[:2]
start_row,start_col=int(height*0.12),int(width*0.12)
end_row,end_col=int(height*0.88),int(width*0.88)
crop=crop[start_row:end_row,start_col:end_col]
#Step 2: Convert into a binary image.
ret,binary = cv.threshold(crop,140,255,cv.THRESH_BINARY_INV)
#Step 3: Remove the blobs connected to the border of the image.
cleared = clear_border(binary)
#Step 4: Closure operation with a disk of radius 10. This operation is
#to keep nodules attached to the lung wall.
selem = disk(2)
closing = binary_closing(cleared, selem)
#Step 5: Label the image.
label_image = label(closing)
#Step 6: 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
segmented_area = label_image > 0
#Step 7: Erosion operation with a disk of radius 2. This operation is
#seperate the lung nodules attached to the blood vessels.
selem = disk(2)
erosion = binary_erosion(segmented_area, selem)
# Step 4: Closure operation with a disk of radius 10. This operation is
# to keep nodules attached to the lung wall.
selem = disk(10)
closing2 = binary_closing(erosion, selem)
#Step 8: Fill in the small holes inside the binary mask of lungs.
edges = roberts(closing2)
fill_holes = ndi.binary_fill_holes(edges)
superimpose = crop.copy()
#Step 9: Superimpose叠加 the binary mask on the input image.
get_high_vals = fill_holes == 0
superimpose[get_high_vals] = 0
superimpose = cv.resize(superimpose, (528, 528))
if show_on_window:
directory1 = path_save+'/'
directory2 = '.jpg'
images = [im, crop, binary, cleared, closing, segmented_area, erosion, closing2, fill_holes, superimpose]
titles = ['0_original_image', '1_cropped_image', '2_binary_image', '3_remove_blobs', '4_closure', '5_roi', '6_erosion', '7_closure', '8_fill_hole', '9_result']
for i, title in enumerate(titles):
filename = directory1 + title + directory2
create_folder_not_exists(filename)
try:
cv.imwrite(filename, images[i])
except:
indices = images[i].astype(np.uint8) #convert to an unsigned byte
indices*=255
cv.imwrite(filename, indices)
else:
#flip vertically
directory1 = 'preprocessing/pre1/'
directory2 = '.jpg'
images = [crop, binary, cleared, label_image, superimpose]
titles = ['cropped_image', 'binary_image', 'remove_blobs', 'label', 'result']
if save:
for y in range(5):
filename = directory1 + str(y+1) + titles[y] + '/' + titles[y] + str(num+1) + directory2
create_folder_not_exists(filename)
cv.imwrite(filename, images[y])
images = [im, crop, binary, cleared, closing, label_image, segmented_area, erosion, closing2, fill_holes, superimpose]
if plot:
titles = ['Original Image',
'Step 1: Cropped Image',
'Step 2: Binary image',
'Step 3: Remove blobs',
'Step 4: Closure',
'Step 5: Label',
'Step 6: Region On Interest',
'Step 7: Erosion',
'Step 8: Closure',
'Step 9: Fill Holes',
'Step 10: Result']
plot_img(images, titles, camp=plt.cm.bone, rows = 3, cols = 4, fontsize= 50)
return superimpose
Editor is loading...
Leave a Comment