Skip to content

Commit

Permalink
add images
Browse files Browse the repository at this point in the history
  • Loading branch information
liupeng89 committed Oct 5, 2018
1 parent 0b2433e commit 229e9f7
Show file tree
Hide file tree
Showing 79 changed files with 1,530 additions and 114 deletions.
179 changes: 179 additions & 0 deletions test/auto_stroke_extaction_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# coding: utf-8
import cv2
import numpy as np
from algorithms.RDP import rdp
import math

from utils.Functions import getConnectedComponents, getSkeletonOfImage, getEndPointsOfSkeletonLine, \
getCrossPointsOfSkeletonLine, createBlankGrayscaleImage, getCropLines, \
getClusterOfCornerPoints, getAllMiniBoundingBoxesOfImage, getContourImage, \
getValidCornersPoints, getDistanceBetweenPointAndComponent, isValidComponent, \
removeShortBranchesOfSkeleton, sortPointsOnContourOfImage, removeBreakPointsOfContour

from utils.stroke_extraction_algorithm import autoStrokeExtractFromCharacter


path = "../test_images/page1_char_3.png"

# Image porcessing
img = cv2.imread(path, 0)
_, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)

components = getConnectedComponents(img, connectivity=8)
print("components num: %d" % len(components))

# 6. Get skeletons of component.
comp_skeleton = getSkeletonOfImage(components[2])
comp_skeleton = getSkeletonOfImage(comp_skeleton)
cv2.imshow("skeleton_original", comp_skeleton)

# 7. Process the skeleton by remove extra branches.
# comp_skeleton = removeShortBranchesOfSkeleton(comp_skeleton, length_threshold=30)
comp_skeleton_rgb = cv2.cvtColor(comp_skeleton, cv2.COLOR_GRAY2RGB)
cv2.imshow("skeleton_smoothed", comp_skeleton)

# 8. Get the end points and cross points after skeleton processed
end_points = getEndPointsOfSkeletonLine(comp_skeleton)
cross_points = getCrossPointsOfSkeletonLine(comp_skeleton)
print("end points num: %d ,and cross points num: %d" % (len(end_points), len(cross_points)))

# 9. Get contour image of component
comp_contours_img = getContourImage(components[2])
comp_contours_img_rbg = cv2.cvtColor(comp_contours_img, cv2.COLOR_GRAY2RGB)

# 10. Detect the number of contours and return all contours
comp_contours = getConnectedComponents(comp_contours_img, connectivity=8)
print("contours num: %d" % len(comp_contours))


# 11. Get points on contours
corners_points = []
for cont in comp_contours:
cont = removeBreakPointsOfContour(cont)
cont_sorted = sortPointsOnContourOfImage(cont)
cont_points = rdp(cont_sorted, 5)
corners_points += cont_points

CORNER_CROSS_DIST_THRESHOLD = 20
corners_points_merged = []
for pt in corners_points:
for cpt in cross_points:
dist = math.sqrt((pt[0] - cpt[0])**2+(pt[1] - cpt[1])**2)
if dist < CORNER_CROSS_DIST_THRESHOLD:
corners_points_merged.append(pt)
break

# for pt in cross_points_:
# cv2.circle(comp_skeleton_rgb, pt, 2, (0,255,0), 3)
for pt in cross_points:
cv2.circle(comp_skeleton_rgb, pt, 2, (0, 0, 255), 3)
for pt in end_points:
cv2.circle(comp_skeleton_rgb, pt, 2, (255, 0, 0), 3)
cv2.imshow("comp_skeleton_rgb", comp_skeleton_rgb)

for pt in corners_points:
cv2.circle(comp_contours_img_rbg, pt, 4, (0,0,255), 4)
for pt in corners_points_merged:
cv2.circle(comp_contours_img_rbg, pt, 2, (0, 255, 0), 3)
cv2.imshow("comp_contours_img_rbg", comp_contours_img_rbg)

corners_points_cluster = getClusterOfCornerPoints(corners_points_merged, corners_points,50)
print(corners_points_cluster)


