Chinese vector word stock generation method based on Potrace algorithm

文档序号:1156518 发布日期:2020-09-15 浏览:11次 中文

阅读说明:本技术 基于Potrace算法的中文矢量字库生成方法 (Chinese vector word stock generation method based on Potrace algorithm ) 是由 王存睿 江子傲 于 2020-06-04 设计创作,主要内容包括:基于<I>Potrace</I>算法的中文矢量字库生成方法,其属于矢量字库生成的技术领域。该方法通过对<I>Potrace</I>算法中的拟合多边形和贝塞尔曲线片段进行优化和改进,对字体曲线轮廓进行高质量的布点,然后将中文字体图像进行矢量化,生成<I>svg</I>矢量文件。原有的<I>Potrace</I>算法生成的结果质量是,细节处字形轮廓上往往布满了冗余的控制点。本方法产生的字体是逼真的高质量结果,在视觉外观上显示出优于其他方法的优势。且轮廓的曲线线条光顺性高,可以精确捕捉整体特征以及局部细节,具有很高的精确度。同时本方法不仅减少了路径点的数量,而且保留了轮廓的关键风格细节。(Based on Potrace A Chinese vector word stock generation method of an algorithm belongs to the technical field of vector word stock generation. The method comprises the steps of Potrace Optimizing and improving the fitting polygon and Bezier curve segment in the algorithm, performing high-quality point distribution on the font curve outline, and performing vectorization on the Chinese font image to generate svg A vector file. Original one Potrace The quality of the result generated by the algorithm is that redundant control points are always distributed on the outline of the glyph at the details. The characters produced by the method are vivid and high-quality results and are better than other characters in visual appearanceThe advantages of the method. And the curve line smoothness of the contour is high, the overall characteristics and the local details can be accurately captured, and the accuracy is high. Meanwhile, the method not only reduces the number of path points, but also reserves the key style details of the contour.)

1. The method for generating the Chinese vector word stock based on the Potrace algorithm is characterized by comprising the following steps of:

s1, designing a font image: designing needed fonts in a font library, making and uniformly storing pictures in a bmp format, and naming each font image by using corresponding unicode codes in the font library;

s2, carrying out binarization and denoising on the font picture: setting a global threshold value, scanning an image, and performing binarization processing on the image; filtering the binary image by using morphological operation and anisotropic diffusion, and finally rasterizing the binarized image into a pixel lattice;

s3, path decomposition: randomly selecting a pair of adjacent pixels with different colors, rotating the image to enable the selected black pixels to be on the left side and white pixel blocks to be on the right side; selecting a path with the length of 1 as a starting path, moving and expanding along the gap of two pixels, and keeping each new edge to have a black pixel on the left side and a white pixel on the right side; the color selection direction of the surrounding pixels is judged to move forward, and the color selection direction continuously returns to the initial vertex; the pixels are converted into closed paths in the mode, and the algorithm is applied recursively to convert all the images into closed paths;

s4, polygon fitting path: fitting each segment of closed path by using the linear segments, constructing a polygon with an index sequence by using the linear segments, and then selecting an optimal polygon;

simplifying the problem of finding an optimal polygon into a problem of finding an optimal ring in a directed graph; using a standard graph theory algorithm to find the optimal ring shape in the directed graph; finding an optimal ring within the time complexity o (nm) by computing a graph, where n is the size of the input path and m is the longest possible segment length;

s5, converting the polygon into a vector outline: converting the polygon in the previous step into a smooth vector outline, adjusting the position of the vertex of the polygon to enable the polygon to be close to a source bitmap, calculating a corner and a curve according to the lengths of adjacent line segments and included angles among the adjacent line segments, and finally fitting by using a cubic Bezier curve;

after corner detection and smoothing, outputting a curve formed by a Bezier curve segment and a straight line segment, and optimizing the number of curves by connecting adjacent Bezier curve segments; firstly, connecting adjacent convex consistent curve segments, then connecting curve segments with the total direction change less than 179 degrees, obtaining a new Bezier curve segment C by connecting the short curve segments, defining a numerical penalty for the curve segment C to judge whether the approximate fitting error of the segment to the original contour is in an acceptable range, and finally converting the shortest polygon path obtained by using a graph theory algorithm into a curve segment set with the approximate error in the acceptable range, thereby completing the fitting of the polygon contour;

s6, writing the vector curve outline into a path in an svg format to generate an svg vector diagram;

and S7, reading and analyzing the svg file, writing the svg data of all fonts into a font library in the TrueType format, and storing the data.

Technical Field

The invention relates to a vector font generation technology, in particular to a Chinese vector font generation method based on Potrace algorithm improvement.

Background

The vector is a straight line segment having a length, a direction, and a position. The outline around the stroke of a Chinese character can be regarded as a curve, the curve can be approximately expressed by a group of vectors which are connected end to end, and the font in the curve font library describes the outline of a Chinese character image by adopting straight lines and curves. Compared to bitmap fonts, vector fonts are defined as a set of parametric curves, can be scaled to any size, and are easier to convert than bitmap fonts by some digital processing. Therefore, we need to convert chinese characters from bitmap fonts to vector fonts when making a word stock.

