def radicalsExtractBtn(self): """ Radical extract button clicked function. :return: """ print("Radicals extract btn clicked!") # clean self.scene.clear() self.lastPoint = None self.endPoint = None if self.image_gray is None: QMessageBox.information(self, "Grayscale image is None!") return self.radicals = None # get all radicals of character radicals = splitConnectedComponents(self.image_gray) print("number of radicals: %d" % len(radicals)) self.radicals = radicals.copy() self.radicals_name = [] for i in range(len(radicals)): self.radicals_name.append("radical_" + str(i + 1)) self.radical_slm.setStringList(self.radicals_name) self.scene.lastPoint = QPoint() self.scene.endPoint = QPoint() self.statusbar.showMessage("Radicals extracted successed!") del radicals
def main(): charset_dir = "../simkaidataset6958" char_list = [] char_names = [] for fl in os.listdir(charset_dir): if ".jpg" in fl: char_list.append(fl) char_names.append(os.path.splitext(fl)[0]) print(char_list) print(char_names) radicals_path = charset_dir + "/radicals" strokes_path = charset_dir + "/strokes" for i in range(len(char_list)): print("index: %d" % i) f_path = os.path.join(charset_dir, char_list[i]) f_name = char_names[i] img_gray = cv2.imread(f_path, 0) _, img_gray = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY) boxes = splitConnectedComponents(img_gray) print(len(boxes)) radical_path = str(os.path.join(radicals_path, f_name)) if not os.path.exists(radical_path): os.mkdir(radical_path) for bi in range(len(boxes)): r_path = radical_path + "/radicals_" + str(bi) + ".jpg" cv2.imwrite(r_path, boxes[bi])
def main(): img_path = "ben.png" img = cv2.imread(img_path, 0) _, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # extract radicals of character radicals = splitConnectedComponents(img) print("radicals len: %d" % len(radicals)) for id, rd in enumerate(radicals): cv2.imshow("id:" + str(id), rd) cv2.imshow("src", img) cv2.waitKey(0) cv2.destroyAllWindows()
def chars_dataset_generationg(): if not os.path.exists(save_path): os.mkdir(save_path) tree = ET.parse(xml_path) if tree is None: print("tree is none!") return root = tree.getroot() print("root len:", len(root)) for child in root: tag = child.attrib["TAG"].strip() if len(tag) > 1: continue # if not os.path.exists(os.path.join(save_path, tag)): # os.mkdir(os.path.join(save_path, tag)) # # # copy char image img_name = "" for fn in file_names: if fn.startswith(tag): shutil.copy2(os.path.join(char_png_path, fn), os.path.join(save_path, tag, fn)) img_name = fn break if not os.path.exists(os.path.join(save_path, tag, img_name)): print(tag) continue # img_ = cv2.imread(os.path.join(save_path, tag, img_name), 0) # # # make structure directory # if not os.path.exists(os.path.join(save_path, tag, "structures")): # os.mkdir(os.path.join(save_path, tag, "structures")) # # # make basic radicals directory # if not os.path.exists(os.path.join(save_path, tag, "basic radicals")): # os.mkdir(os.path.join(save_path, tag, "basic radicals")) # # print(tag) connect_components = splitConnectedComponents(img_) for i in range(len(connect_components)): cv2.imwrite(os.path.join(save_path, tag, "basic radicals", img_name.replace(".png", "") + "_" + str(i) + ".png"), connect_components[i])
def main(): # 0107亻 1133壬 0554十 0427凹 path = "0554十.jpg" # open image img = cv2.imread(path, 0) _, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) img_rgb = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) # contour without break points contour = getContourOfImage(img.copy()) contour = removeBreakPointsOfContour(contour) contour_rgb = cv2.cvtColor(contour, cv2.COLOR_GRAY2RGB) contours = splitConnectedComponents(contour) print("contours num: %d" % len(contours)) contours_sorted = [] for cont in contours: points = sortPointsOnContourOfImage(cont) print("points num: %d" % len(points)) contours_sorted.append(points) contour_points = [] for y in range(contour.shape[0]): for x in range(contour.shape[1]): if contour[y][x] == 0.0: # black points contour_points.append((x, y)) print("contour points num:%d" % len(contour_points)) # skeleton without extra branches skeleton = getSkeletonOfImage(img.copy()) # remove extra branches end_points = getEndPointsOfSkeletonLine(skeleton) cross_points = getCrossPointsOfSkeletonLine(skeleton) print("originale end: %d and cross: %d" % (len(end_points), len(cross_points))) skeleton_nobranches = removeBranchOfSkeletonLine(skeleton.copy(), end_points, cross_points) skeleton = skeleton_nobranches # new end points and cross points end_points = getEndPointsOfSkeletonLine(skeleton) cross_points = getCrossPointsOfSkeletonLine(skeleton) cross_points_bk = cross_points.copy() # merge the close points cross_points_merged = [] cross_distance_threshold = 10 used_index = [] for i in range(len(cross_points)): if i in used_index: continue pt1 = cross_points[i] midd_pt = None used_index.append(i) for j in range(len(cross_points)): if i == j or j in used_index: continue pt2 = cross_points[j] dist = math.sqrt((pt2[0] - pt1[0])**2 + (pt2[1] - pt1[1])**2) if dist < cross_distance_threshold: used_index.append(j) offset = (pt1[0] - pt2[0], pt1[1] - pt2[1]) print(offset) midd_pt = (pt2[0] + int(offset[0] / 2.), pt2[1] + int(offset[1] / 2.0)) if skeleton[midd_pt[1]][midd_pt[0]] == 0.0: cross_points_merged.append(midd_pt) else: min_distance = 100000000 current_pt = None for y in range(skeleton.shape[0]): for x in range(skeleton.shape[1]): if skeleton[y][x] == 0: dist = math.sqrt((midd_pt[0] - x)**2 + (midd_pt[1] - y)**2) if dist < min_distance: min_distance = dist current_pt = (x, y) if current_pt: cross_points_merged.append(current_pt) print("After merge cross points num: %d" % len(cross_points_merged)) cross_points = cross_points_merged print("After end: %d and cross: %d" % (len(end_points), len(cross_points))) skeleton_rgb = cv2.cvtColor(skeleton, cv2.COLOR_GRAY2RGB) # display all end points for pt in end_points: skeleton_rgb[pt[1]][pt[0]] = (0, 0, 255) for pt in cross_points: skeleton_rgb[pt[1]][pt[0]] = (0, 255, 0) for pt in cross_points_bk: skeleton_rgb[pt[1]][pt[0]] = (0, 0, 255) # all corner points on contour img = np.float32(img.copy()) dst = cv2.cornerHarris(img, 3, 3, 0.03) dst = cv2.dilate(dst, None) corners_area_points = [] for y in range(dst.shape[0]): for x in range(dst.shape[1]): if dst[y][x] > 0.1 * dst.max(): corners_area_points.append((x, y)) # show the corner points for pt in corners_area_points: if img[pt[1]][pt[0]] == 0: img_rgb[pt[1]][pt[0]] = (0, 255, 0) else: img_rgb[pt[1]][pt[0]] = (0, 0, 255) # all corner area points on the contour corners_lines_points = [] for pt in corners_area_points: if pt in contour_points: corners_lines_points.append(pt) for pt in corners_lines_points: contour_rgb[pt[1]][pt[0]] = (0, 255, 0) # merge points of corner points corners_merged_points = [] for contour_sorted in contours_sorted: i = 0 while True: midd_index = -1 pt = contour_sorted[i] if pt in corners_lines_points: # red point start = i end = start while True: end += 1 if end >= len(contour_sorted): break # next point next_pt = contour_sorted[end] if next_pt in corners_lines_points: # red point continue else: # black point break end -= 1 midd_index = start + int((end - start) / 2.0) i = end i += 1 if i >= len(contour_sorted): break if midd_index != -1: corners_merged_points.append(contour_sorted[midd_index]) print("After merged, corner points num: %d" % len(corners_merged_points)) for pt in corners_merged_points: contour_rgb[pt[1]][pt[0]] = (0, 0, 255) # remove the no-corner points corners_points = [] threshold_distance = 30 for pt in corners_merged_points: dist_cross = min_distance_point2pointlist(pt, cross_points) dist_end = min_distance_point2pointlist(pt, end_points) if dist_cross < threshold_distance and dist_end > threshold_distance / 3.: corners_points.append(pt) print("corner pints num: %d" % len(corners_points)) for pt in corners_points: contour_rgb[pt[1]][pt[0]] = (255, 0, 0) # segment contour to sub-contours based on the corner points def segmentContourBasedOnCornerPoints(contour_sorted, corner_points): """ Segment contour to sub-contours based on the corner points :param contour_sorted: :param corner_points: :return: """ if contour_sorted is None or corner_points is None: return # sub conotour index sub_contour_index = [] for pt in corner_points: index = contour_sorted.index(pt) sub_contour_index.append(index) print("sub contour index num: %d" % len(sub_contour_index)) sub_contours = [] for i in range(len(sub_contour_index)): if i == len(sub_contour_index) - 1: sub_contour = contour_sorted[sub_contour_index[i]:len( contour_sorted)] + contour_sorted[0:sub_contour_index[0] + 1] else: sub_contour = contour_sorted[ sub_contour_index[i]:sub_contour_index[i + 1] + 1] sub_contours.append(sub_contour) print("sub contours num: %d" % len(sub_contours)) return sub_contours # segment contour to sub-contours for contour in contours: cont_sorted = sortPointsOnContourOfImage(contour) sub_contours = segmentContourBasedOnCornerPoints( cont_sorted, corners_points) # cluster corner points corner_points_cluster = [] used_index = [] colinear_couple = [] for i in range(len(corners_points)): if i in used_index: continue for j in range(len(corners_points)): if i == j or j in used_index: continue min_offset = min(abs(corners_points[i][0] - corners_points[j][0]), abs(corners_points[i][1] - corners_points[j][1])) if min_offset < 20: couple = [corners_points[i], corners_points[j]] colinear_couple.append(couple) used_index.append(j) print("co linear num: %d" % len(colinear_couple)) print("sub contours num: %d" % len(sub_contours)) stroke1_img = np.ones_like(contour) * 255 stroke1_img = np.array(stroke1_img, dtype=np.uint8) stroke1_img_rgb = cv2.cvtColor(stroke1_img, cv2.COLOR_GRAY2RGB) for pt in sub_contours[0]: stroke1_img_rgb[pt[1]][pt[0]] = (0, 0, 0) stroke1_img[pt[1]][pt[0]] = 0 for pt in sub_contours[2]: stroke1_img_rgb[pt[1]][pt[0]] = (0, 0, 0) stroke1_img[pt[1]][pt[0]] = 0 cv2.line(stroke1_img_rgb, sub_contours[0][0], sub_contours[2][-1], (0, 0, 255), 1) cv2.line(stroke1_img_rgb, sub_contours[0][-1], sub_contours[2][0], (0, 0, 255), 1) cv2.line(stroke1_img, sub_contours[0][0], sub_contours[2][-1], 0, 1) cv2.line(stroke1_img, sub_contours[0][-1], sub_contours[2][0], 0, 1) stroke2_img = np.ones_like(contour) * 255 stroke2_img = np.array(stroke2_img, dtype=np.uint8) stroke2_img_rgb = cv2.cvtColor(stroke2_img, cv2.COLOR_GRAY2RGB) for pt in sub_contours[1]: stroke2_img_rgb[pt[1]][pt[0]] = (0, 0, 0) stroke2_img[pt[1]][pt[0]] = 0 for pt in sub_contours[3]: stroke2_img_rgb[pt[1]][pt[0]] = (0, 0, 0) stroke2_img[pt[1]][pt[0]] = 0 cv2.line(stroke2_img_rgb, sub_contours[1][0], sub_contours[3][-1], (0, 0, 255), 1) cv2.line(stroke2_img_rgb, sub_contours[1][-1], sub_contours[3][0], (0, 0, 255), 1) cv2.line(stroke2_img, sub_contours[1][0], sub_contours[3][-1], 0, 1) cv2.line(stroke2_img, sub_contours[1][-1], sub_contours[3][0], 0, 1) storke1_points = sortPointsOnContourOfImage(stroke1_img) stroke2_points = sortPointsOnContourOfImage(stroke2_img) stroke1_img = np.ones_like(stroke1_img) * 255 stroke1_img = np.array(stroke1_img, dtype=np.uint8) storke1_points = np.array([storke1_points], "int32") cv2.fillPoly(stroke1_img, storke1_points, 0) stroke2_img = np.ones_like(stroke2_img) * 255 stroke2_img = np.array(stroke2_img, dtype=np.uint8) storke2_points = np.array([stroke2_points], "int32") cv2.fillPoly(stroke2_img, storke2_points, 0) # find corresponding sub-contours based on the co-linear couple # for sub in sub_contours: # pt1 = sub[0] # pt2 = sub[-1] # # couples = [] # for coup in colinear_couple: # if pt1 in coup or pt2 in coup: # # if 4 points, 2 points should be in same sub-contour # if pt1 in coup and pt2 in coup: # continue # couples.append(coup) # print("sub couples num: %d" % len(couples)) # cv2.imshow("img rgb", img_rgb) # cv2.imshow("skeleton", skeleton) # cv2.imshow("skeleton no branches", skeleton_nobranches ) cv2.imshow("skeleton rgb", skeleton_rgb) cv2.imshow("contour rgb", contour_rgb) cv2.imshow("stroke 1", stroke1_img) cv2.imshow("stroke 2", stroke2_img) cv2.imshow("stroke1rgb", stroke1_img_rgb) cv2.imshow("stroke2rgb", stroke2_img_rgb) # for i in range(len(contours)): # cv2.imshow("contour %d" % i, contours[i]) cv2.waitKey(0) cv2.destroyAllWindows()
def main(): # Point Point = namedtuple("Point", ["x", "y"]) # target image target_path = "../templates/ben.png" target_img = cv2.imread(target_path, 0) _, target_img = cv2.threshold(target_img, 127, 255, cv2.THRESH_BINARY) print(target_img.shape) target_img_rgb = cv2.cvtColor(target_img, cv2.COLOR_GRAY2RGB) # connected components with labeling algorithm partial_parts = splitConnectedComponents(target_img) print("number of parts: %d" % len(partial_parts)) # part 1 part_1 = partial_parts[0] # skeleton line part_1_ = part_1 != 255 part_skel = skeletonize(part_1_) part_skel = (1 - part_skel) * 255 part_skel = np.array(part_skel, dtype=np.uint8) for y in range(part_skel.shape[0]): for x in range(part_skel.shape[1]): if part_skel[y][x] == 0.0: target_img_rgb[y][x] = (0, 255, 0) # remove extra branch end_points = getEndPointsOfSkeletonLine(part_skel) cross_points = getCrossPointsOfSkeletonLine(part_skel) part_skel = removeBranchOfSkeletonLine( part_skel, end_points, cross_points, ) part_1_rgb_no_branch = cv2.cvtColor(part_1, cv2.COLOR_GRAY2RGB) for y in range(part_skel.shape[0]): for x in range(part_skel.shape[1]): if part_skel[y][x] == 0.0: part_1_rgb_no_branch[y][x] = (0, 255, 0) # new end points and cross points without extra branches end_points = getEndPointsOfSkeletonLine(part_skel) print("number of end points: %d" % len(end_points)) cross_points = getCrossPointsOfSkeletonLine(part_skel) print("number of cross points: %d" % len(cross_points)) # add end points to image with blue color for (x, y) in end_points: part_1_rgb_no_branch[y][x] = (255, 0, 0) # add cross points to image with red color for (x, y) in cross_points: part_1_rgb_no_branch[y][x] = (0, 0, 255) # Contour of character part_1_edges = cv2.Canny(part_1, 100, 200) part_1_edges = 255 - part_1_edges # Order the points on contours of character print(part_1_edges.shape) begin_point = None # find the begin point for y in range(part_1_edges.shape[0]): for x in range(part_1_edges.shape[1]): if part_1_edges[y][x] == 0.0: # first black point begin_point = (x, y) break if begin_point: break print("begin point: (%d, %d)" % (begin_point[0], begin_point[1])) edge_order_lables = np.zeros_like(part_1_edges) edge_order_lables[begin_point[1]][begin_point[0]] = 1. curr_point = begin_point # find the second point if part_1_edges[y][x + 1] == 0.0: print("Second point is 4 position") curr_point = (x + 1, y) elif part_1_edges[y + 1][x + 1] == 0.0: print("Second point is 5 position") curr_point = (x + 1, y + 1) print(curr_point) edge_order_lables[curr_point[1]][curr_point[0]] = 1. edge_points = [] edge_points.append(begin_point) edge_points.append(curr_point) next_point = curr_point edge_id = 0 while True: x = curr_point[0] y = curr_point[1] # 2,4,6,8 position firstly and then 3,5,7,9 position # point in 2 position if part_1_edges[y - 1][x] == 0.0 and edge_order_lables[y - 1][x] == 0.0: print("%d po" % 2) next_point = (x, y - 1) edge_order_lables[y - 1][x] = 1. # point in 4 position elif part_1_edges[y][x + 1] == 0.0 and edge_order_lables[y][x + 1] == 0.0: print("%d po" % 4) next_point = (x + 1, y) edge_order_lables[y][x + 1] = 1. # point in 6 position elif part_1_edges[y + 1][x] == 0.0 and edge_order_lables[y + 1][x] == 0.0: print("%d po" % 6) next_point = (x, y + 1) edge_order_lables[y + 1][x] = 1. # point in 8 position elif part_1_edges[y][x - 1] == 0.0 and edge_order_lables[y][x - 1] == 0.0: print("%d po" % 8) next_point = (x - 1, y) edge_order_lables[y][x - 1] = 1. # point in 3 position elif part_1_edges[y - 1][x + 1] == 0.0 and edge_order_lables[y - 1][ x + 1] == 0.0: print("%d po" % 3) next_point = (x + 1, y - 1) edge_order_lables[y - 1][x + 1] = 1. # point in 5 position elif part_1_edges[y + 1][x + 1] == 0.0 and edge_order_lables[y + 1][ x + 1] == 0.0: print("%d po" % 5) next_point = (x + 1, y + 1) edge_order_lables[y + 1][x + 1] = 1. # point in 7 position elif part_1_edges[y + 1][x - 1] == 0.0 and edge_order_lables[y + 1][ x - 1] == 0.0: print("%d po" % 7) next_point = (x - 1, y + 1) edge_order_lables[y + 1][x - 1] = 1. # point in 9 position elif part_1_edges[y - 1][x - 1] == 0.0 and edge_order_lables[y - 1][ x - 1] == 0.0: print("%d po" % 9) next_point = (x - 1, y - 1) edge_order_lables[y - 1][x - 1] = 1. if next_point == curr_point: print(next_point) print(curr_point) edge_points.append(curr_point) break else: edge_points.append(curr_point) edge_id += 1 print("edge id: %d" % edge_id) curr_point = next_point print("edge points len: %d" % len(edge_points)) for pt in edge_points: print(pt) part_1_rgb_no_branch[pt[1]][pt[0]] = (0, 0, 255) # # houngh lines # rho_resolution = 1 # theta_resolution = np.pi / 180 # threshold = 155 # hough_lines = cv2.HoughLines(part_1_edges, rho_resolution, theta_resolution, threshold) # # print("number of hough lines: %d " % len(hough_lines)) # # hough_lines_img = np.zeros_like(part_1_edges) # draw_lines(hough_lines_img, hough_lines) # original_image_with_hough_lines = weighted_img(hough_lines_img, part_1_edges) # # cv2.imshow("hough line image", hough_lines_img) # cv2.imshow("original hough", original_image_with_hough_lines) # # Corner detection # corners = cv2.goodFeaturesToTrack(part_1_edges, 100, 0.01, 10) # corners = np.int0(corners) # # for i in corners: # x, y = i.ravel() # cv2.circle(part_1_rgb_no_branch, (x, y), 3, 255, -1) cv2.imshow("img", target_img) # cv2.imshow("skel", part_skel) # cv2.imshow("img_rgb", target_img_rgb) cv2.imshow("img_rgb_no_branch", part_1_rgb_no_branch) cv2.imshow("edges", part_1_edges) cv2.imwrite("../templates/ben_skeleton.png", target_img_rgb) cv2.imwrite("../templates/ben_skeleton_no_branch.png", part_1_rgb_no_branch) cv2.waitKey(0) cv2.destroyAllWindows()