# img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)

# auto extract strokes
# strokes = autoStrokeExtractFromCharacter(img)
#
# print("stroke num:", len(strokes))
#
# for i in range(len(strokes)):
# cv2.imshow("stroke_%d" % i, strokes[i])


# cv2.waitKey(0)
# cv2.destroyAllWindows()


# # img_rbg = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
# img_rgb = createBlankRGBImage(img)
#
#
# # Get the contour of image
# img_contour = getContourImage(img)
#
# # Sorted the points on contour
# img_contour_points = sortPointsOnContourOfImage(img_contour)
# print("Before simplified num:", len(img_contour_points))
#
# # Simplify points on contour
# img_contour_points = rdp(img_contour_points, 3)
# print("After simplified num: ", len(img_contour_points))
#
# # Start point
# cv2.circle(img_rgb, img_contour_points[0], 5, (0, 255, 0), 6)
#
# for i in range(len(img_contour_points)):
# start_pt = img_contour_points[i]
# if i == len(img_contour_points) - 1:
# end_pt = img_contour_points[0]
# else:
# end_pt = img_contour_points[i + 1]
# cv2.line(img_rgb, start_pt, end_pt, (0, 0, 255), 1)
#
# cv2.circle(img_rgb, start_pt, 4, (255, 0, 0), 5)
#
# # Merge closed points
# img_contour_points_merged = [img_contour_points[0]]
# POINTS_DIST_THRESHOLD = 11
# for i in range(len(img_contour_points)):
# curr_pt = img_contour_points[i]
# if i == len(img_contour_points) - 1:
# next_pt = img_contour_points[0]
# else:
# next_pt = img_contour_points[i + 1]
# dist = math.sqrt((curr_pt[0] - next_pt[0])**2 + (curr_pt[1] - next_pt[1])**2)
#
# if dist < POINTS_DIST_THRESHOLD:
# img_contour_points_merged.append(next_pt)
# i += 1
# else:
# img_contour_points_merged.append(curr_pt)
# print("After merged num:", len(img_contour_points_merged))
# for pt in img_contour_points_merged:
# cv2.circle(img_rgb, pt, 2, (0, 0, 255), 2)




# Get each point tangent angle
# for i in range(len(img_contour_points) - 1):
# start_pt = img_contour_points[i]
# mid_pt = end_pt = start_pt
# if i == len(img_contour_points) - 2:
# mid_pt = img_contour_points[i + 1]
# end_pt = img_contour_points[0]
# elif i == len(img_contour_points) - 1:
# mid_pt = img_contour_points[0]
# end_pt = img_contour_points[1]
#
# # calculate the angle between Line(start_pt, mid_pt) and Line(mid_pt, end_pt)
# theta = math.atan2(end_pt[1] - mid_pt[1], end_pt[0] - mid_pt[0]) - \
# math.atan2(start_pt[1] - mid_pt[1], start_pt[0] - mid_pt[0])
# print(theta)
#
# if theta > 0.05 or theta < -0.05:
# cv2.circle(img_rgb, start_pt, 3, (0, 255, 0), 4)






# cv2.imshow("img", img)
# cv2.imshow("contour", img_contour)
# cv2.imshow("rgb", img_rgb)
#
cv2.waitKey(0)
cv2.destroyAllWindows()
53 changes: 53 additions & 0 deletions test/data_generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# coding: utf-8
import os
import cv2
import numpy as np

from utils.Functions import getConnectedComponents

path = "/Users/liupeng/Documents/PythonProjects/九宫格修复版/九成宫修复版单字"

files = os.listdir(path)


for file in files:
print("process:", file)
if ".png" not in file:
continue
name = file.replace(".png", "")
if not os.path.exists(os.path.join(path, name)):
os.makedirs(os.path.join(path, name))

if not os.path.exists(os.path.join(path, name, "original")):
os.makedirs(os.path.join(path, name, "original"))
if not os.path.exists(os.path.join(path, name, "char")):
os.makedirs(os.path.join(path, name, "char"))
if not os.path.exists(os.path.join(path, name, "strokes")):
os.makedirs(os.path.join(path, name, "strokes"))

