gpt4 book ai didi

Python:如何创建一个离散化圆的子矩阵?

转载 作者:行者123 更新时间:2023-12-01 04:06:21 24 4
gpt4 key购买 nike

在充满的二维方形网格(矩阵)中,我需要创建一个充满1的子矩阵,并且该子矩阵的形状尽可能接近到一个圆圈。我知道当您使用单元格或像素时无法精确表示圆,因此我的目标是离散化圆。

我能想到的最好的办法是这段代码,它生成方形子矩阵(下图中的蓝色方形):

from __future__ import division 
import numpy
import matplotlib.pyplot as plt
import matplotlib.colors as mc
import random
import os
import math

n=101 #Grid size
empty_lattice=numpy.zeros((n,n)) #The empty 2D grid
x=int(numpy.random.uniform(0,n-1)) #X coord. top left corner
y=int(numpy.random.uniform(0,n-1)) #Y coord. top left corner
side=int(numpy.random.uniform(15,n)) #Side of the square approximating the circle
max_y=n-y #Checks the distance between the y of the submatrix origin and the matrix vertical boundary
max_x=n-x #Checks the distance between the x of the submatrix origin and the matrix horizontal boundary
max_width=0 #Initializes a maximum width for the loading submatrix
if max_y<max_x: #This assigns the minimum value between max_y and max_x to max_width, so that the submatrix is always a square
max_width=max_y
else:
max_width=max_x
if side>max_width:
for i in range(0,max_width):
for j in range(0, max_width):
empty_lattice[x+i][y+j]=1
else:
for i in range(0, side):
for j in range(0, side):
empty_lattice[x+i][y+j]=1

现在,在视觉上这会转化为下图,但正如您所知,蓝色正方形和内切圆在面积方面存在明显差异: enter image description here

我的问题:我如何修改我的代码,以便能够“平滑”正方形的角,以便出现类似圆形的东西?

编辑

即使圆圈不完全位于网格边界内(查看图像),也应该绘制圆圈。

最佳答案

这个函数填充了一个由 1 组成的圆圈,看起来相当不错。

def fill_cell(cell, corner, rad):
m, n = cell.shape
ctr = corner[0]+m/2, corner[1]+n/2
x = np.arange(m) - ctr[0]
y = np.arange(n) - ctr[1]
X,Y = np.meshgrid(x, y, order='ij') # could try order='xy'
Z = ((X**2 + Y**2)<= rad**2).astype(cell.dtype)
return Z
empty_lattice[:] = fill_cell(empty_lattice, (x,y),side/2)

empty_lattice 中的位置不正确 - 因为您定义 x,y 坐标的方式与我的假设不同,但我认为您可以解决这个问题。

半径看起来不错,尽管它可能会偏离一个整数。

要填充多个圆圈,您可以迭代 x,y 值,然后为切片( View )分配晶格值

xyslice = slice(x,15), slice(y,15)
empty_lattice[xyslice] = fill_cell(empty_lattice[xyslice],...)

对于重叠的圆圈,我会研究某种逻辑分配

empty_lattice[xyslice] |= fill_cell(...)

关于Python:如何创建一个离散化圆的子矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35522530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com