Beispiel #1
0
def getPerspective(image, points):
    yy, xx, _ = image.shape
    tmp = np.zeros(image.shape[0:2], np.uint8);
    drawContour(tmp, points, (255,), 1)
    houghRatio=houghThreshold//hough_threshold_step

    grid = None
    for i in range(houghRatio):
        lines = cv2.HoughLines(tmp, 1, np.pi / 180, houghThreshold-(i * hough_threshold_step))
        if lines is None:
            continue
        lines = [Line(l[0], l[1]) for l in lines[0]]
        (horizontal, vertical) = partitionLines(lines)
        vertical = filterCloseLines(vertical, horizontal=False)
        horizontal = filterCloseLines(horizontal, horizontal=True)

        if len(vertical) == 2 and len(horizontal) == 2:
            grid = (vertical, horizontal)
            break


    if grid is None:
        return None


    if vertical[0].getCenter()[0] > vertical[1].getCenter()[0]:
        v2, v1 = vertical
    else:
        v1, v2 = vertical

    if horizontal[0].getCenter()[1] > horizontal[1].getCenter()[1]:
        h2, h1 = horizontal
    else:
        h1, h2 = horizontal



    perspective = (h1.intersect(v1),
                   h1.intersect(v2),
                   h2.intersect(v2),
                   h2.intersect(v1))

    ## Doc ##
    #tmp = cv2.cvtColor(tmp, cv2.COLOR_GRAY2BGR)
    #drawContour(tmp, points, (0,0,255), 3)
    #writeDocumentationImage(tmp, "contour_individual_bw")
    #tmp_bw = tmp
    #tmp_orig = image.copy()
    #for tmp in (tmp_bw, tmp_orig):
    #    for l in (v1,v2,h1,h2): l.draw(tmp, (0,255,0), 2)
    #    for p in perspective: drawPoint(tmp, p, (255,0,0), 3)
    #writeDocumentationImage(tmp_bw, "contour_lines_bw")
    #writeDocumentationImage(tmp_orig, "contour_lines_orig")
    ## Doc ##


    return perspective
Beispiel #2
0
def getPerspective(image, points):
    yy, xx, _ = image.shape
    tmp = np.zeros(image.shape[0:2], np.uint8);
    drawContour(tmp, points, (255,), 1)

    grid = None
    for i in range(houghThreshold/hough_threshold_step):
        lines = cv2.HoughLines(tmp, 1, np.pi / 180, houghThreshold-(i * hough_threshold_step))
        if lines is None:
            continue
        lines = [Line(l[0], l[1]) for l in lines[0]]
        (horizontal, vertical) = partitionLines(lines)
        vertical = filterCloseLines(vertical, horizontal=False)
        horizontal = filterCloseLines(horizontal, horizontal=True)

        if len(vertical) == 2 and len(horizontal) == 2:
            grid = (vertical, horizontal)
            break


    if grid is None:
        return None


    if vertical[0].getCenter()[0] > vertical[1].getCenter()[0]:
        v2, v1 = vertical
    else:
        v1, v2 = vertical

    if horizontal[0].getCenter()[1] > horizontal[1].getCenter()[1]:
        h2, h1 = horizontal
    else:
        h1, h2 = horizontal



    perspective = (h1.intersect(v1),
                   h1.intersect(v2),
                   h2.intersect(v2),
                   h2.intersect(v1))

    ## Doc ##
    #tmp = cv2.cvtColor(tmp, cv2.COLOR_GRAY2BGR)
    #drawContour(tmp, points, (0,0,255), 3)
    #writeDocumentationImage(tmp, "contour_individual_bw")
    #tmp_bw = tmp
    #tmp_orig = image.copy()
    #for tmp in (tmp_bw, tmp_orig):
    #    for l in (v1,v2,h1,h2): l.draw(tmp, (0,255,0), 2)
    #    for p in perspective: drawPoint(tmp, p, (255,0,0), 3)
    #writeDocumentationImage(tmp_bw, "contour_lines_bw")
    #writeDocumentationImage(tmp_orig, "contour_lines_orig")
    ## Doc ##


    return perspective
Beispiel #3
0
def extractGrid(img,
                nvertical,
                nhorizontal,
                threshold1=50,
                threshold2=150,
                apertureSize=3,
                hough_threshold_step=20,
                hough_threshold_min=50,
                hough_threshold_max=150):
    """Finds the grid lines in a board image.
    :param img: board image
    :param nvertical: number of vertical lines
    :param nhorizontal: number of horizontal lines
    :returns: a pair (horizontal, vertical). Both elements are lists with the lines' positions.
    """

    w, h, _ = img.shape
    close_threshold_v = (w / nvertical) / 4
    close_threshold_h = (h / nhorizontal) / 4

    im_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    thresh, im_bw = cv2.threshold(im_gray, 128, 255,
                                  cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    im_canny = cv2.Canny(im_bw,
                         threshold1,
                         threshold2,
                         apertureSize=apertureSize)

    for i in range((hough_threshold_max - hough_threshold_min + 1) /
                   hough_threshold_step):
        lines = cv2.HoughLines(
            im_canny, 1, np.pi / 180,
            hough_threshold_max - (hough_threshold_step * i))
        if lines is None:
            continue

        lines = [Line(l[0], l[1]) for l in lines[0]]
        horizontal, vertical = partitionLines(lines)
        vertical = filterCloseLines(vertical,
                                    horizontal=False,
                                    threshold=close_threshold_v)
        horizontal = filterCloseLines(horizontal,
                                      horizontal=True,
                                      threshold=close_threshold_h)

        if len(vertical) >= nvertical and \
           len(horizontal) >= nhorizontal:
            return (horizontal, vertical)
Beispiel #4
0
def extractGrid(
    img,
    nvertical,
    nhorizontal,
    threshold1=50,
    threshold2=150,
    apertureSize=3,
    hough_threshold_step=20,
    hough_threshold_min=50,
    hough_threshold_max=150,
):
    """Finds the grid lines in a board image.
    :param img: board image
    :param nvertical: number of vertical lines
    :param nhorizontal: number of horizontal lines
    :returns: a pair (horizontal, vertical). Both elements are lists with the lines' positions.
    """

    w, h, _ = img.shape
    close_threshold_v = (w / nvertical) / 4
    close_threshold_h = (h / nhorizontal) / 4

    im_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    thresh, im_bw = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    im_canny = cv2.Canny(im_bw, threshold1, threshold2, apertureSize=apertureSize)

    for i in range((hough_threshold_max - hough_threshold_min + 1) / hough_threshold_step):
        lines = cv2.HoughLines(im_canny, 1, np.pi / 180, hough_threshold_max - (hough_threshold_step * i))
        if lines is None:
            continue

        lines = [Line(l[0], l[1]) for l in lines[0]]
        horizontal, vertical = partitionLines(lines)
        vertical = filterCloseLines(vertical, horizontal=False, threshold=close_threshold_v)
        horizontal = filterCloseLines(horizontal, horizontal=True, threshold=close_threshold_h)

        if len(vertical) >= nvertical and len(horizontal) >= nhorizontal:
            return (horizontal, vertical)