Exemplo n.º 1
0
def horizontal_pivot(img, table_file, plot, config):
    copy_img = img.copy()
    dimension = config.getint('horizontalpivot', 'eroseDimension')
    bandwidth = config.getfloat('horizontalpivot', 'bandwidth')
    erosion = enhance(copy_img, table_file, 1, dimension, None, None, config)
    # gray = cv2.cvtColor(norm_img, cv2.COLOR_BGR2GRAY)
    hei, wid = erosion.shape
    #print('shape', hei, wid)
    _, norm_img = cv2.threshold(erosion, 0, 1,
                                cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    '''projecting'''
    hist = np.sum(norm_img, axis=0, dtype=np.int32)
    invert_hist = hei - hist
    # logging.info('hist')
    list_bin = np.arange(0, wid)
    calculus_hist = np.repeat(list_bin, invert_hist)
    '''extremas of probability density'''
    is_blank = False
    try:
        mi, ma, s, prb = kernel_density_estimate(calculus_hist, wid, bandwidth)
    except:
        is_blank = True
        return None, erosion, is_blank
    #print('im her++++++++++++++++++++++++++++++++++++++++')
    estimate_wid = np.std(ma)
    #print('estimate wid ', estimate_wid)
    list_arg_pivot_min = mi
    # logging.info('list arg pivot min')
    # logging.info(len(list_arg_pivot_min))

    # check_cross = config.getint('horizontalpivot', 'checkcross')
    # if check_cross:config.read
    # 	gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # 	_, thesh = cv2.threshold(gray, 0, 1, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    # 	list_arg_pivot_min = checkCrossing(thesh, mi)
    ''' smooth pivot'''
    # logging.info('check smoothed hist')
    smoothed_bin = check_smooth_hist(copy_img, list_arg_pivot_min, ma,
                                     invert_hist, hei, config)
    # logging.info(len(smoothed_bin))
    # logging.info(smoothed_bin)

    nearest_neighbor = config.getint('horizontalpivot', 'nearestNeighbor')
    reduce_arg_min = reduction(copy_img, invert_hist, smoothed_bin, mi, ma,
                               nearest_neighbor, estimate_wid, config)
    # logging.info(reduce_arg_min)
    check_cross = False  #config.getint('horizontalpivot', 'checkcross')
    if check_cross:
        gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        _, thesh = cv2.threshold(gray, 0, 1,
                                 cv2.THRESH_BINARY | cv2.THRESH_OTSU)
        final_pivot = checkCrossing(thesh, reduce_arg_min)
    else:
        final_pivot = reduce_arg_min
    # logging.info(len(reduce_arg_min))
    # logging.info('list reduced arg min')
    # logging.info(final_pivot)
    '''plot'''
    if (plot):
        fig = plt.figure()
        ax1 = plt.subplot2grid((2, 3), (0, 0))
        ax1.set_xlim([0, wid])
        plt.hist(calculus_hist, bins=50, normed=1)
        plt.legend()
        plt.plot(s, prb)

        ax2 = plt.subplot2grid((2, 3), (0, 1), colspan=2)
        ax2.set_xlim([0, wid])
        # prb_unnorm = prb*hei
        plt.plot(s, prb, 'b', s[ma], prb[ma], 'go', s[mi], prb[mi], 'ro')

        # for i in final_pivot:
        # 	cv2.line(norm_img, (i, hei), (i, 0), (255, 0, 0), 1)
        ax3 = plt.subplot2grid((2, 3), (1, 0))
        ax3.set_xlim([0, wid])
        plt.imshow(erosion)

        # ax4 = plt.subplot2grid((2, 3), (1, 0))
        # ax4.set_xlim([0, wid])
        # ax4.set_title('smoothed bin')
        # plt.plot(list_bin, invert_hist, 'b',
        #          list_bin[smoothed_bin], hist[smoothed_bin], 'ro')

        for i in final_pivot:
            cv2.line(copy_img, (i, hei), (i, 0), (255, 0, 0), 2)
        ax5 = plt.subplot2grid((2, 3), (1, 1), colspan=2)
        ax5.set_xlim([0, wid])
        plt.imshow(copy_img)
        figManager = plt.get_current_fig_manager()
        figManager.resize(*figManager.window.maxsize())
        plt.tight_layout()
        plt.show()
        # fig.savefig('{}.PNG'.format(table_file[:-4]))
    return final_pivot, erosion, is_blank
Exemplo n.º 2
0
def horizontal_pivot(img, table_file, plot):
    # img = cv2.imread(table_file)
    # img1 = np.asarray(img.copy())
    erosion = enhance(img, table_file, 0)
    # gray = cv2.cvtColor(norm_img, cv2.COLOR_BGR2GRAY)
    hei, wid = erosion.shape
    print('shape', hei, wid)
    _, norm_img = cv2.threshold(erosion, 0, 1,
                                cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    '''projecting'''
    hist = np.sum(norm_img, axis=0, dtype=np.int32)
    invert_hist = hei - hist
    print('hi', wid)
    logging.info('hist')
    logging.info(hist)
    list_bin = np.arange(0, wid)
    num_ones = np.count_nonzero(norm_img)
    calculus_hist = np.repeat(list_bin, invert_hist)
    '''extremas of probability density'''
    mi, ma, s, prb = kernel_density_estimate(calculus_hist, wid, 20)
    thresh_interval_right = 160
    thresh_interval_left = 120
    list_arg_pivot_min = mi[(mi < wid - thresh_interval_right)
                            & (mi > thresh_interval_left)]
    logging.info('list arg pivot min')
    logging.info(list_arg_pivot_min.shape)
    nearest_neighbor = 120
    '''threshold for get low value pivot'''
    max_invert_hist = max(invert_hist[ma])
    logging.info('max_invert_hist')
    logging.info(max_invert_hist)
    '''filter low hist value '''
    list_arg_pivot_min = list_arg_pivot_min[
        invert_hist[list_arg_pivot_min] < max_invert_hist * 2 / 3]
    logging.info('list arg pivot min')
    logging.info(list_arg_pivot_min)
    reduce_arg_min = reduction(list_arg_pivot_min, nearest_neighbor)
    print(len(reduce_arg_min))
    logging.info('list reduced arg min')
    logging.info(reduce_arg_min)
    ''' smooth pivot'''
    smoothed_bin = check_smooth_hist(reduce_arg_min, ma, invert_hist)
    logging.info('check smoothed hist')
    logging.info(smoothed_bin)
    '''plot'''
    if (plot):
        fig = plt.figure()
        ax1 = plt.subplot(231)
        ax1.set_xlim([0, wid])
        plt.hist(calculus_hist, bins=50, normed=1)
        plt.legend()
        plt.plot(s, prb)

        ax2 = plt.subplot(232)
        ax2.set_xlim([0, wid])
        plt.plot(s, prb, 'b', s[ma], prb[ma], 'go', s[mi], prb[mi], 'ro')

        for i in smoothed_bin:
            cv2.line(norm_img, (i, hei), (i, 0), (255, 0, 0), 2)
        ax3 = plt.subplot(233)
        ax3.set_xlim([0, wid])
        plt.imshow(erosion)

        ax4 = plt.subplot(234)
        ax4.set_xlim([0, wid])
        plt.plot(list_bin, invert_hist, 'b', list_bin[smoothed_bin],
                 hist[smoothed_bin], 'ro')

        for i in smoothed_bin:
            cv2.line(img, (i, hei), (i, 0), (255, 0, 0), 4)
        ax5 = plt.subplot(235)
        ax5.set_xlim([0, wid])
        plt.imshow(img)

        figManager = plt.get_current_fig_manager()
        figManager.window.showMaximized()
        plt.tight_layout()
        plt.show()
    return smoothed_bin
Exemplo n.º 3
0
def horizontal_pivot(img, table_file, plot):
    dimension = 25
    erosion = enhance(img, table_file, 1, dimension)
    # gray = cv2.cvtColor(norm_img, cv2.COLOR_BGR2GRAY)
    hei, wid = erosion.shape
    #print('shape', hei, wid)
    _, norm_img = cv2.threshold(erosion, 0, 1,
                                cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    '''projecting'''
    hist = np.sum(norm_img, axis=0, dtype=np.int32)
    invert_hist = hei - hist
    #print('hi', wid)
    logging.info('hist')
    list_bin = np.arange(0, wid)
    calculus_hist = np.repeat(list_bin, invert_hist)
    '''extremas of probability density'''
    is_blank = False
    try:
        mi, ma, s, prb = kernel_density_estimate(calculus_hist, wid, 1.5)
    except:
        is_blank = True
        return None, erosion, is_blank
    estimate_wid = np.std(ma)
    #print('estimate wid ', estimate_wid)
    list_arg_pivot_min = mi
    logging.info('list arg pivot min')
    logging.info(len(list_arg_pivot_min))
    ''' smooth pivot'''
    logging.info('check smoothed hist')
    smoothed_bin = check_smooth_hist(list_arg_pivot_min, ma, invert_hist, hei)
    logging.info(len(smoothed_bin))
    logging.info(smoothed_bin)

    nearest_neighbor = 15
    reduce_arg_min = reduction(invert_hist, smoothed_bin, mi, ma,
                               nearest_neighbor, estimate_wid)
    # reduce_arg_min = smoothed_bin
    logging.info(len(reduce_arg_min))
    logging.info('list reduced arg min')
    logging.info(reduce_arg_min)
    '''plot'''
    if (plot):
        fig = plt.figure()
        ax1 = plt.subplot2grid((2, 3), (0, 0))
        ax1.set_xlim([0, wid])
        plt.hist(calculus_hist, bins=50, normed=1)
        plt.legend()
        plt.plot(s, prb)

        ax2 = plt.subplot2grid((2, 3), (0, 1), colspan=2)
        ax2.set_xlim([0, wid])
        plt.plot(s, prb, 'b', s[ma], prb[ma], 'go', s[mi], prb[mi], 'ro')

        for i in reduce_arg_min:
            cv2.line(norm_img, (i, hei), (i, 0), (255, 0, 0), 1)
        ax3 = plt.subplot2grid((2, 3), (1, 0))
        ax3.set_xlim([0, wid])
        plt.imshow(erosion)

        for i in reduce_arg_min:
            cv2.line(img, (i, hei), (i, 0), (255, 0, 0), 1)
        ax5 = plt.subplot2grid((2, 3), (1, 1), colspan=2)
        ax5.set_xlim([0, wid])
        plt.imshow(img)

        figManager = plt.get_current_fig_manager()
        figManager.window.showMaximized()
        plt.tight_layout()
        plt.show()
        # fig.savefig('{}.PNG'.format(table_file[:-4]))
    return reduce_arg_min, erosion, is_blank
Exemplo n.º 4
0
def vertical_pivot(img, table_file, plot):
    erosion = enhance(img, table_file, 1)
    hei, wid = erosion.shape
    print('shape', hei, wid)
    _, norm_img = cv2.threshold(erosion, 0, 1,
                                cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    '''projecting'''
    hist = np.sum(norm_img, axis=1, dtype=np.int32)
    invert_hist = wid - hist
    logging.info('hei', hei)
    list_bin = np.arange(0, hei)
    num_ones = np.count_nonzero(norm_img)
    calculus_hist = np.repeat(list_bin, invert_hist)
    '''extremas of probability density'''
    mi, ma, s, prb = kernel_density_estimate(calculus_hist, hei, 4)
    thresh_interval = 50
    list_arg_pivot_min = mi[mi > thresh_interval]
    nearest_neighbor = 20
    '''threshold for get low value pivot'''
    max_invert_hist = max(invert_hist[ma])
    print('max_invert_hist', max_invert_hist)
    list_arg_pivot_min = list_arg_pivot_min[
        invert_hist[list_arg_pivot_min] < max_invert_hist / 2]

    reduce_arg_min = reduction(list_arg_pivot_min, nearest_neighbor)
    '''sorting pivot'''
    '''adjust highest pivot'''
    if len(reduce_arg_min) >= 1:
        reduce_arg_min[0] -= 10
    print(len(reduce_arg_min))
    logging.info('list reduced arg min')
    logging.info(list_arg_pivot_min)
    '''plot'''
    if (plot):
        '''histogram'''
        fig = plt.figure()
        ax1 = plt.subplot(231)
        # ax1.set_xlim([0, wid])
        ax1.set_ylim([hei, 0])
        plt.hist(calculus_hist, bins=50, normed=1, orientation="horizontal")
        plt.legend()
        plt.plot(prb, s)
        '''kde'''
        ax2 = plt.subplot(232)
        # ax2.set_xlim([0, wid])
        ax2.set_ylim([hei, 0])
        plt.plot(prb, s, 'b', prb[ma], s[ma], 'go', prb[mi], s[mi], 'ro')
        '''erosion image'''
        for i in reduce_arg_min:
            cv2.line(norm_img, (wid, i), (0, i), (255, 0, 0), 2)
        ax3 = plt.subplot(233)
        ax3.set_xlim([0, wid])
        ax3.set_ylim([hei, 0])
        plt.imshow(erosion)
        ''''''
        # ax4 = plt.subplot(234)
        # ax4.set_xlim([0, wid])
        # plt.plot(list_bin, invert_hist, 'b',
        # 		 # list_bin[mi], hist[mi], 'ro',
        # 		list_bin[reduce_arg_min], hist[reduce_arg_min], 'ro')
        '''image'''
        for i in reduce_arg_min:
            cv2.line(img, (wid, i), (0, i), (255, 0, 0), 4)
        ax5 = plt.subplot(235)
        ax5.set_xlim([0, wid])
        ax5.set_ylim([hei, 0])
        plt.imshow(img)

        figManager = plt.get_current_fig_manager()
        figManager.window.showMaximized()
        plt.tight_layout()
        plt.show()
    return reduce_arg_min