Exemplo n.º 1
0
def findContoursAndDrawBoundingBox2(image_rgb):

    line_upper = []
    line_lower = []

    line_experiment = []

    grouped_rects = []

    gray_image = cv2.cvtColor(image_rgb, cv2.COLOR_BGR2GRAY)

    for k in np.linspace(-1.6, -0.2, 10):
        # for k in np.linspace(-15, 0, 15):
        # #
        #     thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
        #     binary_niblack = gray_image > thresh_niblack
        #     binary_niblack = binary_niblack.astype(np.uint8) * 255

        binary_niblack = nt.niBlackThreshold(gray_image, 19, k)
        # cv2.imshow("binary_niblack_opencv",binary_niblack_)
        # cv2.imshow("binary_niblack_skimage", binary_niblack)

        # cv2.waitKey(0)
        imagex, contours, hierarchy = cv2.findContours(binary_niblack.copy(),
                                                       cv2.RETR_EXTERNAL,
                                                       cv2.CHAIN_APPROX_SIMPLE)

        for contour in contours:
            bdbox = cv2.boundingRect(contour)
            if (bdbox[3] / float(bdbox[2]) > 0.7 and bdbox[3] * bdbox[2] > 100
                    and bdbox[3] * bdbox[2] < 1000) or (
                        bdbox[3] / float(bdbox[2]) > 3
                        and bdbox[3] * bdbox[2] < 100):
                # cv2.rectangle(rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
                line_upper.append([bdbox[0], bdbox[1]])
                line_lower.append([bdbox[0] + bdbox[2], bdbox[1] + bdbox[3]])

                line_experiment.append([bdbox[0], bdbox[1]])
                line_experiment.append(
                    [bdbox[0] + bdbox[2], bdbox[1] + bdbox[3]])
                # grouped_rects.append(bdbox)

    rgb = cv2.copyMakeBorder(image_rgb, 30, 30, 0, 0, cv2.BORDER_REPLICATE)
    leftyA, rightyA = fitLine_ransac(np.array(line_lower), 2)
    rows, cols = rgb.shape[:2]

    # rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)

    leftyB, rightyB = fitLine_ransac(np.array(line_upper), -4)

    rows, cols = rgb.shape[:2]

    # rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
    pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],
                           [cols - 1, rightyB], [0, leftyB]])
    pts_map2 = np.float32([[136, 36], [0, 36], [136, 0], [0, 0]])
    mat = cv2.getPerspectiveTransform(pts_map1, pts_map2)
    image = cv2.warpPerspective(rgb, mat, (136, 36), flags=cv2.INTER_CUBIC)
    image, M = pd.fastDeskew(image)
    return image
