Esempio n. 1
0
def label_objects(I,
                  min_object_size,
                  threshold=220,
                  connectivity=8,
                  kernel=8,
                  apply_watershed=False):
    # try:
    BW = (cv2.cvtColor(I, cv2.COLOR_BGR2GRAY) < threshold).astype(bool)
    #     if apply_watershed:
    #         BW = morph.binary_opening(BW, np.ones((connectivity,connectivity)).astype(int))
    labels = scilabel(BW)[0]
    BW = fill_holes(
        morph.remove_small_objects(labels,
                                   min_size=min_object_size,
                                   connectivity=connectivity,
                                   in_place=True))
    if apply_watershed:
        distance = distance_transform_edt(BW)
        local_maxi = peak_local_max(distance,
                                    indices=False,
                                    footprint=np.ones((kernel, kernel)),
                                    labels=BW)
        markers = scilabel(local_maxi)[0]
        labels = watershed(-distance, markers, mask=BW)
    else:
        labels = scilabel(BW)[0]
    return (BW != 0), labels
 def __make_mask_3d(self, image):
     assert image.ndim == 3
     image_shape = image.shape
     image = image.reshape(-1)
     mask = np.zeros_like(image, dtype=np.bool)
     thr = self.threshold
     if self.type_str == "threshold_plus":
         mask[image > thr] = True
     elif self.type_str == "threshold_minus":
         mask[image < thr] = True
     elif self.type_str == "otsu_plus":
         thr = otsu_threshold(image) if np.any(image) else thr
         mask[image > thr] = True
     elif self.type_str == "otsu_minus":
         thr = otsu_threshold(image) if np.any(image) else thr
         mask[image < thr] = True
     elif self.type_str == "mean_plus":
         thr = np.mean(image)
         mask[image > thr] = True
     mask = mask.reshape(image_shape)
     mask = ndimg.binary_dilation(mask, iterations=2)
     mask = fill_holes(mask)
     # foreground should not be empty
     assert np.any(mask == True), (
         "no foreground based on the specified combination parameters, "
         "please change choose another `mask_type` or double-check all "
         "input images")
     return mask
Esempio n. 3
0
 def __make_mask_3d(self, image):
     assert image.ndim == 3
     image_shape = image.shape
     image = image.reshape(-1)
     mask = np.zeros_like(image, dtype=np.bool)
     thr = self.threshold
     if self.type_str == 'threshold_plus':
         mask[image > thr] = True
     elif self.type_str == 'threshold_minus':
         mask[image < thr] = True
     elif self.type_str == 'otsu_plus':
         thr = otsu_threshold(image) if np.any(image) else thr
         mask[image > thr] = True
     elif self.type_str == 'otsu_minus':
         thr = otsu_threshold(image) if np.any(image) else thr
         mask[image < thr] = True
     elif self.type_str == 'mean_plus':
         thr = np.mean(image)
         mask[image > thr] = True
     mask = mask.reshape(image_shape)
     mask = ndimg.binary_dilation(mask, iterations=2)
     mask = fill_holes(mask)
     # foreground should not be empty
     assert np.any(mask == True), \
         "no foreground based on the specified combination parameters, " \
         "please change choose another `mask_type` or double-check all " \
         "input images"
     return mask
Esempio n. 4
0
def label_objects(img,
                  otsu=True,
                  min_object_size=100000,
                  threshold=240,
                  connectivity=8,
                  kernel=61,
                  keep_holes=False,
                  max_hole_size=0,
                  gray_before_close=False,
                  blur_size=0):
    I=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
    gray_mask=filter_grays(img, output_type="bool")
    if otsu: threshold = threshold_otsu(I)
    BW = (I<threshold).astype(bool)
    if gray_before_close: BW=BW&gray_mask
    if kernel>0: BW = morph.binary_closing(BW, morph.disk(kernel))#square
    if not gray_before_close: BW=BW&gray_mask
    if blur_size: BW=(cv2.blur(BW.astype(np.uint8), (blur_size,blur_size))==1)
    labels = scilabel(BW)[0]
    labels=morph.remove_small_objects(labels, min_size=min_object_size, connectivity = connectivity, in_place=True)
    if not keep_holes and max_hole_size:
        BW=morph.remove_small_objects(labels==0, min_size=max_hole_size, connectivity = connectivity, in_place=True)==False#remove_small_holes(labels,area_threshold=max_hole_size, connectivity = connectivity, in_place=True)>0
    elif keep_holes:
        BW=labels>0
    else:
        BW=fill_holes(labels)
    labels = scilabel(BW)[0]
    return(BW!=0),labels
Esempio n. 5
0
def DetectAneurysms(b_3, thres, silence=True):
    b_3_int = np.asarray(b_3, dtype=np.uint8)
    b_3_int = cv2.bitwise_not(b_3_int)
    ret, th3 = cv2.threshold(b_3_int, thres, 255, cv2.THRESH_BINARY_INV)
    
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7,7))
    closing = cv2.morphologyEx(th3, cv2.MORPH_CLOSE, kernel)
    
    closing = cv2.bitwise_not(closing)
    closing = fill_holes(closing).astype(np.uint8)
    closing = closing*255
    edges = cv2.Canny(closing, 50, 100, apertureSize=3)
    
    
    des = np.copy(edges)
    contour,hier = cv2.findContours(des,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
    
    for cnt in contour:
        area = cv2.contourArea(cnt)
        if area > 30 and area < 1500:
            rect = cv2.minAreaRect(cnt)
            ratio = rect[1][0] / rect[1][1]
            (x,y),radius = cv2.minEnclosingCircle(cnt)
            circ_area = 3.145 * radius**2
            if ratio > 0.33 and ratio < 3. and circ_area/area < 3. :
                cv2.drawContours(des,[cnt],0,255,-1)
            
    des = cv2.medianBlur(des,5)
    
    if silence==False:
        idx = np.where(des==255)
        detections = np.copy(b_3)
        detections[idx] = 255.
    
        plt.subplot(2,2,1)
        plt.imshow(b_3, cmap='gray')

        plt.subplot(2,2,2)
        plt.imshow(closing, cmap='gray')
    
        plt.subplot(2,2,3)
        plt.imshow(edges, cmap='gray')
        
        plt.subplot(2,2,4)
        plt.imshow(detections, cmap='gray')

        plt.show()
    
    return des/255
Esempio n. 6
0
def label_objects(I,
                  min_object_size,
                  threshold=220,
                  connected_components=False,
                  connectivity=8,
                  kernel=8,
                  apply_watershed=False):

    #try:
    BW = (cv2.cvtColor(I, cv2.COLOR_RGB2GRAY) < threshold).astype(bool)
    #     if apply_watershed:
    #         BW = morph.binary_opening(BW, np.ones((connectivity,connectivity)).astype(int))
    labels = scilabel(BW)[0]
    if connected_components:
        BW = fill_holes(
            morph.remove_small_objects(labels,
                                       min_size=min_object_size,
                                       connectivity=connectivity,
                                       in_place=True))
        labels = scilabel(BW)[0]
    return (BW != 0), labels