コード例 #1
0
def get_part_image(path, path1):
    text = open(path1).read()
    # text = re.sub(u"[\x00-\x08\x0b-\x0c\x0e-\x1f]+", u"", text)
    display = False
    root = ElementTree.fromstring(text)
    imageFilename = os.path.join(path, root[1].attrib['imageFilename'])
    image = np.where(io.imread(imageFilename) > 220, 1, 0)
    image_copy = copy.copy(image)
    row, col = image.shape
    img = np.zeros((row, col))
    total_x, total_y = [], []
    for i, textRegion in enumerate(root[1]):
        listx = []
        listy = []
        for j, pnt in enumerate(textRegion[0][:-1]):
            x = int(pnt.attrib['x'])
            y = int(pnt.attrib['y'])
            x = 0 if x < 0 else x
            x = col if x > col else x
            y = 0 if y < 0 else y
            y = row if y > row else y
            listx.append(x)
            listy.append(y)
        if (display):
            im1, im2, image, flag = isolate_character(image, listy, listx)
            if (flag == 1):
                p1 = plt.subplot(211)
                p1.set_title("原始图像")
                p1.imshow(image_copy)
                p2 = plt.subplot(223)
                p2.set_title("分割后的第一部分")
                p2.imshow(im1)
                p3 = plt.subplot(224)
                p3.set_title("剩余图像")
                p3.imshow(im2)
                plt.show()
            else:
                print("无法切分")
        total_x.append(listx)
        total_y.append(listy)
    print(image_copy.shape)
    total_image = isolate_character(image_copy,
                                    total_y,
                                    total_x,
                                    isdisplay=False)

    k = len(total_image)
    p = np.zeros(k)
    tk = plt.subplot(2, 1, 1)
    tk.set_title("整体切分图像,及分割后的基元部件")
    tk.imshow(image_copy)
    for i in range(k):
        plt.subplot(2, k, (k + i + 1)).imshow(total_image[i])
    plt.show()

    pass