Exemplo n.º 2
0
def findContoursAndDrawBoundingBox2(image_rgb):
    line_upper = []
    line_lower = []
    line_experiment = []
    grouped_rects = []
    gray_image = cv2.cvtColor(image_rgb, cv2.COLOR_BGR2GRAY)
    for k in np.linspace(-1.6, -0.2, 10):
        binary_niblack = nt.niBlackThreshold(gray_image, 19, k)
        imagex, contours, hierarchy = cv2.findContours(
            binary_niblack.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        for contour in contours:
            bdbox = cv2.boundingRect(contour)
            if (bdbox[3] / float(bdbox[2]) > 0.7 and bdbox[3] * bdbox[2] > 100 and bdbox[3] * bdbox[2] < 1000) or (bdbox[3] / float(bdbox[2]) > 3 and bdbox[3] * bdbox[2] < 100):
                line_upper.append([bdbox[0], bdbox[1]])
                line_lower.append([bdbox[0] + bdbox[2], bdbox[1] + bdbox[3]])
                line_experiment.append([bdbox[0], bdbox[1]])
                line_experiment.append(
                    [bdbox[0] + bdbox[2], bdbox[1] + bdbox[3]])
    rgb = cv2.copyMakeBorder(image_rgb, 30, 30, 0, 0, cv2.BORDER_REPLICATE)
    leftyA, rightyA = fitLine_ransac(np.array(line_lower), 2)
    rows, cols = rgb.shape[:2]
    leftyB, rightyB = fitLine_ransac(np.array(line_upper), -4)
    rows, cols = rgb.shape[:2]
    pts_map1 = np.float32(
        [[cols - 1, rightyA], [0, leftyA], [cols - 1, rightyB], [0, leftyB]])
    pts_map2 = np.float32([[136, 36], [0, 36], [136, 0], [0, 0]])
    mat = cv2.getPerspectiveTransform(pts_map1, pts_map2)
    image = cv2.warpPerspective(rgb, mat, (136, 36), flags=cv2.INTER_CUBIC)
    image, M = deskew.fastDeskew(image)
    return image
def refineCrop(sections, width=16):
    new_sections = []
    for section in sections:
        # cv2.imshow("section¡",section)

        # cv2.blur(section,(3,3),3)

        sec_center = np.array([section.shape[1] / 2, section.shape[0] / 2])
        binary_niblack = nt.niBlackThreshold(section, 17, -0.255)
        imagex, contours, hierarchy = cv2.findContours(binary_niblack,
                                                       cv2.RETR_EXTERNAL,
                                                       cv2.CHAIN_APPROX_SIMPLE)
        boxs = []
        for contour in contours:
            x, y, w, h = cv2.boundingRect(contour)

            ratio = w / float(h)
            if ratio<1 and h>36*0.4 and y<16\
                    :
                box = [x, y, w, h]

                boxs.append([box, np.array([x + w / 2, y + h / 2])])
                # cv2.rectangle(section,(x,y),(x+w,y+h),255,1)

        # print boxs

        dis_ = np.array([((one[1] - sec_center)**2).sum() for one in boxs])
        if len(dis_) == 0:
            kernal = [0, 0, section.shape[1], section.shape[0]]
        else:
            kernal = boxs[dis_.argmin()][0]

        center_c = (kernal[0] + kernal[2] / 2, kernal[1] + kernal[3] / 2)
        w_2 = int(width / 2)
        h_2 = kernal[3] / 2

        if center_c[0] - w_2 < 0:
            w_2 = center_c[0]
        new_box = [center_c[0] - w_2, kernal[1], width, kernal[3]]
        # print new_box[2]/float(new_box[3])
        if new_box[2] / float(new_box[3]) > 0.5:
            # print "异常"
            h = int((new_box[2] / 0.35) / 2)
            if h > 35:
                h = 35
            new_box[1] = center_c[1] - h
            if new_box[1] < 0:
                new_box[1] = 1

            new_box[3] = h * 2

        section = section[new_box[1]:new_box[1] + new_box[3],
                          new_box[0]:new_box[0] + new_box[2]]
        # cv2.imshow("section",section)
        # cv2.waitKey(0)
        new_sections.append(section)
        # print new_box

    return new_sections
Exemplo n.º 4
0
def findContoursAndDrawBoundingBox2(image_rgb):


    line_upper  = [];
    line_lower = [];

    line_experiment = []

    grouped_rects = []

    gray_image = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)

    for k in np.linspace(-1.6, -0.2,10):
    # for k in np.linspace(-15, 0, 15):
    # #
    #     thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
    #     binary_niblack = gray_image > thresh_niblack
    #     binary_niblack = binary_niblack.astype(np.uint8) * 255

        binary_niblack = nt.niBlackThreshold(gray_image,19,k)
        # cv2.imshow("binary_niblack_opencv",binary_niblack_)
        # cv2.imshow("binary_niblack_skimage", binary_niblack)

        # cv2.waitKey(0)
        imagex, contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

        for contour in contours:
            bdbox = cv2.boundingRect(contour)
            if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1000) or (bdbox[3]/float(bdbox[2])>3 and bdbox[3]*bdbox[2]<100):
                # cv2.rectangle(rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
                line_upper.append([bdbox[0],bdbox[1]])
                line_lower.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])

                line_experiment.append([bdbox[0],bdbox[1]])
                line_experiment.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
                # grouped_rects.append(bdbox)

    rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
    leftyA, rightyA = fitLine_ransac(np.array(line_lower),2)
    rows,cols = rgb.shape[:2]

    # rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)

    leftyB, rightyB = fitLine_ransac(np.array(line_upper),-4)

    rows,cols = rgb.shape[:2]

    # rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
    pts_map1  = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
    pts_map2 = np.float32([[136,36],[0,36],[136,0],[0,0]])
    mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
    image = cv2.warpPerspective(rgb,mat,(136,36),flags=cv2.INTER_CUBIC)
    image,M= deskew.fastDeskew(image)


    return image
Exemplo n.º 5
0
def refineCrop(sections,width=16):
    new_sections = []
    for section in sections:
        # cv2.imshow("section¡",section)

        # cv2.blur(section,(3,3),3)

        sec_center = np.array([section.shape[1]/2,section.shape[0]/2])
        binary_niblack = nt.niBlackThreshold(section,17,-0.255)
        imagex, contours, hierarchy  = cv2.findContours(binary_niblack,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
        boxs = []
        for contour in contours:
            x,y,w,h = cv2.boundingRect(contour)

            ratio = w/float(h)
            if ratio<1 and h>36*0.4 and y<16\
                    :
                box = [x,y,w,h]

                boxs.append([box,np.array([x+w/2,y+h/2])])
                # cv2.rectangle(section,(x,y),(x+w,y+h),255,1)




        # print boxs

        dis_ = np.array([ ((one[1]-sec_center)**2).sum() for one in boxs])
        if len(dis_)==0:
            kernal = [0, 0, section.shape[1], section.shape[0]]
        else:
            kernal = boxs[dis_.argmin()][0]

        center_c  = (kernal[0]+kernal[2]/2,kernal[1]+kernal[3]/2)
        w_2 = int(width/2)
        h_2 = kernal[3]/2

        if center_c[0] - w_2< 0:
            w_2 = center_c[0]
        new_box = [center_c[0] - w_2,kernal[1],width,kernal[3]]
        # print new_box[2]/float(new_box[3])
        if new_box[2]/float(new_box[3])>0.5:
            # print "异常"
            h = int((new_box[2]/0.35 )/2)
            if h>35:
                h = 35
            new_box[1] = center_c[1]- h
            if new_box[1]<0:
                new_box[1] = 1

            new_box[3] = h*2








        section  = section[new_box[1]:new_box[1]+new_box[3],new_box[0]:new_box[0]+new_box[2]]
        # cv2.imshow("section",section)
        # cv2.waitKey(0)
        new_sections.append(section)
        # print new_box


    return new_sections