-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add RDP algorithm to smooth functions
- Loading branch information
Showing
12 changed files
with
401 additions
and
182 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
The Ramer-Douglas-Peucker algorithm roughly ported from the pseudo-code provided | ||
by http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm | ||
""" | ||
from math import sqrt | ||
|
||
|
||
def distance(a, b): | ||
return sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) | ||
|
||
|
||
def point_line_distance(point, start, end): | ||
""" | ||
Calcluate the distance between point to line. | ||
point = (px, py) p | ||
start = (ax, ay) a | ||
end = (bx, by) b | ||
1. b - a = (bx - ax, by - ay) | ||
p - a = (px - ax, py - ay) | ||
2. (b - a) * (c - a) = | (b-a) * (c-a)| | ||
3. diatance p to line (a, b) | ||
= |(b-a) * (c-a)| / sqrt((bx -ax)^2 + (by-ay)^2) | ||
:param point: | ||
:param start: | ||
:param end: | ||
:return: | ||
""" | ||
if start == end: | ||
return distance(point, start) | ||
else: | ||
n = abs((end[0] - start[0]) * (start[1] - point[1]) - (start[0] - point[0]) * (end[1] - start[1])) | ||
|
||
d = sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2) | ||
return n / d | ||
|
||
|
||
def rdp(points, epsilon): | ||
""" | ||
Reduces a series of points to a simplified version that loses detail, but maintains the general shape of the series. | ||
:param points: | ||
:param epsilon: | ||
:return: | ||
""" | ||
dmax = 0.0 | ||
index = 0 | ||
for i in range(1, len(points)-1): | ||
d = point_line_distance(points[i], points[0], points[-1]) | ||
if d > dmax: | ||
index = i | ||
dmax = d | ||
if dmax > epsilon: | ||
results = rdp(points[:index+1], epsilon)[:-1] + rdp(points[index:], epsilon) | ||
else: | ||
results = [points[0], points[-1]] | ||
return results |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import sys | ||
import math | ||
import cv2 | ||
import os | ||
import numpy as np | ||
from PyQt5.QtCore import * | ||
from PyQt5.QtGui import * | ||
from PyQt5.QtWidgets import * | ||
|
||
from calligraphySmoothingTool.smoothmanuallymainwindow import Ui_MainWindow | ||
|
||
from utils.Functions import getContourOfImage, removeBreakPointsOfContour, \ | ||
sortPointsOnContourOfImage, fitCurve, draw_cubic_bezier | ||
from utils.contours_smoothed_algorithm import autoSmoothContoursOfCharacter | ||
|
||
path = "../test_images/src_resize.png" | ||
|
||
img = cv2.imread(path, 0) | ||
|
||
img_smoothed = autoSmoothContoursOfCharacter(img) | ||
|
||
|
||
|
||
cv2.imshow("img", img) | ||
cv2.imshow("img_smoothed", img_smoothed) | ||
|
||
cv2.waitKey(0) | ||
cv2.destroyAllWindows() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
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()) |
Oops, something went wrong.