如何将线条绘制成numpy数组?

Mar*_*oma 14 python drawing numpy

我希望能够将线条绘制成numpy数组,以获得用于在线手写识别的离线功能.这意味着我根本不需要图像,但我需要在一个numpy数组中的某些位置,给定大小的图像看起来像.

我希望能够指定图像大小,然后绘制这样的笔画:

import module
im = module.new_image(width=800, height=200)
im.add_stroke(from={'x': 123, 'y': 2}, to={'x': 42, 'y': 3})
im.add_stroke(from={'x': 4, 'y': 3}, to={'x': 2, 'y': 1})
features = im.get(x_min=12, x_max=15, y_min=0, y_max=111)
Run Code Online (Sandbox Code Playgroud)

这样的事情很简单(最好直接用numpy/scipy)?

(请注意,我想要灰度插值.所以features应该是[0,255]中的值矩阵.)

Mar*_*oma 16

感谢Joe Kington的回答!我在找skimage.draw.line_aa.

import scipy.misc
import numpy as np
from skimage.draw import line_aa
img = np.zeros((10, 10), dtype=np.uint8)
rr, cc, val = line_aa(1, 1, 8, 4)
img[rr, cc] = val * 255
scipy.misc.imsave("out.png", img)
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考:`pip install scikit-image`. (2认同)

Mar*_*aci 13

我在寻找解决方案时偶然发现了这个问题,所提供的答案很好地解决了这个问题.但是,它并不适合我的目的,我需要一个"可紧张"的解决方案(即在没有显式循环的情况下实现numpy),并且可能使用线宽选项.我最终实现了自己的版本,因为最终它也比line_aa快得多,我想我可以分享它.

它有两种风格,有线宽和没有线宽.实际上前者不是后者的概括,也不完全同意line_aa,但就我的目的而言,它们很好并且在情节上它们看起来还不错.

def naive_line(r0, c0, r1, c1):
    # The algorithm below works fine if c1 >= c0 and c1-c0 >= abs(r1-r0).
    # If either of these cases are violated, do some switches.
    if abs(c1-c0) < abs(r1-r0):
        # Switch x and y, and switch again when returning.
        xx, yy, val = naive_line(c0, r0, c1, r1)
        return (yy, xx, val)

    # At this point we know that the distance in columns (x) is greater
    # than that in rows (y). Possibly one more switch if c0 > c1.
    if c0 > c1:
        return naive_line(r1, c1, r0, c0)

    # We write y as a function of x, because the slope is always <= 1
    # (in absolute value)
    x = np.arange(c0, c1+1, dtype=float)
    y = x * (r1-r0) / (c1-c0) + (c1*r0-c0*r1) / (c1-c0)

    valbot = np.floor(y)-y+1
    valtop = y-np.floor(y)

    return (np.concatenate((np.floor(y), np.floor(y)+1)).astype(int), np.concatenate((x,x)).astype(int),
            np.concatenate((valbot, valtop)))
Run Code Online (Sandbox Code Playgroud)

我之所以称之为"幼稚",是因为它与维基百科中的天真实现非常相似,但是有一些抗锯齿,虽然不完美(例如非常薄的对角线).

加权版本提供更粗的线条更明显的抗锯齿.

def trapez(y,y0,w):
    return np.clip(np.minimum(y+1+w/2-y0, -y+1+w/2+y0),0,1)

def weighted_line(r0, c0, r1, c1, w, rmin=0, rmax=np.inf):
    # The algorithm below works fine if c1 >= c0 and c1-c0 >= abs(r1-r0).
    # If either of these cases are violated, do some switches.
    if abs(c1-c0) < abs(r1-r0):
        # Switch x and y, and switch again when returning.
        xx, yy, val = weighted_line(c0, r0, c1, r1, w, rmin=rmin, rmax=rmax)
        return (yy, xx, val)

    # At this point we know that the distance in columns (x) is greater
    # than that in rows (y). Possibly one more switch if c0 > c1.
    if c0 > c1:
        return weighted_line(r1, c1, r0, c0, w, rmin=rmin, rmax=rmax)

    # The following is now always < 1 in abs
    slope = (r1-r0) / (c1-c0)

    # Adjust weight by the slope
    w *= np.sqrt(1+np.abs(slope)) / 2

    # We write y as a function of x, because the slope is always <= 1
    # (in absolute value)
    x = np.arange(c0, c1+1, dtype=float)
    y = x * slope + (c1*r0-c0*r1) / (c1-c0)

    # Now instead of 2 values for y, we have 2*np.ceil(w/2).
    # All values are 1 except the upmost and bottommost.
    thickness = np.ceil(w/2)
    yy = (np.floor(y).reshape(-1,1) + np.arange(-thickness-1,thickness+2).reshape(1,-1))
    xx = np.repeat(x, yy.shape[1])
    vals = trapez(yy, y.reshape(-1,1), w).flatten()

    yy = yy.flatten()

    # Exclude useless parts and those outside of the interval
    # to avoid parts outside of the picture
    mask = np.logical_and.reduce((yy >= rmin, yy < rmax, vals > 0))

    return (yy[mask].astype(int), xx[mask].astype(int), vals[mask])
Run Code Online (Sandbox Code Playgroud)

重量调整无疑是非常随意的,所以任何人都可以根据自己的喜好进行调整.现在需要rmin和rmax来避免图像外的像素.一个对比:

比较在这里

如你所见,即使w = 1,weighted_line也有点厚,但是以一种同质的方式; 类似地,naive_line略微更薄.

关于基准测试的最后注意事项:在我的机器上,%timeit f(1,1,100,240)针对各种功能运行(对于weighted_line,w = 1)导致line_aa的时间为90μs,对于weighted_line为84μs(尽管时间当然随着权重而增加),18μs为naive_line.再次进行比较,重新实现纯Python中的line_aa(而不是包中的Cython)需要350μs.


And*_*kin 6

我发现此val * 255方法的答案不是很理想,因为它似乎只能在黑色背景上正常工作。如果背景包含较暗和较亮的区域,则似乎不太正确:

在此处输入图片说明

为了使其能够在所有背景上正常工作,必须考虑到抗锯齿线所覆盖的像素的颜色。

这是一个基于原始答案的小演示:

from scipy import ndimage
from scipy import misc
from skimage.draw import line_aa
import numpy as np


img = np.zeros((100, 100, 4), dtype = np.uint8)  # create image
img[:,:,3] = 255                                 # set alpha to full
img[30:70, 40:90, 0:3] = 255                     # paint white rectangle
rows, cols, weights = line_aa(10, 10, 90, 90)    # antialias line

w = weights.reshape([-1, 1])            # reshape anti-alias weights
lineColorRgb = [255, 120, 50]           # color of line, orange here

img[rows, cols, 0:3] = (
  np.multiply((1 - w) * np.ones([1, 3]),img[rows, cols, 0:3]) +
  w * np.array([lineColorRgb])
)
misc.imsave('test.png', img)
Run Code Online (Sandbox Code Playgroud)

有趣的部分是

np.multiply((1 - w) * np.ones([1, 3]),img[rows, cols, 0:3]) +
w * np.array([lineColorRgb])
Run Code Online (Sandbox Code Playgroud)

其中,新颜色是根据图像的原始颜色和线条颜色通过使用anti-alias的值进行线性插值而得出的weights。结果是,橙色线越过两种背景:

在此处输入图片说明

现在,上半部分中围绕线条的像素变暗,而下半部分中围绕像素的像素变