コード例 #2
0
def get_part_image(XmlFile, isDisplayPart=False, isAlterXml=False):
    global count_file
    text = open(XmlFile, encoding='utf-8').read()
    # text = re.sub(u"[\x00-\x08\x0b-\x0c\x0e-\x1f]+", u"", text)
    root = ET.fromstring(text)
    imageFilename = os.path.join(path_image, root[-1].attrib['ImageFileName'])
    image = None
    try:
        image = np.where(io.imread(imageFilename) > 220, 1, 0)
    except FileNotFoundError:

        print('File:', imageFilename, "Not Found")
        print('File:', XmlFile, "Not Found")
        plt.subplot(111)
        plt.show()
        return -1;
    image_copy = copy.copy(image)
    image_copy_1 = copy.copy(image)
    row, col = image.shape

    total_x, total_y = [], []
    point_x, point_y = [], []
    for i, textRegion in enumerate(root[-1]):
        listx = []
        listy = []
        if (len(textRegion[0]) % 2 == 0):
            index = len(textRegion[0])
        else:
            index = -1
        for j, pnt in enumerate(textRegion[0][:index]):
            x = int(pnt.attrib['x'])
            y = int(pnt.attrib['y'])
            x = 0 if x < 0 else x
            x = col - 1 if x > col else x
            y = 0 if y < 0 else y
            y = row - 1 if y > row else y
            listx.append(x)
            listy.append(y)
            point_x.append(x)
            point_y.append(y)
        if (isDisplayPart):
            im1, im2, image, flag = isolate_character(image, listy, listx)
            if (flag == 1):
                p1 = plt.subplot(211)
                p1.set_title("原始图像")
                p1.imshow(image_copy)
                p2 = plt.subplot(223)
                p2.set_title("分割后的第一部分")
                p2.imshow(im1)
                p3 = plt.subplot(224)
                p3.set_title("剩余图像")
                p3.imshow(im2)
                plt.show()
            else:
                print("无法切分")

        total_x.append(listx)
        total_y.append(listy)
    # if(len(total_x) < 3):
    #     count_file = count_file + 3
    # else:
    #     count_file = count_file + len(total_x) + 1
    # return
    print(image_copy.shape)
    total_image, alter_x, alter_y = isolate_character(image_copy, total_y, total_x, isDisplayPart)
    print(alter_x)
    print(alter_y)
    print(imageFilename.split("\\")[-1].replace('chos lugs-pan chen blo chos kyi rgyal mtshan gsung', ''))
    print('    ', len(alter_x) * 2, '  VS  ', len(point_x))
    print(point_x)
    print(point_y)
    if (not isAlterXml):

        k = len(total_image)
        # if k > 2:
        #     return
        for i in range(k):
            row_each, col_each = np.array(total_image[i]).shape
            if(row_each < 10 or col_each < 10):
                continue
            count_file = count_file + 1
            plt.subplot(2, k, (k + i + 1)).imshow(total_image[i])
            # plt.text(20,20,"fff")
        p = np.zeros(k)
        tk = plt.subplot(2, 1, 1)
        tk.plot(point_x, point_y, "r.")
        # tk.plot([y for x in alter_y for y in x], [y for x in alter_x for y in x], "r.")
        tk.set_title(str(imageFilename.split("\\")[-1].split(".")[0]) + ",Pt:" + str(len(point_x)))
        tk.imshow(image_copy_1)
        plt.show()
    else:
        fileName = root[-1].attrib['imageFilename']
        imageHeight = str(int((row - 30) / 3 + 0.5))
        imageWidth = str(int((col - 30) / 3 + 0.5))
        alterXml(fileName, [imageHeight, imageWidth, alter_point(alter_y), alter_point(alter_x)], Alter_Xml_path)
        row, col = image_copy_1.shape
        image_copy_1 = (image_copy_1 * 255).astype(np.uint8)
        new_img = np.zeros((int((row - 30) / 3 + 0.5), int((col - 30) / 3 + 0.5)))
        new_img = cv2.resize(image_copy_1, (int(col / 3 + 0.5), int(row / 3 + 0.5)), interpolation=cv2.INTER_CUBIC)[
                  5:-5, 5:-5]
        io.imsave(os.path.join(Alter_Xml_path, fileName), new_img)
        # shutil.copyfile(imageFilename,os.path.join(Alter_Xml_path,fileName))
        print("save file:" + os.path.join(Alter_Xml_path, fileName))
