From 46a5234a9abb5023272512d2b9d2245dabfe34d9 Mon Sep 17 00:00:00 2001 From: Peter_Liu Date: Mon, 21 May 2018 22:57:58 +0800 Subject: [PATCH] fix bug --- autoStrokeExtracting.py | 17 ++++++++-------- test_radical_stroke_extract.py | 4 ++-- test_skeleton.py | 36 ++++++++++++++++++++++++++++++++++ utils/Functions.py | 6 +++--- 4 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 test_skeleton.py diff --git a/autoStrokeExtracting.py b/autoStrokeExtracting.py index 78682f7..8818362 100644 --- a/autoStrokeExtracting.py +++ b/autoStrokeExtracting.py @@ -4,8 +4,8 @@ import math from utils.Functions import getConnectedComponents, getContourOfImage, getSkeletonOfImage, removeBreakPointsOfContour, \ - removeBranchOfSkeletonLine, removeBranchOfSkeleton, getEndPointsOfSkeletonLine, \ - getCrossPointsOfSkeletonLine, sortPointsOnContourOfImage, min_distance_point2pointlist, \ + removeBranchOfSkeletonLine, removeExtraBranchesOfSkeleton, getEndPointsOfSkeletonLine, \ + getCrossPointsOfSkeletonLine, sortPointsOnContourOfImage, min_distance_point2pointlist, \ getNumberOfValidPixels, segmentContourBasedOnCornerPoints, createBlankGrayscaleImage, \ getLinePoints, getBreakPointsFromContour, merge_corner_lines_to_point, getCropLines, \ getCornerPointsOfImage, getClusterOfCornerPoints, getCropLinesPoints, \ @@ -24,9 +24,9 @@ def autoStrokeExtracting(index, image, threshold_value=200): if image is None: return strokes - # get connected components + # get connected components from the grayscale image, not for the binary image. contour_img = getContourImage(image) - contours = getConnectedComponents(contour_img) + contours = getConnectedComponents(contour_img) # no holes, num=1, holes exist, num >= 2 print("contours num: %d" % len(contours)) corners_points_sorted = [] @@ -41,10 +41,11 @@ def autoStrokeExtracting(index, image, threshold_value=200): # grayscale image to binary image _, img_bit = cv2.threshold(image, threshold_value, 255, cv2.THRESH_BINARY) - # skeleton + # skeleton image of width 1 pixel of binray image skeleton_img = getSkeletonOfImage(img_bit) - end_points = getEndPointsOfSkeletonLine(skeleton_img) - cross_points = getCrossPointsOfSkeletonLine(skeleton_img) + skeleton_img = removeExtraBranchesOfSkeleton(skeleton_img) + end_points = getEndPointsOfSkeletonLine(skeleton_img) # end points + cross_points = getCrossPointsOfSkeletonLine(skeleton_img) # croiss points print("end points num: %d" % len(end_points)) print("cross points num: %d" % len(cross_points)) @@ -72,10 +73,10 @@ def autoStrokeExtracting(index, image, threshold_value=200): contour_rgb[pt[1]][pt[0]] = (0, 0, 255) # cluster corners points based on the cross point - dist_threshold = 40 corner_points_cluster = getClusterOfCornerPoints(corners_points, cross_points) crop_lines = getCropLines(corner_points_cluster) + for line in crop_lines: cv2.line(contour_rgb, line[0], line[1], (0, 255, 0), 1) cv2.line(contour_gray, line[0], line[1], 0, 1) diff --git a/test_radical_stroke_extract.py b/test_radical_stroke_extract.py index c446fa8..9492143 100644 --- a/test_radical_stroke_extract.py +++ b/test_radical_stroke_extract.py @@ -3,7 +3,7 @@ import math from utils.Functions import getConnectedComponents, getContourOfImage, getSkeletonOfImage, removeBreakPointsOfContour, \ - removeBranchOfSkeletonLine, removeBranchOfSkeleton, getEndPointsOfSkeletonLine, \ + removeBranchOfSkeletonLine, removeExtraBranchesOfSkeleton, getEndPointsOfSkeletonLine, \ getCrossPointsOfSkeletonLine, sortPointsOnContourOfImage, min_distance_point2pointlist, \ getNumberOfValidPixels, segmentContourBasedOnCornerPoints, createBlankGrayscaleImage, \ getLinePoints, getBreakPointsFromContour, merge_corner_lines_to_point, getCropLines, \ @@ -35,7 +35,7 @@ # # skeleton skeleton = getSkeletonOfImage(radicals) # # remove extra branches -skeleton = removeBranchOfSkeleton(skeleton, distance_threshod=20) +skeleton = removeExtraBranchesOfSkeleton(skeleton, distance_threshod=20) # end_points = getEndPointsOfSkeletonLine(skeleton) cross_points = getCrossPointsOfSkeletonLine(skeleton) diff --git a/test_skeleton.py b/test_skeleton.py new file mode 100644 index 0000000..d418183 --- /dev/null +++ b/test_skeleton.py @@ -0,0 +1,36 @@ +# coding: utf-8 +import cv2 +import numpy as np + +from utils.Functions import getSkeletonOfImage, getEndPointsOfSkeletonLine, getCrossPointsOfSkeletonLine, \ + removeExtraBranchesOfSkeleton + +# 1133壬 2252支 0631叟 0633口 0242俄 0195佛 0860善 0059乘 0098亩 +path = "2252支.jpg" + +img = cv2.imread(path, 0) +_, img_bit = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY) + +skeleton = getSkeletonOfImage(img_bit) +skeleton = removeExtraBranchesOfSkeleton(skeleton) + +skeleton_rgb = cv2.cvtColor(skeleton, cv2.COLOR_GRAY2RGB) + +end_points = getEndPointsOfSkeletonLine(skeleton) + +cross_points = getCrossPointsOfSkeletonLine(skeleton) + +# 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) + + +cv2.imshow("skeleton rgb", skeleton_rgb) + +cv2.waitKey(0) +cv2.destroyAllWindows() + + diff --git a/utils/Functions.py b/utils/Functions.py index 9b6b4c8..5d8ae53 100644 --- a/utils/Functions.py +++ b/utils/Functions.py @@ -835,9 +835,8 @@ def getEndPointsOfSkeletonLine(image): for y in range(1, image.shape[0] - 1): for x in range(1, image.shape[1] - 1): if image[y][x] == 0.0: - # black points + # black points number black_num = getNumberOfValidPixels(image, x, y) - # end points if black_num == 1: end_points.append((x, y)) @@ -917,7 +916,7 @@ def getCrossPointsOfSkeletonLine(image): return cross_points_no_extra -def removeBranchOfSkeleton(image, distance_threshod=20): +def removeExtraBranchesOfSkeleton(image, distance_threshod=20): """ Remove extra branches of skeleton image :param image: skeleton grayscale image with 1-pixel width line @@ -1761,6 +1760,7 @@ def getCropLines(corner_points_cluster): elif len(corner_clt) == 1: print("One corner point") + elif len(corner_clt) == 4: # based on the y list to detect rectangle or diamond