os.system("cp %s %s" % (os.path.join(path, file), os.path.join(path, name, "original")))

# processing
img = cv2.imread(os.path.join(path, file), 0)
_, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
img = np.array(img)

# moments
im2, contours, hierarchy = cv2.findContours(img, 1, 2)
for i in range(len(contours)):
x, y, w, h = cv2.boundingRect(contours[i])
if w < 30 or h < 30:
img[y: y+h, x: x+w] = 255

img = cv2.resize(img, (int(img.shape[1]/2.), int(img.shape[0]/2.)))

cv2.imwrite(os.path.join(path, name, "char", file), img)

os.system("convert %s %s" % (os.path.join(path, name, "char", file), os.path.join(path, name, "char", name+".pnm")))
os.system(
"potrace -s -o %s %s" % (os.path.join(path, name, "char", name + ".svg"),
os.path.join(path, name, "char", name + ".pnm")))




File renamed without changes.
70 changes: 46 additions & 24 deletions test/pypotrce_test.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
from __future__ import division, print_function
from svgpathtools import Path, Line, QuadraticBezier, CubicBezier, Arc

seg1 = CubicBezier(300+100j, 100+100j, 200+200j, 200+300j)
seg2 = Line(200+300j, 250+350j)
path = Path(seg1, seg2)

from svgpathtools import parse_path
path_alt = parse_path('M 300 100 C 100 100 200 200 200 300 L 250 350')

print(path)
print(path_alt)

print(path == path_alt)

print(path.d())

path.append(CubicBezier(250+350j, 275+350j, 250+225j, 200+100j))
print(path)

path[0] = Line(200+100j, 200+300j)
print(path)

print('path is contious?', path.iscontinuous())
print('path is closed?', path.isclosed())
from svgpathtools import Path, Line, QuadraticBezier, CubicBezier, Arc, wsvg, svg2paths, smoothed_path, kinks

paths, attributes = svg2paths("../test_images/src_resize.svg")

# for p in paths:
# print(type(p))
# print(len(p))


# print(len(paths))
# print(type(paths))
# paths = paths.pop(1)
#
path = paths[0]
# #
path_smooth = smoothed_path(path, maxjointsize=10)
#
wsvg(path, filename="../test_images/path_0.svg")
wsvg(path_smooth, filename="../test_images/path_s.svg")



# seg1 = CubicBezier(300+100j, 100+100j, 200+200j, 200+300j)
# seg2 = Line(200+300j, 250+350j)
# path = Path(seg1, seg2)
#
# from svgpathtools import parse_path
# path_alt = parse_path('M 300 100 C 100 100 200 200 200 300 L 250 350')
#
# print(path)
# print(path_alt)
#
# print(path == path_alt)
#
# print(path.d())
#
# path.append(CubicBezier(250+350j, 275+350j, 250+225j, 200+100j))
# print(path)
#
# path[0] = Line(200+100j, 200+300j)
# print(path)
#
# print('path is contious?', path.iscontinuous())
# print('path is closed?', path.isclosed())
#
# wsvg(path, filename="path.svg")
26 changes: 26 additions & 0 deletions test/shift_max_coverage_area_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import cv2
import numpy as np

from utils.Functions import shiftImageWithMaxCoverageArea, shiftImageWithMaxCR


char_path = "../test_images/src_comp_0.png"

stroke_path = "../test_images/temp_stroke_0.png"

char_img = cv2.imread(char_path, 0)
stroke_img = cv2.imread(stroke_path, 0)

_, char_img = cv2.threshold(char_img, 127, 255, cv2.THRESH_BINARY)
_, stroke_img = cv2.threshold(stroke_img, 127, 255, cv2.THRESH_BINARY)


new_stroke_img = shiftImageWithMaxCR(char_img, stroke_img)


cv2.imshow("stroke img", stroke_img)
cv2.imshow("new stroke img", new_stroke_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Loading

0 comments on commit 229e9f7

Please sign in to comment.