def process_image(img_file, draw=False, drawsteps=False):

    img_src = cv2.imread(img_file, cv2.IMREAD_UNCHANGED)
    w = img_src.shape[1]
    h = img_src.shape[0]
    img_src, invdim = lkd.rescale(img_src, scale_percent=50)
    img_pref = lkd.prefilter_colors(img_src)
    img_grey = cv2.cvtColor(img_pref, cv2.COLOR_BGR2GRAY)
    thres, _ = cv2.threshold(img_grey, 20, 200,
                             cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    _, img_grey = cv2.threshold(img_grey, thres - 1, 255, cv2.THRESH_BINARY)
    org = np.array([int(img_grey.shape[1] / 2), int(img_grey.shape[0] / 2)])
    ret = lkd.process_image_center(img_src,
                                   img_grey,
                                   thres,
                                   org,
                                   draw=False,
                                   drawsteps=drawsteps)
    org = np.array(
        [int((ret[0][0] + ret[2][0]) / 2),
         int((ret[0][1] + ret[2][1]) / 2)])
    ret = lkd.process_image_center(img_src,
                                   img_grey,
                                   thres,
                                   org,
                                   draw=draw,
                                   drawsteps=drawsteps)
    ret = np.multiply(ret, invdim, out=ret, casting='unsafe', dtype=np.int32)
    # print(img_file)
    return ret, w, h
def process_image(img_file, draw=False):

    img_src = cv2.imread(img_file, cv2.IMREAD_UNCHANGED)
    w = img_src.shape[1]
    h = img_src.shape[0]
    img_src, invdim = lkd.rescale(img_src, scale_percent=50)
    img_pref = lkd.prefilter_colors(img_src)
    img_grey = cv2.cvtColor(img_pref, cv2.COLOR_BGR2GRAY)
    thres, _ = cv2.threshold(img_grey, 20, 200,
                             cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    _, img_grey = cv2.threshold(img_grey, thres - 1, 255, cv2.THRESH_BINARY)

    ret = 0

    cv2.imshow("GREY", img_grey)

    ret, _, _ = lkd.get_borders_from_grey_smear(img_src, img_grey, draw=draw)

    if ret is not None:
        if draw:
            minr = lkd.get_scaled_rect(ret, percentage=-4)
            maxr = lkd.get_scaled_rect(ret, percentage=7)

            lkd.drawimg(img_src, [ret], red=maxr, green=minr)
            cv2.waitKey(0)
        ret = np.multiply(ret,
                          invdim,
                          out=ret,
                          casting='unsafe',
                          dtype=np.int32)
    # print(img_file)
    return ret, w, h
def get_borderboxes_intervals(img_src):
    img_pref = lkd.prefilter_colors(img_src)
    img_grey = cv2.cvtColor(img_pref, cv2.COLOR_BGR2GRAY)
    vhist_grey = np.average(img_grey, axis=1)
    glimit_grey = np.min(vhist_grey) + np.average(vhist_grey) * .25
    bvhist_grey = thresbinv(vhist_grey, glimit_grey)

    # bvhist_grey = np.insert(bvhist_grey,0,[0])
    bvhist_grey = np.insert(bvhist_grey, len(bvhist_grey), [0])
    ranges = []
    prev = 0
    # cvopen = False
    cvec = None
    maxvlen = 0
    for idx, l in enumerate(bvhist_grey):
        if prev != l:
            if cvec is None:
                cvec = [idx, idx, 0]
            else:
                cvec[1] = idx - 1
                cvec[2] = cvec[1] - cvec[0] + 1
                ranges.append(cvec)
                if maxvlen < cvec[2]:
                    maxvlen = cvec[2]  # pylint: disable=unsubscriptable-object
                cvec = None
        prev = l
    ret = [[f[0], f[1]] for f in ranges if f[2] < maxvlen]
    return ret
def get_grey_otsu_bw(img_src):
    img_pref = lkd.prefilter_colors(img_src)
    img_grey = cv2.cvtColor(img_pref, cv2.COLOR_BGR2GRAY)
    _, img_bw = cv2.threshold(img_grey, 127, 255, cv2.THRESH_BINARY)
    thres, _ = cv2.threshold(img_grey, 20, 200,
                             cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    _, img_otsu = cv2.threshold(img_grey, thres - 1, 255, cv2.THRESH_OTSU)

    return img_grey, img_otsu, img_bw
示例#5
0
def process_single_file(img_filename, theta=0.01,minvert=4, maxvert=20,scale_percent=20,show=True):
    img_src = cv2.imread(img_filename, cv2.IMREAD_UNCHANGED)
    img_prefilt = lkd.prefilter_colors(img_src)
    # img_prefilt = img_process.copy() 
    img_grey = cv2.cvtColor(img_prefilt, cv2.COLOR_BGR2GRAY)
    ret = process_single(img_src, img_grey, imgfilename=img_filename, theta=theta, minvert=minvert, maxvert=maxvert, scale_percent=scale_percent, draw=show)
    if show:
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    return ret
示例#6
0
def process_image(img_file, scale_percent=50, draw=False, drawsteps=False):
    ret = None
    img_src = cv2.imread(img_file, cv2.IMREAD_UNCHANGED)
    w = img_src.shape[1]
    h = img_src.shape[0]
    img_src, invdim = lkd.rescale(img_src, scale_percent=scale_percent)
    img_pref = lkd.prefilter_colors(img_src)
    img_grey = cv2.cvtColor(img_pref, cv2.COLOR_BGR2GRAY)
    thres, _ = cv2.threshold(img_grey, 20, 200,
                             cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    _, img_grey = cv2.threshold(img_grey, thres - 1, 255, cv2.THRESH_BINARY)

    bbxes, mainbox = get_borderboxes(
        img_src)  # get paintboxes and paper-tissues if they exist
    if bbxes is not None:
        cv2.drawContours(img_grey, bbxes, 0, color=0,
                         thickness=-1)  # blacken them

    if mainbox is not None:  # alternative: blacken everyting
        img_grey, _ = lkd.blacken_outside_shape(img_grey, mainbox)

    if draw:
        lkd.drawimg(img_grey, [], label="GREYBLACKED")

    sdraw = draw

    shrinkrect, _, _ = lkd.process_image_shrink_boxdetect(img_src,
                                                          thres,
                                                          img_grey,
                                                          draw=draw,
                                                          drawsteps=drawsteps,
                                                          scale_percent=100)

    ret = shrinkrect

    lim = max(img_grey.shape)
    if ret.min() < 0 or ret.max() > lim:
        ret = np.array(
            [[0, 0], [img_grey.shape[1], 0],
             [img_grey.shape[1], img_grey.shape[0]], [0, img_grey.shape[0]]],
            dtype=np.int32)
    else:
        # minr = lkd.get_scaled_rect(ret, percentage=-7)
        maxr = lkd.get_scaled_rect(
            ret, percentage=7)  # 7 magic number by try and error

        # blacken out detected rectangle outer parts wit an offset of a certain percentage (see above)
        outerinv = np.zeros(img_grey.shape, dtype=np.uint8)
        outerinv = cv2.drawContours(outerinv, [maxr],
                                    0, [255, 255, 255],
                                    thickness=cv2.FILLED)
        img_grey = cv2.bitwise_and(img_grey, outerinv)

        ret = get_optimized_rect(img_src,
                                 img_grey,
                                 ret,
                                 draw=draw,
                                 drawsteps=drawsteps)

    draw = sdraw

    if draw:
        lkd.drawimg(img_src, [ret], green=ret)
    # print(img_file)
    ret = np.multiply(ret, invdim, out=ret, casting='unsafe', dtype=np.int32)
    return ret, w, h
示例#7
0
def get_borderboxes(img_src):
    """Returns boxes of upper and lower objects besides the main white middle area
       this removes paintboxes and papertissues from the given sample pictures.

       For concept illustration please see plotgeyvert.py
    """
    img_pref = lkd.prefilter_colors(
        img_src)  # ditch greens and fingers as usual :D
    img_grey = cv2.cvtColor(
        img_pref, cv2.COLOR_BGR2GRAY)  # standard grayscale works best
    vhist_grey = np.average(img_grey, axis=1)  # average every horizontal line
    glimit_grey = np.min(vhist_grey) + np.average(
        vhist_grey
    ) * .25  # we cant consider zero as bottom line but need to check the minumum
    # and need some offset to the "bottom" so by
    # looking at the sampled data 25% of the average gave good results in most cases
    bvhist_grey = thresbinv(
        vhist_grey, glimit_grey
    )  # we cant use any grey scale value but need a binary representation tissue/paintbox/painting = 1 "background" = 0

    bvhist_grey = np.insert(bvhist_grey, len(bvhist_grey),
                            [0])  # insert dummy zero at the beginning

    ranges = []  # collection of intervals
    prev = 0  # previouse signal value zero or one
    cvec = None  # current interval
    maxvlen = 0  # longest interval

    # go top down over the values - every continuous block of ones will be returned as a 'range' of the indexes (picture lines/rows)
    for idx, l in enumerate(bvhist_grey):
        if prev != l:  # change from 0 -> 1 or 1 -> 0
            if cvec is None:  # 0 -> 1
                cvec = [idx, idx, 0]  # start new box with start current index
            else:  # 1 -> 0
                cvec[1] = idx - 1  # close open box with end of previous index
                cvec[2] = cvec[1] - cvec[
                    0] + 1  # calculate length so we can remove the longuest (which should be the drawing in the picture)
                ranges.append(cvec)  # append to range collection
                if maxvlen < cvec[
                        2]:  # if this is the longes block we have seen?
                    maxvlen = cvec[2]  # remember the length                   # pylint: disable=unsubscriptable-object
                cvec = None  # reset
        prev = l

    ret = [
        [  # return a rectangle shape with the width of the src image (clockwise orientation)
            [0, f[0]],  # upper left corner                   
            [img_grey.shape[1], f[0]],  # upper right
            [img_grey.shape[1], f[1]],  # lower right
            [0, f[1]]  # lower left
        ] for f in ranges if f[2] <
        maxvlen  # for every range block that is smaller than the longest (which should be the drawing in the picture) 
    ]

    retmaxa = [
        [  # return a rectangle shape with the width of the src image (clockwise orientation)
            [0, f[0]],  # upper left corner                   
            [img_grey.shape[1], f[0]],  # upper right
            [img_grey.shape[1], f[1]],  # lower right
            [0, f[1]]  # lower left
        ] for f in ranges if f[2] ==
        maxvlen  # for every range block that is smaller than the longest (which should be the drawing in the picture) 
    ]

    retmax = None
    if len(retmaxa) > 0:
        retmax = np.array(retmaxa[0], dtype=np.int32)

    return np.array(ret, dtype=np.int32), retmax