Exemplo n.º 1
0
def draw(pred, toDraw, morph_iter=0, threshold=127, thickness=2):
    thresh = moil.getBinaryThreshold(pred, threshold)

    closed = moil.morphMultiClosing(thresh, morph_iter)

    contour = moil.selectBiggerCircularContour(closed)

    if toDraw is not None and contour is not None:
        cv2.drawContours(toDraw, [contour], -1, (0, 0, 255), thickness)
Exemplo n.º 2
0
def getCenter(pred, widthRef, heightRef, morph_iter=0, threshold=127):
    thresh = moil.getBinaryThreshold(pred, threshold)

    closed = moil.morphMultiClosing(thresh, morph_iter)
    contour = moil.selectBiggerCircularContour(closed)
    w, h, c = moil.getWidthHeightChannels(pred)
    try:
        M = cv2.moments(contour)
        cx = int(M['m10'] / M['m00'])
        cy = int(M['m01'] / M['m00'])

    except Exception as e:
        print("No contour detected! Guessing for center...")
        cx = int(w / 2)
        cy = int(h / 2)
    w_scale = widthRef / w
    h_scale = heightRef / h
    cx = cx * w_scale
    cy = cy * h_scale
    return cx, cy
Exemplo n.º 3
0
def centerDiff(pred, true=None, x=None, y=None, width=None, height=None, r=None, morph_iter=0, threshold=127,
               toDraw=None, retDist=False):
    assert true is not None or (
            x is not None and y is not None and width is not None and height is not None and r is not None)

    thresh = moil.getBinaryThreshold(pred, threshold)

    closed = moil.morphMultiClosing(thresh, morph_iter)
    contour = moil.selectBiggerCircularContour(closed)
    if toDraw is not None and contour is not None:
        cv2.drawContours(toDraw, [contour], -1, (255, 255, 255), 1)

    w, h, c = moil.getWidthHeightChannels(pred)
    try:
        M = cv2.moments(contour)
        cx = int(M['m10'] / M['m00'])
        cy = int(M['m01'] / M['m00'])

    except Exception as e:
        print("No contour detected! Guessing for center...")
        cx = int(w / 2)
        cy = int(h / 2)

    if (x is None or y is None or width is None or height is None or r is None):

        width, height, chan = moil.getWidthHeightChannels(true)
        r = width / 10

        thresh = moil.getBinaryThreshold(true, threshold)

        im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        try:
            M = cv2.moments(contours[0])
            x = int(M['m10'] / M['m00'])
            y = int(M['m01'] / M['m00'])
        except:
            print("Bad Ground-truth! Mask in center...")
            x = int(w / 2)
            y = int(h / 2)

    w_scale = width / w
    h_scale = height / h
    cx = cx * w_scale
    cy = cy * h_scale

    dist = np.linalg.norm(np.asarray([cx, cy]) - np.asarray([x, y]))

    if retDist:
        return dist

    maxDist = np.linalg.norm(np.asarray([w / 2, h / 2]) - np.asarray([x, y]))

    DistanceMetric = 1 - dist / maxDist

    CrossLength = math.sqrt(width ** 2 + height ** 2)

    DistanceToCross = dist / CrossLength

    print("Distance Metric: " + str(DistanceMetric) + ", Relative Distance: " + str(
        DistanceToCross) + ", Distance: " + str(dist))

    return DistanceMetric