def main(): src_path = "../chars/src_dan_svg_simple.png" tag_path = "../chars/tag_dan_svg_simple.png" src_img = cv2.imread(src_path, 0) tag_img = cv2.imread(tag_path, 0) # rgt to grayscale _, src_img = cv2.threshold(src_img, 127, 255, cv2.THRESH_BINARY) _, tag_img = cv2.threshold(tag_img, 127, 255, cv2.THRESH_BINARY) src_img, tag_img = resizeImages(src_img, tag_img) print(src_img.shape) print(tag_img.shape) # cv2.imwrite('../chars/src_dan_svg_simple_resized.png', src_img) # cv2.imwrite('../chars/tag_dan_svg_simple_resized.png', tag_img) cv2.imshow("source", src_img) cv2.imshow("target", tag_img) cv2.waitKey(0) cv2.destroyAllWindows()
def main(): src_path = "../characters/src_dan_processed.png" tag_path = "../characters/tag_dan_processed.png" src_img = cv2.imread(src_path, 0) tag_img = cv2.imread(tag_path, 0) _, src_img = cv2.threshold(src_img, 127, 255, cv2.THRESH_BINARY) _, tag_img = cv2.threshold(tag_img, 127, 255, cv2.THRESH_BINARY) cv2.imshow("source", src_img) cv2.imshow("target", tag_img) src_img_box = addMinBoundingBox(src_img) tag_img_box = addMinBoundingBox(tag_img) cv2.imshow("source box", src_img_box) cv2.imshow("target box", tag_img_box) src_img, tag_img = resizeImages(src_img, tag_img) print(src_img.shape) print(tag_img.shape) cv2.imshow("resize source", src_img) cv2.imshow("resize target", tag_img) cv2.waitKey(0) cv2.destroyAllWindows()
def main(): # src_path = "../chars/src_dan_svg_simple_resized.png" # tag_path = "../chars/tag_dan_svg_simple_resized.png" src_path = "../strokes/src_strokes4.png" tag_path = "../strokes/tag_strokes4.png" # src_path = "../strokes/src_strokes1.png" # tag_path = "../strokes/tag_strokes1.png" src_img = cv2.imread(src_path, 0) tag_img = cv2.imread(tag_path, 0) ret, src_img = cv2.threshold(src_img, 127, 255, cv2.THRESH_BINARY) ret, tag_img = cv2.threshold(tag_img, 127, 255, cv2.THRESH_BINARY) # resize src_img, tag_img = resizeImages(src_img, tag_img) # Threshold ret, src_img = cv2.threshold(src_img, 127, 255, cv2.THRESH_BINARY) ret, tag_img = cv2.threshold(tag_img, 127, 255, cv2.THRESH_BINARY) # cv2.imwrite("src_resize.png", src_img) # cv2.imwrite("tag_resize.png", tag_img) # Cover Images coverage_img = coverTwoImages(src_img, tag_img) cr = calculateCoverageRate(src_img, tag_img) print("No shifting cr: %f" % cr) # coverage_img = addIntersectedFig(coverage_img) # coverage_img = addSquaredFig(coverage_img) # Shift images with max CR new_tag_img = shiftImageWithMaxCR(src_img, tag_img) # Cover images coverage_img1 = coverTwoImages(src_img, new_tag_img) cr = calculateCoverageRate(src_img, new_tag_img) print("Shifting cr: %f" % cr) coverage_img_ = addIntersectedFig(coverage_img) coverage_img1_ = addIntersectedFig(coverage_img1) cv2.imshow("coverage img", coverage_img_) cv2.imshow("new coverage img", coverage_img1_) cv2.waitKey(0) cv2.destroyAllWindows()
def main(): temp_path = "/Users/liupeng/Documents/PythonProjects/templates/templates/ben/char/ben.png" targ_path = "/Users/liupeng/Documents/PythonProjects/templates/templates_comparison/ben/char/ben.png" temp_img = cv2.imread(temp_path, 0) targ_img = cv2.imread(targ_path, 0) _, temp_img = cv2.threshold(temp_img, 127, 255, cv2.THRESH_BINARY) _, targ_img = cv2.threshold(targ_img, 127, 255, cv2.THRESH_BINARY) # resize two images of template and target. temp_img, targ_img = resizeImages(temp_img, targ_img) temp_img = np.array(temp_img, dtype=np.uint8) targ_img = np.array(targ_img, dtype=np.uint8) # bounding box of template and target images temp_x, temp_y, temp_w, temp_h = getSingleMaxBoundingBoxOfImage(temp_img) targ_x, targ_y, targ_w, targ_h = getSingleMaxBoundingBoxOfImage(targ_img) temp_ct_x = temp_x + int(temp_w / 2.) temp_ct_y = temp_y + int(temp_h / 2.) targ_ct_x = targ_x + int(targ_w / 2.) targ_ct_y = targ_y + int(targ_h / 2.) # new square width square_width = max(temp_w, temp_h, targ_w, targ_h) # using new square to crop all effective area in template and target images # template image if temp_ct_x - int(square_width / 2.) <= 0: temp_x = 0 else: temp_x = temp_ct_x - int(square_width / 2.) if temp_ct_y - int(square_width / 2, ) <= 0: temp_y = 0 else: temp_y = temp_ct_y - int(square_width / 2, ) if temp_ct_x + int(square_width / 2.) >= temp_img.shape[1]: temp_w = temp_img.shape[1] - temp_x else: temp_w = square_width if temp_ct_y + int(square_width / 2.) >= temp_img.shape[0]: temp_h = temp_img.shape[0] - temp_y else: temp_h = square_width # target image if targ_ct_x - int(square_width / 2.) <= 0: targ_x = 0 else: targ_x = targ_ct_x - int(square_width / 2.) if targ_ct_x - int(square_width / 2, ) <= 0: targ_y = 0 else: targ_y = targ_ct_x - int(square_width / 2, ) if targ_ct_x + int(square_width / 2.) >= targ_img.shape[1]: targ_w = targ_img.shape[1] - targ_x else: targ_w = square_width if targ_ct_x + int(square_width / 2.) >= targ_img.shape[0]: targ_h = targ_img.shape[0] - targ_y else: targ_h = square_width # crop effective areas of the template and target images temp_reg = temp_img[temp_y:temp_y + temp_h, temp_x:temp_x + temp_w] targ_reg = targ_img[targ_y:targ_y + targ_h, targ_x:targ_x + targ_w] shape_similarity = calculateShapeSimilarity(temp_reg, targ_reg) simi = calculateShapeSimilarity(temp_reg, temp_reg) print("Shape similarity: %f" % shape_similarity) print("Same image similarity: %f" % simi) cv2.imshow("jianti_temp", temp_reg) cv2.imshow("targ", targ_reg) cv2.waitKey(0) cv2.destroyAllWindows()
def aesthetic_evaluation(template_path, target_path): if template_path == "" or target_path == "": print("template path and target path are null!") return """ Image pre-processing of template and target. """ # 1. find template and target images and strokes template_char_path = template_path + "/char/" template_strokes_path = template_path + "/strokes/" target_char_path = target_path + "/char/" target_strokes_path = target_path + "/strokes/" # 2. load image from these image paths template_char_img = cv2.imread(template_char_path + "ben.png") target_char_img = cv2.imread(target_char_path + "ben.png") # 3. rgb imag to grayscale image template_char_img_gray = cv2.cvtColor(template_char_img, cv2.COLOR_RGB2GRAY) target_char_img_gray = cv2.cvtColor(target_char_img, cv2.COLOR_RGB2GRAY) # 4. Resize template and target images template_char_img_gray, target_char_img_gray = resizeImages(template_char_img_gray, target_char_img_gray) template_char_img_gray = np.array(template_char_img_gray, dtype=np.uint8) target_char_img_gray = np.array(target_char_img_gray, dtype=np.uint8) print("Resized shape:",template_char_img_gray.shape, template_char_img_gray.shape) # 5. Stroke extraction and save to fixed directory # 6. Load strokes of template and target images; template_strokes_img = [] target_strokes_img = [] for fl in os.listdir(template_strokes_path): if ".png" in fl: stroke = cv2.imread(template_strokes_path + "/" + fl) if len(stroke.shape) == 3: stroke = cv2.cvtColor(stroke, cv2.COLOR_RGB2GRAY) _, stroke = cv2.threshold(stroke, 127, 255, cv2.THRESH_BINARY) template_strokes_img.append(stroke) for fl in os.listdir(target_strokes_path): if ".png" in fl: stroke = cv2.imread(target_strokes_path + "/" + fl) if len(stroke.shape) == 3: stroke = cv2.cvtColor(stroke, cv2.COLOR_RGB2GRAY) _, stroke = cv2.threshold(stroke, 127, 255, cv2.THRESH_BINARY) target_strokes_img.append(stroke) print("template stroke num: %d , and target strokes num: %d" % (len(template_strokes_img), len(target_strokes_img))) # 2. Aesthetic evaluation of template and target. """ 1. Global features """ # 1.1 max CR # shift the target image to get maximal CR target_char_img_gray = shiftImageWithMaxCR(template_char_img_gray, target_char_img_gray) max_cr = calculateCoverageRate(template_char_img_gray, target_char_img_gray) print("max cr: %0.3f" % max_cr) # 1.2 Center of gravity # template_cog = getCenterOfGravity(template_char_img_gray) # target_cog = getCenterOfGravity(target_char_img_gray) # # print("center of gravity: ", template_cog, target_cog) # 1.3 convex hull area # template_convex_hull = getConvexHullOfImage(template_char_img_gray) # target_convex_hull = getConvexHullOfImage(target_char_img_gray) # # template_convex_area = calculateConvexHullArea(template_convex_hull) # target_convex_area = calculateConvexHullArea(target_convex_hull) # # print("convex area:", template_convex_area, target_convex_area) # # template_valid_pixel_area = 0 # target_valid_pixel_area = 0 # # calculate the valid pixel of template and target images. # for y in range(template_char_img_gray.shape[0]): # for x in range(template_char_img_gray.shape[1]): # if template_char_img_gray[y][x] == 0.0: # template_valid_pixel_area += 1 # # for y in range(target_char_img_gray.shape[0]): # for x in range(target_char_img_gray.shape[1]): # if target_char_img_gray[y][x] == 0.0: # target_valid_pixel_area += 1 # print("valid pixel area:", template_valid_pixel_area, target_valid_pixel_area) # # template_convex_size_ratio = template_convex_area / (template_char_img_gray.shape[0] * template_char_img_gray.shape[1]) # target_convex_size_ratio = target_convex_area / (target_char_img_gray.shape[0] * target_char_img_gray.shape[1]) # # template_valid_convex_ratio = template_valid_pixel_area / template_convex_area # target_valid_convex_ratio = target_valid_pixel_area / target_convex_area # # print("convex size ratio:", template_convex_size_ratio, target_convex_size_ratio) # print("valid convex ratio:", template_valid_convex_ratio, target_valid_convex_ratio) # 1.4. histograms of template and target images at 0, 45, 90, 135 degree. # template_char_img_gray = np.array(template_char_img_gray, np.uint8) # target_char_img_gray = np.array(target_char_img_gray, np.uint8) # # # 0 degree # template_x_axis = np.zeros((template_char_img_gray.shape[1], 1)) # template_y_axis = np.zeros((template_char_img_gray.shape[0], 1)) # target_x_axis = np.zeros((target_char_img_gray.shape[1], 1)) # target_y_axis = np.zeros((target_char_img_gray.shape[0], 1)) # # for y in range(template_char_img_gray.shape[0]): # for x in range(template_char_img_gray.shape[1]): # if template_char_img_gray[y][x] == 0.0: # template_x_axis[x] += 1 # template_y_axis[y] += 1 # for y in range(target_char_img_gray.shape[0]): # for x in range(target_char_img_gray.shape[1]): # if target_char_img_gray[y][x] == 0.0: # target_x_axis[x] += 1 # target_y_axis[y] += 1 # # # mean # template_x_axis_mean = np.mean(template_x_axis) # template_y_axis_mean = np.mean(template_y_axis) # target_x_axis_mean = np.mean(target_x_axis) # target_y_axis_mean = np.mean(target_y_axis) # # template_x_axis_median = np.median(template_x_axis) # template_y_axis_median = np.median(template_y_axis) # target_x_axis_median = np.median(target_x_axis) # target_y_axis_median = np.median(target_y_axis) # # print("0 degree mean:", template_x_axis_mean, template_y_axis_mean, target_x_axis_mean, target_y_axis_mean) # print("0 degree median:", template_x_axis_median, template_y_axis_median, target_x_axis_median, target_y_axis_median) # # # display the template and target x-axis and y-axis # labels = list(range(template_char_img_gray.shape[0])) # # plt.subplot(2, 2, 1) # plt.plot(labels, template_x_axis) # plt.title("template x axis") # plt.ylabel("Number of valid pixels") # # plt.subplot(2, 2, 2) # plt.plot(labels, template_y_axis) # plt.title("template y axis") # plt.ylabel("Number of valid pixels") # # plt.subplot(2, 2, 3) # plt.plot(labels, target_x_axis) # plt.title("target x axis") # plt.ylabel("Number of vaild pixels") # # plt.subplot(2, 2, 4) # plt.plot(labels, target_y_axis) # plt.title("target y axis") # plt.ylabel("Number of vaild pixels") # # plt.show() # # # 45 degree # template_char_img_gray_45 = rotateImage(template_char_img_gray, 45) # target_char_img_gray_45 = rotateImage(target_char_img_gray, 45) # # template_x_axis = np.zeros((template_char_img_gray_45.shape[0], 1)) # template_y_axis = np.zeros((template_char_img_gray_45.shape[1], 1)) # target_x_axis = np.zeros((target_char_img_gray_45.shape[0], 1)) # target_y_axis = np.zeros((target_char_img_gray_45.shape[1], 1)) # # for y in range(template_char_img_gray_45.shape[0]): # for x in range(template_char_img_gray_45.shape[1]): # if template_char_img_gray_45[y][x] == 0.0: # template_x_axis[x] += 1 # template_y_axis[y] += 1 # for y in range(target_char_img_gray_45.shape[0]): # for x in range(target_char_img_gray_45.shape[1]): # if target_char_img_gray_45[y][x] == 0.0: # target_x_axis[x] += 1 # target_y_axis[y] += 1 # # # mean # template_x_axis_mean = np.mean(template_x_axis) # template_y_axis_mean = np.mean(template_y_axis) # target_x_axis_mean = np.mean(target_x_axis) # target_y_axis_mean = np.mean(target_y_axis) # # template_x_axis_median = np.median(template_x_axis) # template_y_axis_median = np.median(template_y_axis) # target_x_axis_median = np.median(target_x_axis) # target_y_axis_median = np.median(target_y_axis) # # print("45 degree mean:", template_x_axis_mean, template_y_axis_mean, target_x_axis_mean, target_y_axis_mean) # print("45 degree median:", template_x_axis_median, template_y_axis_median, target_x_axis_median, # target_y_axis_median) # # # display the template and target x-axis and y-axis # labels = list(range(template_char_img_gray.shape[0])) # # plt.subplot(2, 2, 1) # plt.plot(labels, template_x_axis) # plt.title("template x axis") # plt.ylabel("Number of valid pixels") # # plt.subplot(2, 2, 2) # plt.plot(labels, template_y_axis) # plt.title("template y axis") # plt.ylabel("Number of valid pixels") # # plt.subplot(2, 2, 3) # plt.plot(labels, target_x_axis) # plt.title("target x axis") # plt.ylabel("Number of vaild pixels") # # plt.subplot(2, 2, 4) # plt.plot(labels, target_y_axis) # plt.title("target y axis") # plt.ylabel("Number of vaild pixels") # # plt.show() # # # # 90 degree # template_char_img_gray_90 = rotateImage(template_char_img_gray, 90) # target_char_img_gray_90 = rotateImage(target_char_img_gray, 90) # # template_x_axis = np.zeros((template_char_img_gray_90.shape[0], 1)) # template_y_axis = np.zeros((template_char_img_gray_90.shape[1], 1)) # target_x_axis = np.zeros((target_char_img_gray_90.shape[0], 1)) # target_y_axis = np.zeros((target_char_img_gray_90.shape[1], 1)) # # for y in range(template_char_img_gray_90.shape[0]): # for x in range(template_char_img_gray_90.shape[1]): # if template_char_img_gray_90[y][x] == 0.0: # template_x_axis[x] += 1 # template_y_axis[y] += 1 # for y in range(target_char_img_gray_90.shape[0]): # for x in range(target_char_img_gray_90.shape[1]): # if target_char_img_gray_90[y][x] == 0.0: # target_x_axis[x] += 1 # target_y_axis[y] += 1 # # # mean # template_x_axis_mean = np.mean(template_x_axis) # template_y_axis_mean = np.mean(template_y_axis) # target_x_axis_mean = np.mean(target_x_axis) # target_y_axis_mean = np.mean(target_y_axis) # # template_x_axis_median = np.median(template_x_axis) # template_y_axis_median = np.median(template_y_axis) # target_x_axis_median = np.median(target_x_axis) # target_y_axis_median = np.median(target_y_axis) # # print("90 degree mean:", template_x_axis_mean, template_y_axis_mean, target_x_axis_mean, target_y_axis_mean) # print("90 degree median:", template_x_axis_median, template_y_axis_median, target_x_axis_median, # target_y_axis_median) # # # display the template and target x-axis and y-axis # labels = list(range(template_char_img_gray.shape[0])) # # plt.subplot(2, 2, 1) # plt.plot(labels, template_x_axis) # plt.title("template x axis") # plt.ylabel("Number of valid pixels") # # plt.subplot(2, 2, 2) # plt.plot(labels, template_y_axis) # plt.title("template y axis") # plt.ylabel("Number of valid pixels") # # plt.subplot(2, 2, 3) # plt.plot(labels, target_x_axis) # plt.title("target x axis") # plt.ylabel("Number of vaild pixels") # # plt.subplot(2, 2, 4) # plt.plot(labels, target_y_axis) # plt.title("target y axis") # plt.ylabel("Number of vaild pixels") # # plt.show() # # # 135 degree # template_char_img_gray_135 = rotateImage(template_char_img_gray, 135) # target_char_img_gray_135 = rotateImage(target_char_img_gray, 135) # # template_x_axis = np.zeros((template_char_img_gray_135.shape[0], 1)) # template_y_axis = np.zeros((template_char_img_gray_135.shape[1], 1)) # target_x_axis = np.zeros((target_char_img_gray_135.shape[0], 1)) # target_y_axis = np.zeros((target_char_img_gray_135.shape[1], 1)) # # for y in range(template_char_img_gray_135.shape[0]): # for x in range(template_char_img_gray_135.shape[1]): # if template_char_img_gray_135[y][x] == 0.0: # template_x_axis[x] += 1 # template_y_axis[y] += 1 # for y in range(target_char_img_gray_135.shape[0]): # for x in range(target_char_img_gray_135.shape[1]): # if target_char_img_gray_135[y][x] == 0.0: # target_x_axis[x] += 1 # target_y_axis[y] += 1 # # # mean # template_x_axis_mean = np.mean(template_x_axis) # template_y_axis_mean = np.mean(template_y_axis) # target_x_axis_mean = np.mean(target_x_axis) # target_y_axis_mean = np.mean(target_y_axis) # # template_x_axis_median = np.median(template_x_axis) # template_y_axis_median = np.median(template_y_axis) # target_x_axis_median = np.median(target_x_axis) # target_y_axis_median = np.median(target_y_axis) # # print("135 degree mean:", template_x_axis_mean, template_y_axis_mean, target_x_axis_mean, target_y_axis_mean) # print("135 degree median:", template_x_axis_median, template_y_axis_median, target_x_axis_median, target_y_axis_median) # # # display the template and target x-axis and y-axis # labels = list(range(template_char_img_gray.shape[0])) # # plt.subplot(2, 2, 1) # plt.plot(labels, template_x_axis) # plt.title("template x axis") # plt.ylabel("Number of valid pixels") # # plt.subplot(2, 2, 2) # plt.plot(labels, template_y_axis) # plt.title("template y axis") # plt.ylabel("Number of valid pixels") # # plt.subplot(2, 2, 3) # plt.plot(labels, target_x_axis) # plt.title("target x axis") # plt.ylabel("Number of vaild pixels") # # plt.subplot(2, 2, 4) # plt.plot(labels, target_y_axis) # plt.title("target y axis") # plt.ylabel("Number of vaild pixels") # # plt.show() # 1.5. Statistics on Jiu-gong grid 1 | 2 | 3 # 4 | 5 | 6 # 7 | 8 | 9 # mesh_shape = 3 # # template_grids = separateImageWithElesticMesh(template_char_img_gray, mesh_shape) # target_grids = separateImageWithElesticMesh(target_char_img_gray, mesh_shape) # # # statistics of valid pixels in grids # template_grids_statis = [] # target_grids_statis = [] # for i in range(len(template_grids)): # grid = template_grids[i] # grid = 255 - grid # num = np.sum(grid) / 255. # template_grids_statis.append(num) # print(template_grids_statis) # # for i in range(len(target_grids)): # grid = target_grids[i] # grid = 255 - grid # num = np.sum(grid) / 255. # target_grids_statis.append(num) # print(target_grids_statis) # 1.6. Aspect ratio # _, _, temp_mini_box_w, temp_mini_box_h = getSingleMaxBoundingBoxOfImage(template_char_img_gray) # _, _, targ_mini_box_w, targ_mini_box_h = getSingleMaxBoundingBoxOfImage(target_char_img_gray) # # print(temp_mini_box_w, temp_mini_box_h, (temp_mini_box_w / temp_mini_box_h * 1.)) # print(targ_mini_box_w, targ_mini_box_h, (targ_mini_box_w / targ_mini_box_h * 1.)) # 1.7. elastic mesh effective statistics # mesh_shape = 9 # # template_mesh_grids = separateImageWithElesticMesh(template_char_img_gray, mesh_shape) # target_mesh_grids = separateImageWithElesticMesh(target_char_img_gray, mesh_shape) # print(len(template_mesh_grids)) # print(len(target_mesh_grids)) # # # statistics effective area in grid of effective area of template and target images. # template_mesh_effective_area = [] # target_mesh_effective_area = [] # # template # for i in range(len(template_mesh_grids)): # grid = template_mesh_grids[i] # grid of template # grid = 255 - grid # inverse grid # num = np.sum(grid) / 255. # sum the effective area pixels of template # template_mesh_effective_area.append(num) # print(template_mesh_effective_area) # # # target # for i in range(len(target_mesh_grids)): # grid = target_mesh_grids[i] # grid = 255 - grid # num = np.sum(grid) / 255. # target_mesh_effective_area.append(num) # print(target_mesh_effective_area) """ 2. Radical features """ # 2.1 Radicals layout # 2.2 Effective area of radical. """ 3. Stroke features """ if len(template_strokes_img) != len(target_strokes_img): print("Strokes number is different!!!") else: print("Stroke number is same!") # 3.1 Coverage rate # strokes_cr = [] # # for i in range(len(template_strokes_img)): # temp_stroke = template_strokes_img[i] # targ_stroke = target_strokes_img[i] # targ_stroke = shiftImageWithMaxCR(temp_stroke, targ_stroke) # # cr_ = calculateCoverageRate(temp_stroke, targ_stroke) # # strokes_cr.append(cr_) # print(strokes_cr) # 3.2 Genter of gravity # template_strokes_cog = [] # target_strokes_cog = [] # # for i in range(len(template_strokes_img)): # stroke = template_strokes_img[i] # stroke_cog = getCenterOfGravity(stroke) # template_strokes_cog.append(stroke_cog) # # for i in range(len(target_strokes_img)): # stroke = target_strokes_img[i] # stroke_cog = getCenterOfGravity(stroke) # target_strokes_cog.append(stroke_cog) # # print(template_strokes_cog) # print(target_strokes_cog) # 3.3 Convex hull area # template_strokes_convex = [] # target_strokes_convex = [] # # for i in range(len(template_strokes_img)): # stroke = template_strokes_img[i] # convex_ = getConvexHullOfImage(stroke) # template_strokes_convex.append(convex_) # # for i in range(len(target_strokes_img)): # stroke = target_strokes_img[i] # convex_ = getConvexHullOfImage(stroke) # target_strokes_convex.append(convex_) # # # calculate the convex hull area of template and target # template_strokes_convex_area = [] # target_strokes_convex_area = [] # for convex_ in template_strokes_convex: # area = calculateConvexHullArea(convex_) # template_strokes_convex_area.append(area) # for convex_ in target_strokes_convex: # area = calculateConvexHullArea(convex_) # target_strokes_convex_area.append(area) # print(template_strokes_convex_area) # print(target_strokes_convex_area) # 3.4 Begin, end and middle segmentation of stroke # 3.5 Angle of stroke: heng and shu # for i in range(len(template_strokes_img)): # temp_stroke = template_strokes_img[i] # targ_stroke = target_strokes_img[i] # # # detect stroke is heng or shu # _, _, temp_w, temp_h = getSingleMaxBoundingBoxOfImage(temp_stroke) # _, _, targ_w, targ_h = getSingleMaxBoundingBoxOfImage(targ_stroke) # # if temp_w > temp_h and temp_h / temp_w * 1. <= 0.5: # print("heng") # # elif temp_h > temp_w and temp_w / temp_h * 1. <= 0.2: # print("shu") cv2.waitKey(0) cv2.destroyAllWindows()
def main(): # src_path = "../strokes/src_strokes4.png" src_path = "../chars/src_dan_svg_simple_resized.png" tag_path = "../strokes/tag_strokes4.png" src_img = cv2.imread(src_path, 0) tag_img = cv2.imread(tag_path, 0) ret, src_img = cv2.threshold(src_img, 127, 255, cv2.THRESH_BINARY) ret, tag_img = cv2.threshold(tag_img, 127, 255, cv2.THRESH_BINARY) # resize src_img, tag_img = resizeImages(src_img, tag_img) ret, src_img = cv2.threshold(src_img, 127, 255, cv2.THRESH_BINARY) ret, tag_img = cv2.threshold(tag_img, 127, 255, cv2.THRESH_BINARY) # obtain the skeleton of strokes src_img_ = src_img != 255 tag_img_ = tag_img != 255 src_skel = skeletonize(src_img_) tag_skel = skeletonize(tag_img_) src_skel = (1 - src_skel) * 255 tag_skel = (1 - tag_skel) * 255 src_skel = np.array(src_skel, dtype=np.uint8) tag_skel = np.array(tag_skel, dtype=np.uint8) src_skel_rgb = cv2.cvtColor(src_skel, cv2.COLOR_GRAY2BGR) tag_skel_rgb = cv2.cvtColor(tag_skel, cv2.COLOR_GRAY2BGR) src_end_points = getEndPointsOfSkeletonLine(src_skel) tag_end_points = getEndPointsOfSkeletonLine(tag_skel) for (x, y) in src_end_points: src_skel_rgb[y][x] = (0, 0, 255) for (x, y) in tag_end_points: tag_skel_rgb[y][x] = (0, 0, 255) print("src end points len: %d" % len(src_end_points)) print("tag end points len: %d" % len(tag_end_points)) if len(src_end_points) > 2: print("src skeleton line has branch") if len(tag_end_points) > 2: print("tag skeleton line has branch") src_cross_points = getCrossPointsOfSkeletonLine(src_skel) tag_cross_points = getCrossPointsOfSkeletonLine(tag_skel) for (x, y) in src_cross_points: src_skel_rgb[y][x] = (255, 0, 0) for (x, y) in tag_cross_points: tag_skel_rgb[y][x] = (255, 0, 0) print("src cross len: %d" % len(src_cross_points)) print("tag cross len: %d" % len(tag_cross_points)) if len(src_cross_points) > 0: # exist branches src_skel = removeBranchOfSkeletonLine(src_skel, src_end_points, src_cross_points, ) if len(tag_cross_points) > 0: # exist branches tag_skel = removeBranchOfSkeletonLine(tag_skel, tag_end_points, tag_cross_points, ) # src_skel_no_branch = removeBranchOfSkeletonLine(src_skel, src_end_points, src_cross_points) # tag_skel_no_btranch = removeBranchOfSkeletonLine(tag_skel, tag_end_points, tag_cross_points) cv2.imshow("coverage img", src_img) cv2.imshow("new coverage img", tag_img) cv2.imshow("src rgb", src_skel_rgb) cv2.imshow("tag rgb", tag_skel_rgb) cv2.imshow("src skeleton img", src_skel) cv2.imshow("tag skeleton img", tag_skel) # cv2.imshow("src skeleton img no branch", src_skel_no_branch) # cv2.imshow("tag skeleton img no branch", tag_skel_no_branch) cv2.waitKey(0) cv2.destroyAllWindows()