개정판 9dddbb7d
Fixed issue #485: Vectorization
DTI_PID/DTI_PID/SymbolBase.py | ||
---|---|---|
79 | 79 |
self.ocrOption = ocrOption |
80 | 80 |
|
81 | 81 |
def getOcrOption(self): |
82 |
return self.ocrOption |
|
82 |
return self.ocrOption |
|
83 |
|
|
84 |
|
|
85 |
class imgLine(): |
|
86 |
def __init__(self, start, end): |
|
87 |
self.start = start |
|
88 |
self.end = end |
|
89 |
|
|
90 |
def tostring(self): |
|
91 |
return str(self.start.real) + ',' + str(self.start.imag) + ' ' + str(self.end.real) + ',' + str(self.end.imag) |
DTI_PID/DTI_PID/potrace.py | ||
---|---|---|
3 | 3 |
|
4 | 4 |
import numpy as np, cv2, subprocess |
5 | 5 |
import os |
6 |
from xml.dom import minidom |
|
7 |
import svg.path |
|
8 |
from xml.etree.ElementTree import Element, SubElement, dump, ElementTree |
|
9 |
import SymbolBase |
|
6 | 10 |
|
7 | 11 |
# potrace command |
8 | 12 |
POTRACE = os.path.dirname(os.path.realpath(__file__)) + '\\potrace.exe' |
9 |
|
|
10 |
def passpotrace(image): |
|
13 |
|
|
14 |
# extract line from image by using potrace |
|
15 |
def passpotrace(imgFile): |
|
16 |
image = cv2.imread(imgFile, cv2.IMREAD_GRAYSCALE) |
|
17 |
|
|
11 | 18 |
# convert to bmp binary so that potrace can handle it |
12 | 19 |
retval, buf = cv2.imencode('.bmp', image) |
13 | 20 |
if retval == False: |
... | ... | |
32 | 39 |
if len(stderr) != 0: |
33 | 40 |
raise RuntimeError('Potrace threw error:\n' + stderr.decode('utf-8')) |
34 | 41 |
|
35 |
return stdout |
|
42 |
imgLines = [] |
|
43 |
# parse svg data and extract lines |
|
44 |
svgdom = minidom.parseString(stdout) |
|
45 |
pathstrings = [path.getAttribute('d') for path in svgdom.getElementsByTagName('path')] |
|
46 |
for pathstring in pathstrings: |
|
47 |
pathdata = svg.path.parse_path(pathstring) |
|
48 |
for segment in pathdata: |
|
49 |
if type(segment) is svg.path.Line: |
|
50 |
imgline = SymbolBase.imgLine(segment.start, segment.end) |
|
51 |
imgLines.append(imgline.tostring()) |
|
52 |
# up to here |
|
53 |
|
|
54 |
return imgLines |
|
36 | 55 |
|
37 | 56 |
if __name__ == '__main__': |
38 | 57 |
execpath = os.path.dirname(os.path.realpath(__file__)) |
39 | 58 |
|
40 |
testpic = cv2.imread(execpath + '\\res\\UY1-K-2007_P1_300dpi_black.png', cv2.IMREAD_GRAYSCALE) |
|
41 |
svg = passpotrace(testpic).decode('utf-8') |
|
42 |
|
|
59 |
imgLines = passpotrace(execpath + '\\res\\UY1-K-2007_P1_300dpi_black.png') |
|
43 | 60 |
|
44 |
file = open(execpath + '\\res\\UY1-K-2007_P1_300dpi_black.svg', 'w')
|
|
45 |
file.write(svg)
|
|
61 |
file = open(execpath + '\\res\\lines.svg', 'w')
|
|
62 |
file.writelines(''.join(imgLines))
|
|
46 | 63 |
file.close() |
내보내기 Unified diff