Exemple #1
0
def dist_watershed(data):

    data = contrast(data)

    _, bin = cv2.threshold(data,0,255,cv2.THRESH_OTSU)

    #remove white dots. ok on ANON_LUNG_TC148 #54
    #kernel = np.ones((7,7),np.uint8)
    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5))
    #nowhitedots = cv2.erode(bin, kernel, iterations = 3)
    nowhitedots = cv2.morphologyEx(bin,cv2.MORPH_OPEN, kernel, iterations = 1)

    dist = ndimage.distance_transform_edt(nowhitedots)
    localMax = peak_local_max(dist, indices=False, min_distance=10,
                              labels=nowhitedots)

    markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
    labels = watershed(-dist, markers, mask=nowhitedots)

    res = []
    for label in np.unique(labels):
        # skip background
        if label == 0:
            continue

        mask = np.zeros(data.shape, dtype="uint8")
        mask[labels == label] = 255

        _, cnts, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        res += cnts

    return res
def contouring_binsym_all(data):

    data = contrast(data)
    #display(data)

    _, bin = cv2.threshold(data, 0, 255, cv2.THRESH_OTSU)

    # keep only what doesn't appear on the other side
    sym = bin[:, ::-1]
    diff = bin - sym
    _, diff = cv2.threshold(
        diff, 128, 255,
        cv2.THRESH_BINARY)  # clean the '1' which mess up with findcontours

    #remove white dots. ok on ANON_LUNG_TC148 #54
    kernel = np.ones((3, 3), np.uint8)
    nowhitedots = cv2.morphologyEx(diff, cv2.MORPH_OPEN, kernel, iterations=2)

    #fill black holes
    kernel = np.ones((3, 3), np.uint8)
    noblackholes = cv2.morphologyEx(nowhitedots,
                                    cv2.MORPH_CLOSE,
                                    kernel,
                                    iterations=2)

    # NB: COMP allows to get external boundaries, we don't care about holes
    _, contours, _ = cv2.findContours(noblackholes, cv2.RETR_TREE,
                                      cv2.CHAIN_APPROX_SIMPLE)

    return contours
Exemple #3
0
def contouring(data):

    data = data.astype(np.float32)

    sym = data[:, ::-1]
    symd = (sym - data)**2
    symd = contrast((symd - symd.min()) * (data - data.min()))
    #display(symd)

    # TODO: more robust opening!
    kernel = np.ones((6, 6), np.uint8)
    symd = cv2.morphologyEx(symd, cv2.MORPH_OPEN, kernel, iterations=2)
    symd = contrast(symd)
    #display(symd)

    # Set filters (disable them, mostly)
    params = cv2.SimpleBlobDetector_Params()

    params.minThreshold = 100
    params.maxThreshold = 200
    params.thresholdStep = 50

    params.filterByColor = False
    params.blobColor = 255  # detect bright spot

    params.filterByArea = False
    #params.minArea = 1500

    params.filterByCircularity = True
    params.minCircularity = 0.3

    params.filterByConvexity = False
    #params.minConvexity = 0.87

    params.filterByInertia = False
    #params.minInertiaRatio = 0.01

    detector = cv2.SimpleBlobDetector_create(params)
    keypoints = detector.detect(symd)
    boxes = [makebox(kp) for kp in keypoints if validate(kp)]

    return merge_contours_naive(boxes)
def contouring_binsym(data):

    data = contrast(data)
    #display(data)

    _, bin = cv2.threshold(data, 0, 255, cv2.THRESH_OTSU)

    # keep only what doesn't appear on the other side
    sym = bin[:, ::-1]
    diff = bin - sym
    _, diff = cv2.threshold(
        diff, 128, 255,
        cv2.THRESH_BINARY)  # clean the '1' which mess up with findcontours

    #remove white dots. ok on ANON_LUNG_TC148 #54
    kernel = np.ones((3, 3), np.uint8)
    nowhitedots = cv2.morphologyEx(diff, cv2.MORPH_OPEN, kernel, iterations=2)

    #fill black holes
    kernel = np.ones((3, 3), np.uint8)
    noblackholes = cv2.morphologyEx(nowhitedots,
                                    cv2.MORPH_CLOSE,
                                    kernel,
                                    iterations=2)

    # Setup SimpleBlobDetector parameters.
    params = cv2.SimpleBlobDetector_Params()

    # Change thresholds
    params.minThreshold = 0
    params.maxThreshold = 255
    params.thresholdStep = 55

    # Filter by Color
    params.filterByColor = False
    params.blobColor = 255  # detect bright spot

    # Filter by Area.
    params.filterByArea = False
    params.minArea = 1500

    # Filter by Circularity
    params.filterByCircularity = True
    params.minCircularity = 0.1

    # Filter by Convexity
    params.filterByConvexity = False
    #params.minConvexity = 0.87

    # Filter by Inertia
    params.filterByInertia = True
    params.minInertiaRatio = 0.1  #More important than circularity actually

    detector = cv2.SimpleBlobDetector_create(params)
    keypoints = detector.detect(noblackholes)

    # We'll keep the CC corresponding to these keypoints.
    # Naive version :
    #  get contours of all CC, and test if one of a point is in contour

    # NB: COMP allows to get external boundaries, we don't care about holes
    _, contours, _ = cv2.findContours(noblackholes, cv2.RETR_TREE,
                                      cv2.CHAIN_APPROX_SIMPLE)

    kept = []
    for cnt in contours:
        for kp in keypoints:
            if cv2.pointPolygonTest(cnt, kp.pt, False) >= 0:
                kept.append(cnt)
                break

    return kept