Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
liupeng89 committed May 21, 2018
1 parent 8f3c063 commit 46a5234
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
17 changes: 9 additions & 8 deletions autoStrokeExtracting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand All @@ -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 = []
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test_radical_stroke_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions test_skeleton.py
Original file line number Diff line number Diff line change
@@ -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()


6 changes: 3 additions & 3 deletions utils/Functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 46a5234

Please sign in to comment.