コード例 #3
0
def get_part_image(XmlFile, isDisplayPart=False, isAlterXml=False):
    text = open(XmlFile, encoding='utf-8').read()
    # text = re.sub(u"[\x00-\x08\x0b-\x0c\x0e-\x1f]+", u"", text)
    root = ET.fromstring(text)
    imageFilename = os.path.join(path_image, root[-1].attrib['ImageFileName'])
    image = None
    try:
        image = np.where(io.imread(imageFilename) > 220, 1, 0)
    except FileNotFoundError:

        print('File:', imageFilename, "Not Found")
        return -1
    image_copy = copy.copy(image)
    image_copy_1 = np.where(image > 0, 0, 1)
    print(imageFilename)
    row, col = image.shape
    img = np.zeros((row, col))
    total_x, total_y = [], []
    point_x, point_y = [], []
    for i, textRegion in enumerate(root[-1]):
        listx = []
        listy = []
        if (len(textRegion[0]) % 2 == 0):
            index = len(textRegion[0])
        else:
            index = -1
        for j, pnt in enumerate(textRegion[0][:index]):
            x = int(pnt.attrib['x'])
            y = int(pnt.attrib['y'])
            x = 0 if x < 0 else x
            x = col if x > col else x
            y = 0 if y < 0 else y
            y = row if y > row else y
            listx.append(x)
            listy.append(y)
            point_x.append(x)
            point_y.append(y)
        if (isDisplayPart):
            im1, im2, image, flag = isolate_character(image, listy, listx)
            if (flag == 1):
                p1 = plt.subplot(211)
                p1.set_title("原始图像")
                p1.imshow(image_copy)
                p2 = plt.subplot(223)
                p2.set_title("分割后的第一部分")
                p2.imshow(im1)
                p3 = plt.subplot(224)
                p3.set_title("剩余图像")
                p3.imshow(im2)
                plt.show()
            else:
                print("无法切分")
        total_x.append(listx)
        total_y.append(listy)
    print(image_copy.shape)
    total_image, alter_x, alter_y = isolate_character(image_copy, total_y,
                                                      total_x, isDisplayPart)
    print(alter_x)
    print(alter_y)
    print(imageFilename)
    print('    ', len(alter_x) * 2, '  VS  ', len(point_x))
    print(point_x)
    print(point_y)
    if (not isAlterXml):
        # print(alter_x)
        # print(alter_y)
        # print('    ',len(alter_x) * 2,'  VS  ',len(point_x))
        # print(point_x)
        # print(point_y)
        k = len(total_image)

        # if not(k == 1):
        #     total_x, total_y = [], []
        #     point_x, point_y = [], []
        #     return 0
        # for i in range(k):
        #     plt.subplot(2,k,(k + i + 1)).imshow(total_image[i])
        #     # plt.text(20,20,"fff")
        # p = np.zeros(k)
        tk = plt.subplot(1, 1, 1)
        # tk.plot(point_x,point_y,"r")
        # tk.plot([y for x in alter_y for y in x], [y for x in alter_x for y in x], "r^", linewidth=8.0)
        tk.plot([alter_y[0][0][0]], [alter_x[0][0][0]], 'rv', linewidth=4.0)
        tk.plot([alter_y[0][0][1]], [alter_x[0][0][1]], 'r^', linewidth=4.0)

        tk.plot([0, 75], [24, 24], 'b.-', linewidth=4.0)
        # tk.set_title(str(imageFilename.split("\\")[-1].split(".")[0]) + ",Pt:" + str(len(point_x)))
        tk.imshow(image_copy_1, cmap='gray')

        plt.show()
    else:
        fileName = root[-1].attrib['imageFilename']
        imageHeight = str(row)
        imageWidth = str(col)
        alterXml(fileName, [
            imageHeight, imageWidth,
            alter_point(alter_y),
            alter_point(alter_x)
        ], Alter_Xml_path)
