def remove_pectoral(self, img, breast_mask, high_int_threshold=.8,
morph_kn_size=3, n_morph_op=7, sm_kn_size=25):
'''Remove the pectoral muscle region from an input image
Args:
img (2D array): input image as a numpy 2D array.
breast_mask (2D array):
high_int_threshold ([int]): a global threshold for high intensity
regions such as the pectoral muscle. Default is 200.
morph_kn_size ([int]): kernel size for morphological operations
such as erosions and dilations. Default is 3.
n_morph_op ([int]): number of morphological operations. Default is 7.
sm_kn_size ([int]): kernel size for final smoothing (i.e. opening).
Default is 25.
Returns:
an output image with pectoral muscle region removed as a numpy
2D array.
Notes: this has not been tested on .dcm files yet. It may not work!!!
'''
# Enhance contrast and then thresholding.
img_equ = cv2.equalizeHist(img)
if high_int_threshold < 1.:
high_th = int(img.max()*high_int_threshold)
else:
high_th = int(high_int_threshold)
maxval = self.max_pix_val(img.dtype)
_, img_bin = cv2.threshold(img_equ, high_th,
maxval=maxval, type=cv2.THRESH_BINARY)
pect_marker_img = np.zeros(img_bin.shape, dtype=np.int32)
# Sure foreground (shall be pectoral).
pect_mask_init = self.select_largest_obj(img_bin, lab_val=maxval,
fill_holes=True,
smooth_boundary=False)
kernel_ = np.ones((morph_kn_size, morph_kn_size), dtype=np.uint8)
pect_mask_eroded = cv2.erode(pect_mask_init, kernel_,
iterations=n_morph_op)
pect_marker_img[pect_mask_eroded > 0] = 255
# Sure background - breast.
pect_mask_dilated = cv2.dilate(pect_mask_init, kernel_,
iterations=n_morph_op)
pect_marker_img[pect_mask_dilated == 0] = 128
# Sure background - pure background.
pect_marker_img[breast_mask == 0] = 64
# Watershed segmentation.
img_equ_3c = cv2.cvtColor(img_equ, cv2.COLOR_GRAY2BGR)
cv2.watershed(img_equ_3c, pect_marker_img)
img_equ_3c[pect_marker_img == -1] = (0, 0, 255)
# Extract only the breast and smooth.
breast_only_mask = pect_marker_img.copy()
breast_only_mask[breast_only_mask == -1] = 0
breast_only_mask = breast_only_mask.astype(np.uint8)
breast_only_mask[breast_only_mask != 128] = 0
breast_only_mask[breast_only_mask == 128] = 255
kernel_ = np.ones((sm_kn_size, sm_kn_size), dtype=np.uint8)
breast_only_mask = cv2.morphologyEx(breast_only_mask, cv2.MORPH_OPEN,
kernel_)
img_breast_only = cv2.bitwise_and(img_equ, breast_only_mask)
return (img_breast_only, img_equ_3c)
评论列表
文章目录