簡體   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