コード例 #4
0
def show_point(file):
    global totalpoint
    img = io.imread(file, as_grey=True)
    tt = (get_outline(
        fill_image((get_fileter_image(
            adjunction_image(np.where(img / 255 > 0.9, 1,
                                      0))))))).astype(np.uint8)
    img_filter = get_fileter_image(
        adjunction_image(np.where(img / 255 > 0.9, 1, 0)))
    fill_img = fill_image(
        (get_fileter_image(adjunction_image(np.where(img / 255 > 0.9, 1, 0)))))
    skelenton_image = skeletonize(fill_img)
    end_pointx, end_pointy = end_point(skelenton_image)
    # plt.subplot(211).imshow(fill_img)
    # pp = plt.subplot(212)
    # pp.imshow(skelenton_image)
    # pp.plot(end_pointy, end_pointx, 'r.')
    # plt.show()

    # plt.show()
    row, col = tt.shape
    # get_line(tt)
    horizontal_projection = np.sum(tt[:int(col / 2), :], axis=1)
    horizontal_projection_t = horizontal_projection[4:-4]
    index_zero = np.where(horizontal_projection_t < 2)
    index = np.where(
        horizontal_projection == np.max(horizontal_projection))[0][0]
    if (len(index_zero[0]) != 0):
        print("change index position")
        index = index_zero[0][0] + 4
    # print(horizontal_projection)
    print('基线位置: ', index)

    pointx, pointy = [], []
    vewol_point = []
    if (col <= 80):
        corners = cv2.goodFeaturesToTrack((tt).astype(np.uint8), 30, 0.2, 7)
    else:
        corners = cv2.goodFeaturesToTrack((tt).astype(np.uint8),
                                          30,
                                          0.001,
                                          0.05,
                                          blockSize=4)
    """
    1.8 skeleton
    water: 71.97

    outline:
    feature point 20,0.25,7 SVM 0.85  81.33 11859-5461
    feature point 20,0.30,7 SVM 0.85  79.72 11180-5353
    feature point 20,0.20,7 SVM 0.85  82.44 12751-5535
    feature point 20,0.20,7 SVM 0.85  82.23 12151-5521   alter-col

    1.4 skeleton
    water:  67.887

    the best result of outline:
    feature point 20,0.20,7 SVM 0.85  78.41 12151-5265   alter-col

    """
    print(row, col, '行列')
    corners = np.int0(corners)
    for item in corners:
        x, y = item.ravel()
        # cv2.circle(img, (x, y), 3, [0, 255, 255], -1)
        print(x, 'total', int(col / 3 + 0.5 + 2))
        if ((x <= int(col / 3 + 0.5) or x >= int(col / 3 * 2 + 0.5 - 2))
                and col <= 80):
            print(x, 'now x id')
            continue
        if ((x <= int(col / 4 + 0.5) or x >= int(col / 4 * 3 + 0.5))
                and col > 80):
            continue
        point_flag = True
        if (tt[y][x] == 0):
            y, x = alter_point(tt, y, x)
        vewol = 0
        for i in range(len(end_pointx)):
            if ((end_pointy[i] - x) * (end_pointy[i] - x) +
                (end_pointx[i] - y) *
                (end_pointx[i] - y) < 25) and (abs(y - index) >= 3):
                point_flag = False
                break
            elif (((end_pointy[i] - x) * (end_pointy[i] - x) +
                   (end_pointx[i] - y) * (end_pointx[i] - y) > 25)
                  and (index - y >= 2)):
                vewol = vewol + 1
        if (len(end_pointx) == vewol):
            vewol_point.append([x, y])
        if (not point_flag):
            continue
        if (abs(y - index) < 3 and not (total_point(tt, y, x) > 3)
                or y < index):
            continue

        pointx.append(x)
        pointy.append(y)

    alter_pointx, alter_pointy = alter_index_points(tt, index)
    pointx = pointx + alter_pointx
    pointy = pointy + alter_pointy
    alter_pointx, alter_pointy = alter_index_points(tt, index + 1)
    pointx = pointx + alter_pointx
    pointy = pointy + alter_pointy
    alter_pointx, alter_pointy = alter_index_points(tt, index + 2)
    pointx = pointx + alter_pointx
    pointy = pointy + alter_pointy
    alter_pointx, alter_pointy = alter_index_points(tt, index + 3)
    pointx = pointx + alter_pointx
    pointy = pointy + alter_pointy

    pointx, pointy = del_near_point(pointx, pointy)
    if (len(pointx) == 0):
        print("MDZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ")
        for item in corners:
            x, y = item.ravel()
            # cv2.circle(img, (x, y), 3, [0, 255, 255], -1)
            point_flag = True

            if (tt[y][x] == 0):
                y, x = alter_point(tt, y, x)
            if (abs(y - index) < 3 and not (total_point(tt, y, x) > 3)):
                continue
            if (x <= int(col / 3 + 0.5 + 1)
                    or x >= int(col / 3 * 2 + 0.5 - 1) and col < 80):
                continue
            if (x <= int(col / 4 + 0.5 + 2)
                    or x >= int(col / 4 * 3 + 0.5 - 2) and col >= 80):
                continue
            if (3 < abs(y - index) < 8):
                pointx.append(x)
                pointy.append(y)
    print('vewol: ', vewol_point)

    if (len(vewol_point) > 0):
        new_vewol = judge_vowel(img_filter, index, vewol_point)

        if (len(new_vewol) > 0):
            print('newwol: ', new_vewol)
            for i in range(len(new_vewol)):
                pointx.append(new_vewol[i][0])
                pointy.append(new_vewol[i][1])
    position_point = get_position(skelenton_image, pointx, pointy)
    if (len(pointx) < 1):
        print("can't find feature point!")
        return 0

    result_point = find_segmention_point(adjunction_image(img), index, pointy,
                                         pointx, position_point)
    print(pointx)
    print(pointy)
    print(position_point)
    # wrfile.write(os.path.basename(file) + ',')
    # for i in range(len(result_point)):
    #     wrfile.write(str(result_point[i][1] - 3) + ' ' + str(result_point[i][0] - 3) + ' '
    #                  + str(result_point[i][3] - 3) + ' ' + str(result_point[i][2] - 3) + ',')
    # wrfile.write('\n')

    result_pointx, result_pointy = [], []
    yyy, xxx = [], []
    for result in result_point:
        yy, xx = [], []
        if (result[0] > result[2]):
            result[0] = result[0] - 2
            result[2] = result[2] - 4
        else:
            result[0] = result[0] - 4
            result[2] = result[2] - 2
        yy.append(result[0])
        yy.append(result[2])
        xx.append(result[1] - 3)
        xx.append(result[3] - 3)

        yyy.append(result[0])
        yyy.append(result[2])
        xxx.append(result[1] - 3)
        xxx.append(result[3] - 3)
        result_pointy.append(yy)
        result_pointx.append(xx)
        rr, cc = line(result[0], result[1] - 3, result[2], result[3] - 3)
        img[rr, cc] = 0

    p2 = plt.subplot(211)
    p2.imshow(
        fill_image(
            get_fileter_image(adjunction_image(np.where(img / 255 > 0.9, 1,
                                                        0)))))
    p2.imshow(img)
    # print(corners)
    p1 = plt.subplot(212)
    p1.plot([1, col - 1], [index, index])
    if (col <= 80):
        p1.plot([int(col / 3 + 0.5), int(col / 3 + 0.5)], [0, row - 1],
                label[0])
        p1.plot([int(col / 3 * 2 + 0.5),
                 int(col / 3 * 2 + 0.5)], [0, row - 1], label[0])
    else:
        p1.plot([int(col / 4 + 0.5), int(col / 4 + 0.5)], [0, row - 1],
                label[0])
        p1.plot([int(col / 4 * 3 + 0.5),
                 int(col / 4 * 3 + 0.5)], [0, row - 1], label[0])

    for i in range(len(result_point)):
        p2.plot([result_point[i][1], result_point[i][3]],
                [result_point[i][0], result_point[i][2]], 'b.-')
    p2.imshow(adjunction_image(img))
    p1 = plt.subplot(111)
    p1.imshow(np.where(skelenton_image > 0, 0, 1), cmap='gray')
    p1.plot([pointx[0], pointx[2]], [pointy[0], pointy[2]],
            'r^',
            linewidth=8.0)
    p1.plot([pointx[1]], [pointy[1]], 'rv', linewidth=8.0)
    plt.axis('off')
    plt.show()
    total_image, alter_x, alter_y = isolate_character(img / 255, result_pointy,
                                                      result_pointx, False)
    print(len(total_image))
    k = len(total_image)
    # for i in range(k):
    #     row_each, col_each = np.array(total_image[i]).shape
    #     if (row_each < 10 or col_each < 10):
    #         continue
    #     plt.subplot(2, k, (k + i + 1)).imshow(total_image[i])
    #     # plt.text(20,20,"fff")
    # p = np.zeros(k)
    tk = plt.subplot(1, 1, 1)
    plt.axis('off')
    tk.plot([xxx[1], xxx[2], xxx[5]], [yyy[1], yyy[2], yyy[5]],
            'rv',
            linewidth=4.0)
    tk.plot([xxx[0], xxx[3], xxx[4]], [yyy[0], yyy[3], yyy[4]],
            'r^',
            linewidth=4.0)

    tk.imshow(np.where(img > 240, 0, 1), cmap='gray')
    plt.show()