At present, the output of the Chinese character font library mainly adopts a curve vector font library, such as common Truetype and opentype font libraries, which describe the outline of a font image through a vector curve. In the process of making the curve font library, the vectorization of font images is a crucial step, and the output quality of the whole font library is directly determined. Because the existing vectorization algorithm can not meet the requirements and quality requirements of the current font design industry for a while, most of the current commercial font products still rely heavily on manual layout of font images by designers and manual adjustment to complete the vectorization work of the font images. The production efficiency is low, so the research on a method for automatically generating the high-quality vector word stock has important practical significance.

Disclosure of Invention

In order to solve the problems in the prior art, the method for vectorizing the font image based on Potrace algorithm improvement is provided, and comprises the steps of optimizing and improving a fitting polygon and a Bezier curve segment in the Potrace algorithm, carrying out high-quality point distribution on the font curve outline, vectorizing the Chinese font image to generate an svg vector file, and finally generating a complete Chinese font vector font library.

In order to achieve the purpose, the technical scheme of the application is as follows: a Chinese font vector font generation method based on Potrace algorithm improvement comprises the following steps:

firstly, designing a font image: designing needed fonts in a font library, making pictures in a bmp format for unified storage, and naming each font image by using corresponding unicode codes in the font library;

secondly, carrying out binarization and denoising on the font picture: setting a global threshold value, scanning an image, and performing binarization processing on the image; the binary image is filtered with morphological operations and anisotropic diffusion with the aim of reducing noise without removing important parts of the image content. Finally, the binarized picture is rasterized into a pixel lattice;

thirdly, path decomposition: a pair of differently colored but adjacent pixels are randomly selected and the image is rotated so that the selected black pixel is on the left and the white pixel block is on the right. A path of length 1 is chosen as the starting path, moving the extension along the slit of two pixels, keeping each new edge with black pixels on its left and white pixels on its right. The color selection direction of the surrounding pixels is determined to advance, and the process is continued to return to the initial vertex. In this way, the pixels are converted into closed paths, and the image is entirely converted into closed paths by applying the above algorithm recursively.

Step four, polygon fitting path: fitting each segment of closed path by using the linear segments, constructing a polygon with an index sequence by using the linear segments, and then selecting an optimal polygon;

fifthly, converting the polygon into a vector outline: and (3) converting the polygon in the last step into a smooth vector contour, adjusting the positions of the vertexes of the polygon to enable the polygon to be close to a source bitmap as much as possible, calculating a corner and a curve according to the lengths of adjacent line segments and included angles among the adjacent line segments, and finally completing the fitting of the polygon contour by using a cubic Bessel curve.

And a sixth step: and writing the vector curve outline into a path in an svg format, and further generating an svg vector diagram.

The seventh step: and reading and analyzing the svg file, writing the svg data of all fonts into a font library in the TrueType format, and storing.

Further, in order to make the vector font generation effect better in the present application, that is, the number of contour segments is less, in the fourth step, the problem of finding an optimal polygon is reduced to finding an optimal ring in the directed graph. A standard graph theory algorithm is used to find the optimal ring in the directed graph. By calculating the graph, the optimal ring is found within the time complexity o (nm), where n is the size of the input path and m is the longest possible segment length.

Further, in the fifth step, after corner detection and smoothing, a curve composed of a bezier curve segment and a straight line segment is output, and the number of curves is optimized by connecting adjacent bezier curve segments; firstly connecting adjacent convex consistent curve segments, then connecting curve segments with the total direction change less than 179 degrees, obtaining a new Bezier curve segment C by connecting the short curve segments, defining a numerical penalty for the curve segment C to judge whether the approximate fitting error of the segment to the original contour is in an acceptable range, and finally converting the shortest polygon path obtained by using a graph theory algorithm into a curve segment set with the approximate error in the acceptable range according to the process, thereby completing the fitting of the polygon contour.

The invention has the beneficial effects that: according to the method, a fitted polygon and a Bezier curve segment in a Potrace algorithm are optimized and improved, high-quality point distribution is carried out on a font curve outline, and then a Chinese font image is vectorized to generate an svg vector file. The quality of the result generated by the original Potrace algorithm is that redundant control points are always distributed on the outline of the glyph at the details. The fonts produced by the method are vivid high-quality results and show advantages over other methods in visual appearance. And the curve line smoothness of the contour is high, the overall characteristics and the local details can be accurately captured, and the accuracy is high. Meanwhile, the method not only reduces the number of path points, but also reserves the key style details of the contour.

Drawings

Fig. 1 is a flow chart of the present application.

Fig. 2 is a schematic diagram of the algorithm of the present application.

Fig. 3 is a font vectorization effect diagram of the present application.

Detailed Description

The technical solutions in the embodiments of the present invention will be described clearly and completely with reference to the accompanying drawings in the embodiments of the present invention, and it should be understood that the described examples are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments of the present invention without making any creative effort, shall fall within the protection scope of the present invention.

