Exemplo n.º 1
0
def main():
    from skimage.feature import peak_local_max
    from skimage.morphology import watershed
    import scipy.ndimage as ndi

    img = realImage()
    # img = testImage()
    img = fillHoles(img)

    thresh = img.copy()

    with utils.timeit_context():
        dst = ndi.distance_transform_edt(img)
        localMax = peak_local_max(dst, indices=False, min_distance=1, labels=thresh)
        markers = ndi.label(localMax)[0]
        labels = watershed(-dst, markers, mask=thresh)

    segmImg = (labels * (255 / labels.max())).astype(np.uint8)

    wnd = CvNamedWindow(flags=cv2.WINDOW_NORMAL)
    segmWnd = CvNamedWindow('segm', flags=cv2.WINDOW_NORMAL)

    wnd.imshow(img)
    segmWnd.imshow(segmImg)

    cvWaitKeys()
Exemplo n.º 2
0
def main_():
    img = testImage()
    cv2.imshow('', img)
    ret, labels = cv2.connectedComponents(img)
    print(ret, np.unique(labels), labels[100, 100])
    # 1oids)

    cvWaitKeys()
Exemplo n.º 3
0
def main():
    img = testImage()
    origWnd, dstWnd, centersWnd = CvNamedWindow.create('orig', 'dstTransform', 'centers')
    dst = cv2.distanceTransform(img, cv2.DIST_L2, cv2.DIST_MASK_5)

    dx = cv2.Sobel(dst, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=5)
    dy = cv2.Sobel(dst, ddepth=cv2.CV_32F, dx=0, dy=1, ksize=5)
    grad = cv2.addWeighted(cv2.convertScaleAbs(dx), 1, cv2.convertScaleAbs(dy), 1, 0)
    grad = cv2.convertScaleAbs(grad)

    origWnd.imshow(img)
    centersWnd.imshow(grad)
    condition = (grad == 0) & (dst > 0)
    print(np.where(condition))

    cvWaitKeys()
Exemplo n.º 4
0
def main():
    # img = testImage()
    img = realImage()
    n = 1
    sz = n * 2 + 1  # 3, 5 ...
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (sz, sz))
    iterations = 1
    mode = None  # erode dilate
    wnd = CvNamedWindow()

    def images():
        while True:
            if mode == 'dilate':
                cv2.dilate(img, kernel, dst=img, iterations=iterations)
            elif mode == 'erode':
                cv2.erode(img, kernel, dst=img, iterations=iterations)

            yield img

    for im in images():
        wnd.imshow(im)
        key = cvWaitKeys(27, '1', '2')
        if key == 27:
            break
        elif key == ord('1'):
            mode = 'erode'
        elif key == ord('2'):
            mode = 'dilate'
Exemplo n.º 5
0
def main():
    img = np.zeros((300, 300, 3), np.uint8)
    cv2.circle(img, (150, 150), 75, (0, 180, 0), -1)
    wnd = CvNamedWindow('select ROI')
    while True:
        wnd.imshow(img)
        key = cvWaitKeys(27, ord('r'), ord('s'))
        if key in (27, -1):
            break
Exemplo n.º 6
0
def main():
    oringWnd, dstWnd, centersWnd = CvNamedWindow.create('orig', {'dstTransform': cv2.WINDOW_NORMAL}, 'centers')

    img = testImage()
    # img = realImage()
    img = fillHoles(img)

    centers = img.copy()
    indexes, (distTransform, dx, dy) = findCentersIndexesSobel(img)
    centers[indexes] = 127

    oringWnd.imshow(img)
    centersWnd.imshow(centers)
    dstWnd.imshow(visDistTransformResult(distTransform))

    def mouse_callback(evt, x, y, flags, _):
        if evt == cv2.EVENT_LBUTTONDOWN:
            print(x, y, distTransform[y, x], dx[y, x], dy[y, x])

    dstWnd.mouse_callback = mouse_callback

    cvWaitKeys()