如何将简单的几何形状写入numpy数组

时间:2012-04-05 15:32:42

标签: python image numpy geometry

我想生成一个200x200元素的numpy数组,并在其中放入一个以100,100坐标为中心的圆,半径为80,笔画宽度为3像素。如何在python 2.7中执行此操作而不涉及文件操作?可能使用几何或成像库来推广其他形状。

5 个答案:

答案 0 :(得分:37)

通常的方法是定义坐标网格并应用形状方程。要做到这一点,最简单的方法是使用numpy.mgrid

http://docs.scipy.org/doc/numpy/reference/generated/numpy.mgrid.html

# xx and yy are 200x200 tables containing the x and y coordinates as values
# mgrid is a mesh creation helper
xx, yy = numpy.mgrid[:200, :200]
# circles contains the squared distance to the (100, 100) point
# we are just using the circle equation learnt at school
circle = (xx - 100) ** 2 + (yy - 100) ** 2
# donuts contains 1's and 0's organized in a donut shape
# you apply 2 thresholds on circle to define the shape
donut = numpy.logical_and(circle < (6400 + 60), circle > (6400 - 60))

答案 1 :(得分:20)

Cairo是一个现代,灵活,快速的2D图形库。它有Python bindings并允许创建&#34;曲面&#34;基于NumPy数组:

import numpy
import cairo
import math
data = numpy.zeros((200, 200, 4), dtype=numpy.uint8)
surface = cairo.ImageSurface.create_for_data(
    data, cairo.FORMAT_ARGB32, 200, 200)
cr = cairo.Context(surface)

# fill with solid white
cr.set_source_rgb(1.0, 1.0, 1.0)
cr.paint()

# draw red circle
cr.arc(100, 100, 80, 0, 2*math.pi)
cr.set_line_width(3)
cr.set_source_rgb(1.0, 0.0, 0.0)
cr.stroke()

# write output
print data[38:48, 38:48, 0]
surface.write_to_png("circle.png")

此代码打印

[[255 255 255 255 255 255 255 255 132   1]
 [255 255 255 255 255 255 252 101   0   0]
 [255 255 255 255 255 251  89   0   0   0]
 [255 255 255 255 249  80   0   0   0  97]
 [255 255 255 246  70   0   0   0 116 254]
 [255 255 249  75   0   0   0 126 255 255]
 [255 252  85   0   0   0 128 255 255 255]
 [255 103   0   0   0 118 255 255 255 255]
 [135   0   0   0 111 255 255 255 255 255]
 [  1   0   0  97 254 255 255 255 255 255]]

显示圆圈的一些随机片段。它还创建了这个PNG:

Red circle

答案 2 :(得分:6)

opencv new python bindings import cv2创建numpy数组作为默认图像格式

它们包括drawing functions

答案 3 :(得分:4)

另一种可能性是使用scikit-image。您可以使用circle_perimeter表示空心,或circle表示整圆。

您可以像这样画一个笔画圈:

import matplotlib.pyplot as plt
from skimage import draw
arr = np.zeros((200, 200))
rr, cc = draw.circle_perimeter(100, 100, radius=80, shape=arr.shape)
arr[rr, cc] = 1
plt.imshow(arr)
plt.show()

您还可以使用loop模拟笔画。在这种情况下,您应该使用消除锯齿的版本来避免伪影:

import matplotlib.pyplot as plt
from skimage import draw
arr = np.zeros((200, 200))
stroke = 3
# Create stroke-many circles centered at radius. 
for delta in range(-(stroke // 2) + (stroke % 2), (stroke + 1) // 2):
    rr, cc, _ = draw.circle_perimeter_aa(100, 100, radius=80+delta, shape=arr.shape)
    arr[rr, cc] = 1
plt.imshow(arr)
plt.show()

一种可能更有效的方法是生成两个完整的圆圈并从外部圆圈“减去”内部:

import matplotlib.pyplot as plt
from skimage import draw
arr = np.zeros((200, 200))
stroke = 3
# Create an outer and inner circle. Then subtract the inner from the outer.
radius = 80
inner_radius = radius - (stroke // 2) + (stroke % 2) - 1 
outer_radius = radius + ((stroke + 1) // 2)
ri, ci = draw.circle(100, 100, radius=inner_radius, shape=arr.shape)
ro, co = draw.circle(100, 100, radius=outer_radius, shape=arr.shape)
arr[ro, co] = 1
arr[ri, ci] = 0
plt.imshow(arr)
plt.show()

这两种方法实际上产生了略微不同的结果。

Circle with <code>stroke=3</code>

答案 4 :(得分:0)

仅使用numpy(类似于@Simon的答案)的一种方法如下:

import numpy as np

def draw_circle(radius, dim=None):
    if dim == None:
        dim = (radius * 2, radius * 2)
    circle = np.zeros(dim)
    x, y = np.meshgrid(np.arange(dim[0]), np.arange(dim[1]))
    r = np.abs((x - dim[0] / 2)**2 + (y - dim[1] / 2)**2 - radius**2)

    m1 = r.min(axis=1, keepdims=True)
    m2 = r.min(axis=0, keepdims=True)
    rr = np.logical_or(r == m1, r == m2)
    l_x_lim = int(dim[0] / 2 - radius)
    u_x_lim = int(dim[0] / 2 + radius + 1)
    l_y_lim = int(dim[0] / 2 - radius)
    u_y_lim = int(dim[0] / 2 + radius + 1)

    circle[l_x_lim:u_x_lim, l_y_lim:u_y_lim][rr[l_x_lim:u_x_lim, l_y_lim:u_y_lim]] = 1
    return circle

gen_circle(20) # draw a circle of radius 20 pixels