The invention provides a method for compressing Chinese character font images provided by designers into vector data through an improved Potrace image vectorization algorithm and automatically generating a vector font library. After the font image is subjected to binarization and denoising, the font image is converted into a vector path by optimizing polygons and curve segments in a Potrace algorithm, and the vector path is written into a curve font library.

The flow chart of the invention is shown in figure 1, and the principle is shown in figure 2, and the specific implementation steps are as follows:

the method comprises the steps of carrying out binarization and denoising on font pictures designed by designers, setting a global threshold value, scanning images, carrying out binarization processing on the images, and filtering the binary images by using morphological operation and anisotropic diffusion, wherein the purpose of the method is to reduce noise under the condition of not removing important parts of image contents.

The bitmap image is placed on the coordinate system such that the corners of each pixel have integer coordinates, the background color of the image is set to white, the foreground color is set to black, and the portion of the coordinate plane outside the bitmap boundary is filled with white pixels. A directed graph is constructed by the following steps, assuming that p is an integer coordinate point, with p adjacent to four pixels. If the color of four pixels is not exactly the same, there is a point where the color of one pixel is different from the color of the other three pixels, which is called a corner. If the euclidean distance of two inflection points in the image is 1, there are two vertex-connected edges, and if a straight line segment from vertex v to vertex w separates a black pixel from a white pixel, the black pixel is located on the left side and the white pixel is located on the right side thereof when the direction of the line segment is from vertex v to vertex w. If a path is defined by a series of inflection points v0,...,vnI for all points i ═ i }N-1 all have one side from viTo vi+1. If v isn=v0Since the euclidean distance between two pixels is 1, the length of the obtained path is the number of sides. I.e. the goal of the path decomposition is to find a closed path in all corners so that each edge appears only once.

To make the curve more approximate to a polygon, at point aiA unit square is drawn. Then find parallel to

Figure BDA0002523355750000062

Line L ofiAnd in aiIs brought into contact with the square and is as close to a straight line as possible

Figure BDA0002523355750000061

Let c be LiAnd

Figure BDA0002523355750000063

and γ is

Figure BDA0002523355750000064

Length of (1) andthe quotient of (a). Order toAnd consider Bezier curve linkage bi-1And biUsing the parameter α, tangent to the curve at bi-1ai,Li,aibiThese 3 lines.

Finally using the calculated parameters α for corner detection decisionsi-1To biIf α ≦ 1, then we draw a smooth Bezier curve at this vertex, if α > 1, there is no convex Bezier curve to link bi-1And biAnd tangent to Li. A corner is detected and passes through two intersections at point aiLinear segment link of (b)i-1And biα is adjustedThe integers between 0.55 and 1, α > 0.55, are to prevent the curve from being too "flat", α < 1 to ensure that the resulting Bezier curve is convex.

Assuming such a curve C, it must be compared withAndtangency, then one can findAndthe intersection O of the two curves, leaving only one degree of freedom in the curve to be considered, i.e., the parameter α. if we add a constraint-the curve C is connected with

Figure BDA00025233557500000611

The area enclosed and the original curve segment andthe enclosed areas must be equal and the parameter α can be determined, resulting in a unique Bezier curve C that is a candidate for approximating a given line segment.

This approximately given curve segment C is then checked for acceptability and, if so, given a numerical penalty. Here a simple tangency check is used, for each i 1iThe tangent at the position thereof will be parallel to

Figure BDA0002523355750000071

Let diIs ziSegment to lineEuclidean distance of. Then, for each i ═ 1.,. n, find the point z 'on C'iTangent to, on CParallel toAnd d 'to'iIs z'iTo line segment LiEuclidean distance of, if z'iAnd aiAt LiOn the same side, it is marked as positive, otherwise it is negative.

If d isi≤,z′iIs > -, and ziOn-line

Figure BDA0002523355750000074

Is located at aiAnd aj+1In between, this approximation is considered acceptable. The value is a constant value and is called the tolerance of the curve optimization algorithm; its preset is 0.2, which can be changed with the- -opttolerance command line option.

As shown in fig. 3, the present method can produce realistic, high quality results, showing advantages over others in visual appearance. The original Potrace algorithm is capable of vectorizing low resolution images, but the result is low quality. When the detail is enlarged, redundant control points are often distributed on the outline of the glyph, as shown in fig. 3(a), and the smoothness of the curve line of the outline is low, so that the overall feature and the local detail cannot be accurately captured, and the accuracy is poor.

In contrast, the method provided by the application not only reduces the number of path points, but also retains the key style details of the contour. In addition to visual appearance, the method of the present application is also superior to other methods in vectorization quality.

7页详细技术资料下载
上一篇:一种医用注射器针头装配设备
下一篇:流程图生成方法、装置、计算机设备和存储介质

网友询问留言

已有0条留言

还没有人留言评论。精彩留言会获得点赞!

精彩留言,会给你点赞!