From e7b20db4c0f09ac03a47f3626412402d6acb5c9f Mon Sep 17 00:00:00 2001 From: Peter_Liu Date: Tue, 8 May 2018 22:45:03 +0800 Subject: [PATCH] fix bug --- test.py | 4 +- test_gui.py | 74 +++-- test_radical_stroke_extract.py | 497 ++++++++++++++++++++------------- 3 files changed, 354 insertions(+), 221 deletions(-) diff --git a/test.py b/test.py index fe98801..14a555c 100644 --- a/test.py +++ b/test.py @@ -5,7 +5,8 @@ from utils.Functions import getConnectedComponents, getContourOfImage, getSkeletonOfImage, removeBreakPointsOfContour, \ removeBranchOfSkeletonLine, removeBranchOfSkeleton, getEndPointsOfSkeletonLine, \ getCrossPointsOfSkeletonLine, sortPointsOnContourOfImage, min_distance_point2pointlist, \ - getNumberOfValidPixels, segmentContourBasedOnCornerPoints, merge_corner_lines_to_point + getNumberOfValidPixels, segmentContourBasedOnCornerPoints, merge_corner_lines_to_point, \ + fitCurve, draw_cubic_bezier # 1133壬 2252支 0631叟 path = "0631叟.jpg" @@ -23,6 +24,7 @@ contour_points_sorted = sortPointsOnContourOfImage(contour) print("contour points num:%d" % len(contour_points_sorted)) + img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) _, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) print(img.shape) diff --git a/test_gui.py b/test_gui.py index 6ab2a29..74f284a 100644 --- a/test_gui.py +++ b/test_gui.py @@ -255,30 +255,64 @@ # # print(isSubList(a, b)) -import cv2 -import numpy as np -from skimage import feature - -from utils.Functions import createBlankGrayscaleImage, createBlankRGBImage - +# import cv2 +# import numpy as np +# from skimage import feature +# +# from utils.Functions import createBlankGrayscaleImage, createBlankRGBImage +# +# +# # 1133壬 2252支 0631叟 +# path = "0631叟.jpg" +# +# img = cv2.imread(path, 0) +# _, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) +# img_rbg = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) +# +# edges = feature.canny(img) +# edges1 = feature.canny(img, sigma=3) +# +# print(edges) +# +# contour_gray = createBlankGrayscaleImage(img) +# contour_rgb = createBlankRGBImage(img) +# +# cv2.imshow("edge", edges) +# cv2.imshow("edge1", edges1) +# +# cv2.waitKey(0) +# cv2.destroyAllWindows() -# 1133壬 2252支 0631叟 -path = "0631叟.jpg" +import numpy as np -img = cv2.imread(path, 0) -_, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) -img_rbg = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) -edges = feature.canny(img) -edges1 = feature.canny(img, sigma=3) +def medfilt (x, k): + """Apply a length-k median filter to a 1D array x. + Boundaries are extended by repeating endpoints. + """ + assert k % 2 == 1, "Median filter length must be odd." + assert x.ndim == 1, "Input must be one-dimensional." + k2 = (k - 1) // 2 + y = np.zeros ((len (x), k), dtype=x.dtype) + y[:,k2] = x + for i in range (k2): + j = k2 - i + y[j:,i] = x[:-j] + y[:j,i] = x[0] + y[:-j,-(i+1)] = x[j:] + y[-j:,-(i+1)] = x[-1] + return np.median (y, axis=1) -print(edges) -contour_gray = createBlankGrayscaleImage(img) -contour_rgb = createBlankRGBImage(img) +def test (): + import pylab as p + x = np.linspace (0, 1, 101) + x[3::10] = 1.5 + p.plot (x) + p.plot (medfilt (x,3)) + p.plot (medfilt (x,5)) + p.show () -cv2.imshow("edge", edges) -cv2.imshow("edge1", edges1) -cv2.waitKey(0) -cv2.destroyAllWindows() \ No newline at end of file +if __name__ == '__main__': + test () \ No newline at end of file diff --git a/test_radical_stroke_extract.py b/test_radical_stroke_extract.py index 0e9dbb2..1bb94cc 100644 --- a/test_radical_stroke_extract.py +++ b/test_radical_stroke_extract.py @@ -29,7 +29,7 @@ radicals = np.array(radicals, dtype=np.uint8) # # contour -contour = getContourOfImage(radicals, minVal=20, maxVal=240) +# contour = getContourOfImage(radicals, minVal=20, maxVal=240) contour = np.array(contour, dtype=np.uint8) contour_seg = getConnectedComponents(contour) @@ -82,10 +82,10 @@ def getBreakPointsFromContour(contour): cross_points = getCrossPointsOfSkeletonLine(skeleton) skeleton_rgb = cv2.cvtColor(skeleton, cv2.COLOR_GRAY2RGB) # -# 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 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) # corner area detect img_corner = np.float32(img.copy()) @@ -158,204 +158,314 @@ def merge_corner_lines_to_point(corner_line_points, contour_sorted): print("corner points num: %d" % len(corner_points)) # cluster the corner points -dist_threshold = 40 +dist_threshold = 30 corner_points_cluster = [] used_index = [] -for i in range(len(corner_points)): - if i in used_index: - continue - pt1 = corner_points[i] - cluster = [pt1] - used_index.append(i) +for i in range(len(cross_points)): + cross_pt = cross_points[i] + cluster = [] for j in range(len(corner_points)): - if i == j or j in used_index: + if j in used_index: continue - pt2 = corner_points[j] - dist = math.sqrt((pt1[0]-pt2[0])**2 + (pt1[1]-pt2[1])**2) - if dist <= dist_threshold: - cluster.append(pt2) + corner_pt = corner_points[j] + dist = math.sqrt((cross_pt[0]-corner_pt[0])**2 + (cross_pt[1]-corner_pt[1])**2) + if dist < dist_threshold: + cluster.append(corner_pt) used_index.append(j) + if cluster: + corner_points_cluster.append(cluster) +print("corner cluster num:%d" % len(corner_points_cluster)) +print(corner_points_cluster) + +# detect corner points type: two point, four point (rectangle or diamond) + + +crop_lines = [] +for i in range(len(corner_points_cluster)): + corner_clt = corner_points_cluster[i] + if len(corner_clt) == 2: + print(" tow points") + crop_lines.append((corner_clt)) + elif len(corner_clt) == 4: + # rectangle or diamond (vertical/horizon or pie/na) + min_offset = 1000 + for i in range(len(corner_clt)): + pt1 = corner_clt[i] + if i == len(corner_clt) - 1: + pt2 = corner_clt[0] + else: + pt2 = corner_clt[i+1] + offset = abs(pt1[0]-pt2[0]) + if offset <= min_offset: + min_offset = offset + if min_offset <= 10: + print("rectangle") + if abs(corner_clt[0][0]-corner_clt[1][0]) <= 10: + crop_lines.append((corner_clt[0], corner_clt[1])) + crop_lines.append((corner_clt[2], corner_clt[3])) + if abs(corner_clt[0][1] - corner_clt[2][1]) <= 10: + crop_lines.append((corner_clt[0], corner_clt[2])) + crop_lines.append((corner_clt[1], corner_clt[3])) + else: + crop_lines.append((corner_clt[0], corner_clt[3])) + crop_lines.append((corner_clt[1], corner_clt[2])) + elif abs(corner_clt[0][0] - corner_clt[2][0]) <= 10: + crop_lines.append((corner_clt[0], corner_clt[2])) + crop_lines.append((corner_clt[1], corner_clt[3])) + + if abs(corner_clt[0][1] - corner_clt[1][1]) <= 10: + crop_lines.append((corner_clt[0], corner_clt[1])) + crop_lines.append((corner_clt[2], corner_clt[3])) + else: + crop_lines.append((corner_clt[0], corner_clt[3])) + crop_lines.append((corner_clt[1], corner_clt[2])) + + else: + print("diamond") + """ + P3 + P0 P2 + P1 + """ + P0 = P1 = P2 = P3 = None + min_x = min_y = 10000000 + max_x = max_y = 0 + for pt in corner_clt: + if pt[0] > max_x: + max_x = pt[0] + if pt[0] < min_x: + min_x = pt[0] + if pt[1] > max_y: + max_y = pt[1] + if pt[1] < min_y: + min_y = pt1[1] + print("minx:%d miny:%d maxx:%d maxy:%d" % (min_x, min_y, max_x, max_y)) + + for pt in corner_clt: + if pt[0] == min_x: + P0 = pt + elif pt[0] == max_x: + P2 = pt + if pt[1] == min_y: + P3 = pt + elif pt[1] == max_y: + P1 = pt + crop_lines.append((P0, P1)) + crop_lines.append((P1, P2)) + crop_lines.append((P2, P3)) + crop_lines.append((P3, P0)) + +# display crop lines +for line in crop_lines: + cv2.line(contour_rgb, line[0], line[1], (0,255,0),1) + +crop_lines_points = [] + +# crop character +print("contor point num: %d" % len(contour_sorted)) +sub_contours = segmentContourBasedOnCornerPoints(contour_sorted, corner_points) +print("sub contours num: %d" % len(sub_contours)) - corner_points_cluster.append(cluster) - -print("clust num: %d" % len(corner_points_cluster)) +# separate single region to several region +contour_separate_region = cv2.cvtColor(contour_rgb, cv2.COLOR_RGB2GRAY) +_, contour_separate_region = cv2.threshold(contour_separate_region, 240, 255, cv2.THRESH_BINARY) -def isInCluster(point_pair, cluster): - if point_pair is None or cluster is None: - return False - label = False - for cl in cluster: - if point_pair[0] in cl and point_pair[1] in cl: - label = True - break - return label -# segment contour to sub-contour -print("contor point num: %d" % len(contour_sorted)) -sub_contours = segmentContourBasedOnCornerPoints(contour_sorted, corner_points) -print("sub contour num: %d" % len(sub_contours)) -# corner points correspondence -def isInOneSubContour(pt1, pt2, sub_contours): - if pt1 is None or pt2 is None or sub_contours is None: - return False - label = False - for sub in sub_contours: - if pt1 in sub and pt2 in sub: - label = True - break - return label - -# co-linear |y1-y2| <= 10 pixels and not in same sub-contour -co_linear_points = [] -parallel_points = [] -co_sub_contour = [] -for i in range(len(corner_points)): - pt1 = corner_points[i] - for j in range(len(corner_points)): - if i == j: - continue - pt2 = corner_points[j] - - # co-linear should be in same cluster and can be in same sub-contour - if abs(pt1[0] - pt2[0]) <= 10 and isInCluster([pt1, pt2], corner_points_cluster): - # co-linear - if [pt1, pt2] not in co_linear_points and [pt2, pt1] not in co_linear_points: - co_linear_points.append([pt1, pt2]) - - # parallel, should not be in same sub-contour, but should be in same cluster - if abs(pt1[1] - pt2[1]) <= 10 and not isInOneSubContour(pt1, pt2, sub_contours) and isInCluster([pt1, pt2], corner_points_cluster): - # parallel - if [pt1, pt2] not in parallel_points and [pt2, pt1] not in parallel_points: - if [pt1, pt2] not in co_linear_points and [pt2, pt1] not in co_linear_points: - parallel_points.append([pt1, pt2]) - - # co sub-contour, and do not repeat in previous lists. - if isInOneSubContour(pt1, pt2, sub_contours): - # co subcontour - if [pt1, pt2] not in co_sub_contour and [pt2, pt1] not in co_sub_contour: - if [pt1, pt2] not in co_linear_points and [pt2, pt1] not in co_linear_points: - if [pt1, pt2] not in parallel_points and [pt2, pt1] not in parallel_points and isInCluster([pt1, pt2], corner_points_cluster): - co_sub_contour.append([pt1, pt2]) - -print(co_linear_points) -print(parallel_points) -print(co_sub_contour) - -co_linear_points_cluster = [] -parallel_points_cluster = [] -co_sub_contour_cluster = [] - -def isSubList(l1, l2): - if l1 == [] and l2 != []: - return True - if l1 != [] and l2 == []: - return False - if l1 == [] and l2 == []: - return True - for item in l1: - if item not in l2: - return False - return True - -# cluster co_linear points pair based on the cluster points -used_index = [] -for i in range(len(co_linear_points)): - if i in used_index: - continue - used_index.append(i) - pair_cluster = [co_linear_points[i]] - cluster = None - for cl in corner_points_cluster: - if isSubList(co_linear_points[i], cl): - cluster = cl.copy() - - # j - if cluster is None: - print("cluster should not be None!") - for j in range(len(co_linear_points)): - if j == i or j in used_index: - continue - if isSubList(co_linear_points[j], cluster): - pair_cluster.append(co_linear_points[j]) - used_index.append(j) - co_linear_points_cluster.append(pair_cluster) +# cv2.imshow("radicals", radicals) +cv2.imshow("contour", contour) +# cv2.imshow("skeleton", skeleton) +cv2.imshow("skeleton rgb", skeleton_rgb) +# cv2.imshow("img_corner_area", img_corner_area) +cv2.imshow("contour_rgb", contour_rgb) +cv2.imshow("contour_separate_region", contour_separate_region) -print(co_linear_points_cluster) +cv2.waitKey(0) +cv2.destroyAllWindows() -# cluster the parallel points pair -used_index = [] -for i in range(len(parallel_points)): - if i in used_index: - continue - used_index.append(i) - pair_cluster = [parallel_points[i]] - cluster = None - for cl in corner_points_cluster: - if isSubList(parallel_points[i], cl): - cluster = cl.copy() - - # j - if cluster is None: - print("cluster should not be None!") - for j in range(len(parallel_points)): - if j == i or j in used_index: - continue - if isSubList(parallel_points[j], cluster): - pair_cluster.append(parallel_points[j]) - used_index.append(j) - parallel_points_cluster.append(pair_cluster) -print(parallel_points_cluster) -# cluster the co-subcontour points pair -used_index = [] -for i in range(len(co_sub_contour)): - if i in used_index: - continue - used_index.append(i) - pair_cluster = [co_sub_contour[i]] - cluster = None - for cl in corner_points_cluster: - if isSubList(co_sub_contour[i], cl): - cluster = cl.copy() - - # j - if cluster is None: - print("cluster should not be None!") - for j in range(len(co_sub_contour)): - if j == i or j in used_index: - continue - if isSubList(co_sub_contour[j], cluster): - pair_cluster.append(co_sub_contour[j]) - used_index.append(j) - co_sub_contour_cluster.append(pair_cluster) - -print(co_sub_contour_cluster) - -def findCoLinearSubContours(point_pair, sub_contours): - """ - Find two co-linear sub-contours based on the point pair. - :param point_pair: - :param sub_contours: - :return: - """ - if point_pair is None or sub_contours is None: - return - pt1 = point_pair[0]; pt2 = point_pair[1] - sub1 = None; sub2 = None - for sub in sub_contours: - if pt1 in sub and pt2 not in sub: - sub1 = sub.copy() - if pt2 in sub and pt1 not in sub: - sub2 = sub.copy() - return [sub1, sub2] - -# -print("sub-contours num : %d" % len(sub_contours)) +# exit() +# +# def isInCluster(point_pair, cluster): +# if point_pair is None or cluster is None: +# return False +# label = False +# for cl in cluster: +# if point_pair[0] in cl and point_pair[1] in cl: +# label = True +# break +# return label +# +# +# # segment contour to sub-contour +# print("contor point num: %d" % len(contour_sorted)) +# sub_contours = segmentContourBasedOnCornerPoints(contour_sorted, corner_points) +# print("sub contour num: %d" % len(sub_contours)) +# +# # corner points correspondence +# +# +# def isInOneSubContour(pt1, pt2, sub_contours): +# if pt1 is None or pt2 is None or sub_contours is None: +# return False +# label = False +# for sub in sub_contours: +# if pt1 in sub and pt2 in sub: +# label = True +# break +# return label +# +# # co-linear |y1-y2| <= 10 pixels and not in same sub-contour +# co_linear_points = [] +# parallel_points = [] +# co_sub_contour = [] +# for i in range(len(corner_points)): +# pt1 = corner_points[i] +# for j in range(len(corner_points)): +# if i == j: +# continue +# pt2 = corner_points[j] +# +# # co-linear should be in same cluster and can be in same sub-contour +# if abs(pt1[0] - pt2[0]) <= 10 and isInCluster([pt1, pt2], corner_points_cluster): +# # co-linear +# if [pt1, pt2] not in co_linear_points and [pt2, pt1] not in co_linear_points: +# co_linear_points.append([pt1, pt2]) +# +# # parallel, should not be in same sub-contour, but should be in same cluster +# if abs(pt1[1] - pt2[1]) <= 10 and not isInOneSubContour(pt1, pt2, sub_contours) and isInCluster([pt1, pt2], corner_points_cluster): +# # parallel +# if [pt1, pt2] not in parallel_points and [pt2, pt1] not in parallel_points: +# if [pt1, pt2] not in co_linear_points and [pt2, pt1] not in co_linear_points: +# parallel_points.append([pt1, pt2]) +# +# # co sub-contour, and do not repeat in previous lists. +# if isInOneSubContour(pt1, pt2, sub_contours): +# # co subcontour +# if [pt1, pt2] not in co_sub_contour and [pt2, pt1] not in co_sub_contour: +# if [pt1, pt2] not in co_linear_points and [pt2, pt1] not in co_linear_points: +# if [pt1, pt2] not in parallel_points and [pt2, pt1] not in parallel_points and isInCluster([pt1, pt2], corner_points_cluster): +# co_sub_contour.append([pt1, pt2]) +# +# print(co_linear_points) +# print(parallel_points) +# print(co_sub_contour) +# +# co_linear_points_cluster = [] +# parallel_points_cluster = [] +# co_sub_contour_cluster = [] +# +# def isSubList(l1, l2): +# if l1 == [] and l2 != []: +# return True +# if l1 != [] and l2 == []: +# return False +# if l1 == [] and l2 == []: +# return True +# for item in l1: +# if item not in l2: +# return False +# return True +# +# # cluster co_linear points pair based on the cluster points +# used_index = [] +# for i in range(len(co_linear_points)): +# if i in used_index: +# continue +# used_index.append(i) +# pair_cluster = [co_linear_points[i]] +# cluster = None +# for cl in corner_points_cluster: +# if isSubList(co_linear_points[i], cl): +# cluster = cl.copy() +# +# # j +# if cluster is None: +# print("cluster should not be None!") +# for j in range(len(co_linear_points)): +# if j == i or j in used_index: +# continue +# if isSubList(co_linear_points[j], cluster): +# pair_cluster.append(co_linear_points[j]) +# used_index.append(j) +# co_linear_points_cluster.append(pair_cluster) +# +# print(co_linear_points_cluster) +# +# # cluster the parallel points pair +# used_index = [] +# for i in range(len(parallel_points)): +# if i in used_index: +# continue +# used_index.append(i) +# pair_cluster = [parallel_points[i]] +# cluster = None +# for cl in corner_points_cluster: +# if isSubList(parallel_points[i], cl): +# cluster = cl.copy() +# +# # j +# if cluster is None: +# print("cluster should not be None!") +# for j in range(len(parallel_points)): +# if j == i or j in used_index: +# continue +# if isSubList(parallel_points[j], cluster): +# pair_cluster.append(parallel_points[j]) +# used_index.append(j) +# parallel_points_cluster.append(pair_cluster) +# +# print(parallel_points_cluster) +# +# # cluster the co-subcontour points pair +# used_index = [] +# for i in range(len(co_sub_contour)): +# if i in used_index: +# continue +# used_index.append(i) +# pair_cluster = [co_sub_contour[i]] +# cluster = None +# for cl in corner_points_cluster: +# if isSubList(co_sub_contour[i], cl): +# cluster = cl.copy() +# +# # j +# if cluster is None: +# print("cluster should not be None!") +# for j in range(len(co_sub_contour)): +# if j == i or j in used_index: +# continue +# if isSubList(co_sub_contour[j], cluster): +# pair_cluster.append(co_sub_contour[j]) +# used_index.append(j) +# co_sub_contour_cluster.append(pair_cluster) +# +# print(co_sub_contour_cluster) +# +# def findCoLinearSubContours(point_pair, sub_contours): +# """ +# Find two co-linear sub-contours based on the point pair. +# :param point_pair: +# :param sub_contours: +# :return: +# """ +# if point_pair is None or sub_contours is None: +# return +# pt1 = point_pair[0]; pt2 = point_pair[1] +# sub1 = None; sub2 = None +# for sub in sub_contours: +# if pt1 in sub and pt2 not in sub: +# sub1 = sub.copy() +# if pt2 in sub and pt1 not in sub: +# sub2 = sub.copy() +# return [sub1, sub2] +# +# # +# print("sub-contours num : %d" % len(sub_contours)) # # # def findCoSubContour(pair, sub_contours): # # if pair is None or sub_contours is None: @@ -372,17 +482,4 @@ def findCoLinearSubContours(point_pair, sub_contours): -# cv2.imshow("radicals", radicals) -cv2.imshow("contour", contour) -# cv2.imshow("skeleton", skeleton) -# cv2.imshow("skeleton rgb", skeleton_rgb) -# cv2.imshow("img_corner_area", img_corner_area) -# cv2.imshow("contour_rgb", contour_rgb) - -cv2.waitKey(0) -cv2.destroyAllWindows() - - - -