繁体   English   中英

将 plot 转换为 1 和 0 的数组

[英]Convert plot into an array of 1s and 0s

我想将绘制形状并填充它们的 plot 转换为形状为 1 且空白为 0 的数组。 我在下面包含了 plot 的代码。 我可以以某种方式使用 numpy 来做到这一点吗?

PLOT 代码:

import matplotlib.pyplot as plt

#define plot window size
plt.axis([0, 280, 0, 270])

#define start point (x,y)
x_start=20
y_start=250
plt.plot(x_start,y_start,'ro')
plt.text(x_start,y_start,'START')

#define end point (x,y)
x_end=265
y_end=20
plt.plot(x_end,y_end,'ro')
plt.text(x_end,y_end,'END')

#define, display, and connect vertices of polygon1
x1 = [175,270,200,130,175]
y1 = [150,200,220,180,150]
#plt.scatter(x1,y1) #plot vertices
plt.plot(x1,y1,'b') #connect vertices with lines
plt.fill(x1, y1, 'b') #fill blue

#define, display, and connect vertices of polygon2
x2 = [50,100,150,50]
y2 = [255,150,230,255]
#plt.scatter(x2,y2)
plt.plot(x2,y2,'b')
plt.fill(x2, y2, 'b')

#define, display, and connect vertices of polygon3
x3 = [5,50,50,5,5]
y3 = [210,210,25,25,210]
#plt.scatter(x3,y3,)
plt.plot(x3,y3,'b')
plt.fill(x3,y3, 'b')

#define, display, and connect vertices of polygon4
x4 = [60,140,190,160,80,60]
y4 = [110,155,80,10,45,110]
#plt.scatter(x4,y4)
plt.plot(x4,y4,'b')
plt.fill(x4, y4, 'b')

#define, display, and connect vertices of polygon5
x5 = [280,255,185,280]
y5 = [160,60,20,160]
#plt.scatter(x5,y5)
plt.plot(x5,y5,'b')
plt.fill(x5, y5, 'b')

#display
plt.show()

不确定您实际上要整体做什么,但是绘制本质上是矢量图的内容,然后将其栅格化到网格上并将其转换为图像,然后希望 dpi 可以正常工作并解码图像不是理想的方法。

我可以建议您改用scikit-image polygon()吗?

#!/usr/bin/env python3

import numpy as np
from skimage.draw import polygon

# Set height and width
h, w = 10, 12

# Create blank canvas
img = np.zeros((h, w), dtype=np.uint8)

# Define polygon
r = np.array([1, 8, 1])
c = np.array([1, 1, 10])

# Draw it
rr, cc = polygon(r, c)
img[rr, cc] = 1
print(img)

样品 Output

[[0 0 0 0 0 0 0 0 0 0 0 0]
 [0 1 1 1 1 1 1 1 1 1 1 0]
 [0 1 1 1 1 1 1 1 1 0 0 0]
 [0 1 1 1 1 1 1 1 0 0 0 0]
 [0 1 1 1 1 1 1 0 0 0 0 0]
 [0 1 1 1 1 0 0 0 0 0 0 0]
 [0 1 1 1 0 0 0 0 0 0 0 0]
 [0 1 1 0 0 0 0 0 0 0 0 0]
 [0 1 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM