Skip to content

Commit

Permalink
add autostroke for gui tool
Browse files Browse the repository at this point in the history
  • Loading branch information
liupeng89 committed Jun 13, 2018
1 parent d716051 commit e5fa1ba
Show file tree
Hide file tree
Showing 6 changed files with 532 additions and 189 deletions.
22 changes: 22 additions & 0 deletions calligraphyStrokeExtractionToolGUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from calligraphyStrokeExtractionTool.strokeExtractingMainwindow import Ui_MainWindow
from utils.Functions import splitConnectedComponents
from utils.stroke_extraction_algorithm import autoStrokeExtractFromComponent


class StrokeExtractToolMainWindow(QMainWindow, Ui_MainWindow):
Expand Down Expand Up @@ -62,6 +63,7 @@ def __init__(self):
self.add_stroke_btn.clicked.connect(self.addStrokeBtn)
self.delete_stroke_btn.clicked.connect(self.deleteStrokeBtn)
self.saveStroke_btn.clicked.connect(self.strokesSaveBtn)
self.auto_extract_btn.clicked.connect(self.autoStrokesExtract)

def openBtn(self):
"""
Expand Down Expand Up @@ -169,6 +171,26 @@ def radicalsListView_clicked(self, qModelIndex):
self.statusbar.showMessage("Radical listview item " + str(qModelIndex.row()) + " selected!")
del img_, qimg

def autoStrokesExtract(self):
"""
Automatically strokes extraction.
:return:
"""
print("auto stroke extract button clicked!")
component = self.radical_gray.copy()

strokes = autoStrokeExtractFromComponent(component)

print("strokes num: %d" % len(strokes))

self.strokes += strokes

for i in range(len(strokes)):
stroke_name = "stroke_" + str(i)
self.strokes_name.append(stroke_name)

self.stroke_slm.setStringList(self.strokes_name)

def addStrokeBtn(self):
"""
Select stroke from image, and add to strokes list.
Expand Down
2 changes: 1 addition & 1 deletion test/autoStrokeExtracting.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def autoStrokeExtracting(index, image, threshold_value=200):
corner_points_cluster = getClusterOfCornerPoints(corners_points, cross_points)

# cropping lines based on the corner points
crop_lines = getCropLines(corner_points_cluster)
crop_lines = getCropLines(corner_points_cluster, None)

for line in crop_lines:
cv2.line(contour_rgb, line[0], line[1], (0, 255, 0), 1)
Expand Down
2 changes: 1 addition & 1 deletion test/test_radical_stroke_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
print("corner cluster num:%d" % len(corner_points_cluster))

# detect corner points type: two point, four point (rectangle or diamond)
crop_lines = getCropLines(corner_points_cluster)
crop_lines = getCropLines(corner_points_cluster, None)
for line in crop_lines:
cv2.line(contour_rgb, line[0], line[1], (0, 255, 0), 1)

Expand Down
25 changes: 1 addition & 24 deletions test_auto_stroke_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def autoStrokeExtractFromComponent(component):
print("corner points cluster num: %d" % len(corners_points_cluster))

# 14. Generate cropping lines between two corner points
crop_lines = getCropLines(corners_points_cluster)
crop_lines = getCropLines(corners_points_cluster, None)
print("cropping lines num: %d" % len(crop_lines))

# 15. Separate the components based on the cropping lines
Expand All @@ -129,25 +129,6 @@ def autoStrokeExtractFromComponent(component):
print("parts of component num: %d" % len(comp_parts))

# 17. Add cropping lines to corresponding parts of component
def getDistancePointToRegion(point, region):
"""
Get minimal distance fomt point to region.
:param point:
:param region:
:return:
"""
if point is None or region is None:
return -1
min_dist = 100000000
for y in range(region.shape[0]):
for x in range(region.shape[1]):
if region[y][x] == 0.0:
dist = math.sqrt((point[0] - x)**2 + (point[1] - y)**2)
if dist < min_dist:
min_dist = dist

return min_dist

# add lines to parts of component.
for i in range(len(comp_parts)):
part = comp_parts[i]
Expand Down Expand Up @@ -187,7 +168,6 @@ def getDistancePointToRegion(point, region):
# Cropping lines are divided into two types: in intersect part and not in this part.
line_parts_relation = []
for index in intersect_parts_crop_lines_index:
print("line index: %d" % index)
line = crop_lines[index]

# line and parts that are connected by this crop line: A - intersect_part - B
Expand All @@ -198,7 +178,6 @@ def getDistancePointToRegion(point, region):
if intersect_part[line[0][1]][line[0][0]] == 0.0 and intersect_part[line[1][1]][line[1][0]] == 0.0:
# line in this intersect part
line_connected_parts.append(i)
# print(line_connected_parts)
# find two parts connectd by this crop line
for i in range(len(comp_parts)):

Expand All @@ -215,7 +194,6 @@ def getDistancePointToRegion(point, region):
# add line connected parts to relation list.
if line_connected_parts not in line_parts_relation:
line_parts_relation.append(line_connected_parts)
# print(line_parts_relation)

# add independent parts to relation of line and parts
for i in range(len(comp_parts)):
Expand All @@ -230,7 +208,6 @@ def getDistancePointToRegion(point, region):

if line_connected_parts != []:
line_parts_relation.append(line_connected_parts)
print(line_parts_relation)

# 20. Merge parts based on the line parts relation
for i in range(len(line_parts_relation)):
Expand Down
Loading

0 comments on commit e5fa1ba

Please sign in to comment.