Python中的矩形可视化

2024-06-16 10:11:10 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我有一个10x10的网格。关于如何在其中可视化2025个矩形有什么想法吗? 我的意思是,我想把它们放在一页(也许是一张图片)上,但我不知道该用哪一个库。 我想用alphas来形象化所有的东西,比如当彼此之间有更多的边界时,我也想形象化。我是说我不知道怎么做。我不需要给我代码或任何东西,我只想要一个库名称使用。你知道吗

编辑: 我的矩形是在一个有4个元组的列表中定义的,其中一个元组有一个角的x,y坐标,比如:[(x, y), (x2, y), (x, y2), (x2, y2)]

编辑2: 现在我的代码是:

multipler = 100
canvasH = 10 * multipler  # 10 == Y in squares.py
canvasW = 10 * multipler  # 10 == X in squares.py

img = numpy.zeros((canvasH, canvasW, 3), numpy.uint8)
# img = cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)

rectangles = [[(0, 0), (1, 0), (0, 1), (1, 1)], [(0, 0), (1, 0), (0, 2), (1, 2)], [(0, 0), (1, 0), (0, 3), (1, 3)]...]

for rectangle in rectangles:
    cv2.rectangle(img, (rectangle[0][0] * multipler, rectangle[0][1] * multipler), (rectangle[3][0] * multipler, rectangle[3][1] * multipler), (206, 220, 242))

cv2.imshow('img', img)

cv2.waitKey(0)
cv2.destroyAllWindows()

但是我只看到9x9张照片,所以我不能看到所有的照片(我有2025张照片)。你知道吗

谢谢你, 马西


Tags: 代码in编辑imgcv2照片元组x2
1条回答
网友
1楼 · 发布于 2024-06-16 10:11:10

你的问题有很多未知数。这是一种可能的解释。你知道吗

一些评论:

  • 有许多库可以绘制矩形。Matplotlib经常用于可视化,并且有一个方便的^{}函数。你知道吗
  • 存在许多颜色表示法,例如,对于红-绿-蓝(更多信息here),由0到1之间的3个值表示。你知道吗
  • α可以作为第四个元素合并,也可以单独给出。你知道吗
  • 如果只想看到边,请将facecolor设置为“无”。你知道吗
  • 无需缩放值。matplotlib等库通过设置dpi(每英寸点数,标准值:100、300、600)自动执行此操作
  • 如果您不想只在屏幕上使用结果,savefig可以保存为多种格式。png“最适合纯图像格式。”。svg对于高质量的矢量格式来说很有趣。你知道吗
  • 最终的结果现在看起来像一幅抽象画,但可以作为探索的起点。你知道吗
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
import random

rectangles = []
for _ in range(2025):
    x = random.uniform(0.0, 10.0)
    y = random.uniform(0.0, 10.0)
    w = random.uniform(0.1, 1.0)
    h = random.uniform(0.1, 1.0)
    rectangles.append([(x,y), (x+w,y), (x,y+h), (x+w,y+h)])

color = [c/256 for c in (206, 220, 242)] # rgb values between 0 and 1
fig, ax = plt.subplots(figsize=(10,10))
ax.set(xlim=(0, 10), ylim=(0, 10))
ax.axis('off')

for rectangle in rectangles:
    xy = rectangle[0]
    width = rectangle[3][0] - rectangle[0][0]
    height = rectangle[3][1] - rectangle[0][1]
    rect = Rectangle(xy, width, height, linewidth=1, edgecolor='none', facecolor=color, alpha=0.3)
    ax.add_patch(rect)

plt.savefig("test.png", bbox_inches='tight', dpi=300)
plt.show()

resulting image

PS1:对于你在评论中关于网格上重叠的许多矩形的附加问题,你可以画一个由覆盖每个单位正方形的矩形的数量着色的图像。一些示例代码:

from matplotlib import pyplot as plt

rectangles = [[(0, 0), (1, 0), (0, 1), (1, 1)], [(0, 0), (1, 0), (0, 2), (1, 2)], [(0, 0), (1, 0), (0, 3), (1, 3)],
              [(0, 0), (1, 0), (0, 4), (1, 4)], [(0, 0), (1, 0), (0, 5), (1, 5)], [(0, 0), (1, 0), (0, 6), (1, 6)],
              [(0, 0), (1, 0), (0, 7), (1, 7)], [(0, 0), (1, 0), (0, 8), (1, 8)], [(0, 0), (1, 0), (0, 9), (1, 9)],
              ... ]

grid = [[0 for y in range(10)] for x in range(10)]
for r in rectangles:
    for x in range(r[0][0], r[3][0] + 1):
        for y in range(r[0][1], r[3][1] + 1):
            grid [x][y] += 1

plt.imshow(grid, cmap='magma_r')
plt.show()

PS2:以10x10网格上几乎所有整数坐标的矩形为例,一个想法是只画边,使用随机颜色区分并随机移动所有点,这样它们就不再重叠。即便如此,仍然有大量几乎重叠的矩形。你知道吗

from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib import cm
import random

rectangles = ...

fig, ax = plt.subplots(figsize=(10,10))
ax.set(xlim=(0, 11), ylim=(0, 11))
ax.axis('off')
cmap = cm.get_cmap('rainbow')
for rectangle in rectangles:
    x0 = rectangle[0][0] + random.uniform(-.45, .45)
    y0 = rectangle[0][1] + random.uniform(-.45, .45)
    x1 = rectangle[3][0] + random.uniform(-.45, .45)
    y1 = rectangle[3][1] + random.uniform(-.45, .45)
    width = x1-x0
    height = y1-y0
    rect = Rectangle((x0, y0), width, height, linewidth=1,
                     facecolor='none', edgecolor=cmap(random.uniform(0,1)), alpha=0.5)
    ax.add_patch(rect)
plt.savefig("test.png", bbox_inches='tight', dpi=300)
plt.show()

相关问题 更多 >