2012-04-05 74 views
22

我想生成一個大小爲200x200的元素的numpy數組,並放入一個以100,100座標,半徑80和筆劃寬度爲3像素爲中心的圓。如何在不涉及文件操作的情況下在python 2.7中執行此操作?可能使用幾何或圖像庫來推廣到其他形狀。如何將簡單的幾何形狀寫入numpy數組

回答

17

Cairo是一個現代化的,靈活和快速的2D圖形庫。它有Python bindings和允許創建基於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

+0

由於我使用灰度(8位)數據I使用以下內容:\ data = numpy.zeros((200,200),dtype = numpy.uint8)\ surface = cairo.ImageSurface.create_for_data(data,cairo.FORMAT_A8,200,200)\ #繪製50%灰色表面(使用alpha值)\ cr.set_source_rgba(0,0,0,0.5) – a1an 2012-04-10 08:51:53

+0

如果有人試圖將此示例與cairocffi庫一起使用,它將無法工作。下面是關於cairocffi repo的一個問題,詳細介紹如下:https:// github。com/Kozea/cairocffi/issues/51 – neelshiv 2016-09-28 01:03:17

6

OpenCV的新的python綁定import cv2創建numpy的數組作爲默認的圖像格式

它們包括drawing functions

+0

輸出數組有什麼格式?只是座標列表或二進制網格? – 2012-06-22 14:43:26

+0

這是一個圖像=即通常是一個2D的numpy數組,你可以對它們進行所有正常的numpy操作並以任何圖像格式保存 – 2012-06-22 14:46:56

+0

非常感謝您的更新@Martin – 2012-06-22 14:56:35

31

的常用方法是定義一個座標網格並應用形狀的方程。要做到這一點最簡單的方法是使用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)) 
+0

我從來不知道numpy能做到這一點! – 2012-04-06 14:47:57

+2

@MartinBeckett mgrid允許您評估一系列值的函數。請注意,您不限於2個維度。 – Simon 2012-04-06 15:11:40

+2

在附註中,我發現'donut =(circle <(6400 + 60))&(circle(6400 - 60))'比明確調用'logical_and'更可讀。不過,這是個人喜好的問題。他們完全相同。 (注意,'&'會調用'numpy.logical_and',儘管'and'不能被覆蓋。) – 2012-04-06 17:13:16

2

另一種可能性是使用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>