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 extractPiece(tile, margin=0.05):
    imgs = [tile]
    w, h, _ = tile.shape

    im_gray = cv2.cvtColor(tile, cv2.COLOR_BGR2GRAY)
    imgs.append(cv2.cvtColor(im_gray, cv2.COLOR_GRAY2BGR))

    #   im_gray = im_gray[(h*margin):(h*(1-margin)),
    #                     (w*margin):(w*(1-margin))]
    #   imgs.append(cv2.cvtColor(im_gray, cv2.COLOR_GRAY2BGR))

    #   im_gray = cv2.equalizeHist(im_gray)
    im_gray = cv2.medianBlur(im_gray, 3)
    imgs.append(cv2.cvtColor(im_gray, cv2.COLOR_GRAY2BGR))

    bright = np.mean(im_gray)
    im_bw = im_gray
    im_bw[np.where(im_gray < bright)] = 0
    im_bw[np.where(im_gray >= bright)] = 255
    imgs.append(cv2.cvtColor(im_bw, cv2.COLOR_GRAY2BGR))

    if np.mean(im_bw) < 128:
        im_bw = 255 - im_bw

    imgs.append(cv2.cvtColor(im_bw, cv2.COLOR_GRAY2BGR))

    #_, im_bw = cv2.threshold(im_gray, 50, 250, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    #im_bw = cv2.Canny(im_bw, 0,255, apertureSize=5)

    contours, hierarchy = cv2.findContours(im_bw.copy(), cv2.RETR_CCOMP,
                                           cv2.CHAIN_APPROX_TC89_KCOS)

    hulls = [cv2.convexHull(c) for c in contours]
    ids = ignoreContours(im_bw,
                         hulls,
                         max_area_percentage=0.75,
                         min_area_percentage=0.2)

    im_bw = cv2.cvtColor(im_bw, cv2.COLOR_GRAY2BGR)
    tmp = im_bw.copy()
    for i in ids:
        c = np.squeeze(hulls[i], 1)
        drawContour(tmp, c, randomColor(), thickness=1)

    imgs.append(tmp)

    return imgs