Skip to content

Commit

Permalink
add auto
Browse files Browse the repository at this point in the history
  • Loading branch information
liupeng89 committed May 15, 2018
1 parent 2beaa64 commit 7200618
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 93 deletions.
121 changes: 91 additions & 30 deletions autoStrokeExtracting.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,118 @@
# coding: utf-8
import cv2
import numpy as np
import math

from utils.Functions import getConnectedComponents
from utils.Functions import getConnectedComponents, getContourOfImage, getSkeletonOfImage, removeBreakPointsOfContour, \
removeBranchOfSkeletonLine, removeBranchOfSkeleton, getEndPointsOfSkeletonLine, \
getCrossPointsOfSkeletonLine, sortPointsOnContourOfImage, min_distance_point2pointlist, \
getNumberOfValidPixels, segmentContourBasedOnCornerPoints, createBlankGrayscaleImage, \
getLinePoints, getBreakPointsFromContour, merge_corner_lines_to_point, getCropLines, \
getCornerPointsOfImage, getClusterOfCornerPoints, getCropLinesPoints, \
getConnectedComponentsOfGrayScale, getAllMiniBoundingBoxesOfImage, getCornersPoints, \
getContourImage, getValidCornersPoints


def extractStrokesFromRadical(radical):
"""
Stroke extracting from one radical.
:param radical:
:return:
"""
if radical is None:
return



def autoStrokeExtractiing(image):
def autoStrokeExtracting(index, image, threshold_value=200):
"""
Automatic strokes extracting
:param image: grayscale image
:return: strokes images with same size
"""
strokes = []
if image is None:
return
return strokes

# get connected components
radicals = getConnectedComponents(image)
print("radicals num: %d" % len(radicals))
contour_img = getContourImage(image)
contours = getConnectedComponents(contour_img)
print("contours num: %d" % len(contours))

# strokes
total_strokes = []
for rad in radicals:
strokes = extractStrokesFromRadical(rad)
total_strokes += strokes
corners_points_sorted = []
for ct in contours:
points = sortPointsOnContourOfImage(ct)
corners_points_sorted.append(points)
if len(corners_points_sorted) == 1:
print("No holes exist!")
elif len(corners_points_sorted) >= 2:
print("Holes exist!")

# grayscale image to binary image
_, img_bit = cv2.threshold(image, threshold_value, 255, cv2.THRESH_BINARY)

# skeleton
skeleton_img = getSkeletonOfImage(img_bit)
end_points = getEndPointsOfSkeletonLine(skeleton_img)
cross_points = getCrossPointsOfSkeletonLine(skeleton_img)

print("end points num: %d" % len(end_points))
print("cross points num: %d" % len(cross_points))

if len(cross_points) == 0:
print("no cross points!")
strokes.append(image)
return strokes

# corner area points
corners_all_points = getCornersPoints(image.copy(), contour_img)
corners_points = getValidCornersPoints(corners_all_points, cross_points, end_points)
print("corners points num: %d" % len(corners_points))

return total_strokes

contour_rgb = cv2.cvtColor(contour_img, cv2.COLOR_GRAY2RGB)
for pt in corners_points:
contour_rgb[pt[1]][pt[0]] = (0, 0, 255)

# cluster corners points based on the cross point
dist_threshold = 30
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.imshow("radical_%d" % index, contour_rgb)

return strokes


def main():
path = "1133壬.jpg"
# 1133壬 2252支 0631叟 0633口 0242俄 0195佛
path = "0195佛.jpg"

img = cv2.imread(path, 0)
_, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
img_rgb = cv2.imread(path)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
_, img_bit = cv2.threshold(img_gray, 200, 255, cv2.THRESH_BINARY)

# grayscale threshold filter pixel > 200 = 255
for y in range(img_gray.shape[0]):
for x in range(img_gray.shape[1]):
if img_gray[y][x] > 200:
img_gray[y][x] = 255

# get components
components = getConnectedComponentsOfGrayScale(img_gray, threshold_value=200)

if components is None or len(components) == 0:
print("components num is 0")
return

total_strokes = []

img = np.array(img, dtype=np.uint8)
for i in range(len(components)):
radical = components[i]

strokes = autoStrokeExtractiing(img)
radical_strokes = autoStrokeExtracting(i, radical)
if radical_strokes is None or len(radical_strokes) == 0:
print("radiacal is None")
else:
total_strokes += radical_strokes

# print("storke num :%d" % len(strokes))
# cv2.imshow("img gray", img_gray)
#
# for i in range(len(strokes)):
# cv2.imshow("stroke_%d"%i, strokes[i])
# for i in range(len(components)):
# cv2.imshow("stroke_%d"%i, components[i])

cv2.waitKey(0)
cv2.destroyAllWindows()
Expand Down
4 changes: 1 addition & 3 deletions test_radical_stroke_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
radicals = components[0]
radicals = np.array(radicals, dtype=np.uint8)

# sorted the contour points
# sorted the contour po
contour_sorted = sortPointsOnContourOfImage(contour)
contour_rgb = cv2.cvtColor(contour, cv2.COLOR_GRAY2RGB)

Expand Down Expand Up @@ -78,8 +78,6 @@
sub_contours = segmentContourBasedOnCornerPoints(contour_sorted, corner_points)
print("sub contours num: %d" % len(sub_contours))



for i in range(len(strokes_components)):
component = strokes_components[i]
cv2.imshow("component_%d" % i, component)
Expand Down
Loading

0 comments on commit 7200618

Please sign in to comment.