Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
liupeng89 committed May 18, 2018
1 parent b2a3f5f commit 8f3c063
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 40 deletions.
29 changes: 11 additions & 18 deletions autoStrokeExtracting.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def autoStrokeExtracting(index, image, threshold_value=200):

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

if len(corners_points) == 0:
Expand All @@ -72,7 +72,7 @@ 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 = 30
dist_threshold = 40
corner_points_cluster = getClusterOfCornerPoints(corners_points, cross_points)

crop_lines = getCropLines(corner_points_cluster)
Expand Down Expand Up @@ -155,7 +155,6 @@ def autoStrokeExtracting(index, image, threshold_value=200):
# cluster components based on the cropping lines
for i in range(len(components)):
part = components[i]

part_lines = [] # used to detect overlap region components.

# find single part is stroke
Expand All @@ -168,14 +167,13 @@ def autoStrokeExtracting(index, image, threshold_value=200):
break
if part[y1][x1] == 0.0 and part[y2][x2] == 0.0:
part_lines.append(line)
print("part lines num: %d" % len(part_lines))

if is_single and isIndependentCropLines(part_lines):
strokes.append(part)
used_components.append(i)

print("single stroke num: %d" % len(strokes))
print("used components num: %d" % len(used_components))
print(used_components)

# cluster components based on the cropping lines
for i in range(len(components)):
Expand All @@ -195,8 +193,6 @@ def autoStrokeExtracting(index, image, threshold_value=200):
component_line_relation[i] = lines_id
used_components.append(i)

print(component_line_relation)

# cluster components based on the relations and merge those related components
clusters = []
for k1, v1 in component_line_relation.items():
Expand All @@ -221,7 +217,6 @@ def autoStrokeExtracting(index, image, threshold_value=200):
# merge components based on the cluster
for i in range(len(clusters)):
cluster = clusters[i]

bk = createBlankGrayscaleImage(image)

for clt in cluster:
Expand All @@ -230,25 +225,24 @@ def autoStrokeExtracting(index, image, threshold_value=200):
# add to strokes
strokes.append(bk)


cv2.imshow("radical_%d" % index, contour_rgb)
cv2.imshow("radical_gray_%d" % index, contour_gray)

for i in range(len(overlap_components)):
cv2.imshow("over_%d_com_%d" % (index, i), overlap_components[i])
# for i in range(len(overlap_components)):
# cv2.imshow("over_%d_com_%d" % (index, i), overlap_components[i])

for i in range(len(components)):
cv2.imshow("ra_%d_com_%d" % (index, i), components[i])
# for i in range(len(components)):
# cv2.imshow("ra_%d_com_%d" % (index, i), components[i])

for i in range(len(strokes)):
cv2.imshow("ra_%d_stroke_%d" % (index, i), strokes[i])
# for i in range(len(strokes)):
# cv2.imshow("ra_%d_stroke_%d" % (index, i), strokes[i])

return strokes


def main():
# 1133壬 2252支 0631叟 0633口 0242俄 0195佛 0860善 0059乘 0098亩
path = "0098亩.jpg"
path = "2252支.jpg"

img_rgb = cv2.imread(path)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
Expand Down Expand Up @@ -278,8 +272,7 @@ def main():
else:
total_strokes += radical_strokes

# cv2.imshow("img gray", img_gray)
#
# display strokes
for i in range(len(total_strokes)):
cv2.imshow("stroke_%d"%i, total_strokes[i])

Expand Down
66 changes: 46 additions & 20 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,54 @@
# cv2.waitKey(0)
# cv2.destroyAllWindows()

strokes = []
# strokes = []
#
# component_line_relation = {5: [2, 3, 4, 5], 10: [8, 9, 10, 11], 0: [4, 5], 1: [2, 3], 6: [2, 3], 7: [4, 5], 8: [9, 11], 9: [8, 10], 11: [8, 10], 12: [9, 11]}
# print(component_line_relation)
#
# clusters = []
# for k1, v1 in component_line_relation.items():
#
# cluster = [k1]
# value_sets = [set(v1)]
#
# for k2, v2 in component_line_relation.items():
# is_related = True
# for value in value_sets:
# if not value.intersection(set(v2)):
# is_related = False
# break
# if is_related and k2 not in cluster:
# cluster.append(k2)
# value_sets.append(set(v2))
# cluster = sorted(cluster)
# if cluster not in clusters:
# clusters.append(cluster)
#
# print(clusters)
import cv2
import numpy as np

component_line_relation = {5: [2, 3, 4, 5], 10: [8, 9, 10, 11], 0: [4, 5], 1: [2, 3], 6: [2, 3], 7: [4, 5], 8: [9, 11], 9: [8, 10], 11: [8, 10], 12: [9, 11]}
print(component_line_relation)
path = "2252支.jpg"

clusters = []
for k1, v1 in component_line_relation.items():
img = cv2.imread(path, 0)
img_rgb = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)

cluster = [k1]
value_sets = [set(v1)]
corner_img = np.float32(img)
dst = cv2.cornerHarris(corner_img, 2, 3, 0.04)
dst = cv2.dilate(dst, None)

for k2, v2 in component_line_relation.items():
is_related = True
for value in value_sets:
if not value.intersection(set(v2)):
is_related = False
break
if is_related and k2 not in cluster:
cluster.append(k2)
value_sets.append(set(v2))
cluster = sorted(cluster)
if cluster not in clusters:
clusters.append(cluster)
corners_area_points = []
for y in range(dst.shape[0]):
for x in range(dst.shape[1]):
if dst[y][x] > 0.1 * dst.max():
corners_area_points.append((x, y))

print(clusters)

for pt in corners_area_points:
img_rgb[pt[1]][pt[0]] = (0, 0, 255)

cv2.imshow("rgb", img_rgb)

cv2.waitKey(0)
cv2.destroyAllWindows()
4 changes: 2 additions & 2 deletions utils/Functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1756,7 +1756,7 @@ def getCropLines(corner_points_cluster):
for i in range(len(corner_points_cluster)):
corner_clt = corner_points_cluster[i]
if len(corner_clt) == 2:
print(" tow points")
print("tow points")
crop_lines.append((corner_clt))
elif len(corner_clt) == 1:
print("One corner point")
Expand Down Expand Up @@ -1932,7 +1932,7 @@ def getCornersPoints(grayscale, contour_img):

# corner area points
corner_img = np.float32(grayscale)
dst = cv2.cornerHarris(corner_img, 3, 3, 0.04)
dst = cv2.cornerHarris(corner_img, 2, 3, 0.04)
dst = cv2.dilate(dst, None)

corners_area_points = []
Expand Down

0 comments on commit 8f3c063

Please sign in to comment.