【问题标题】:Python: how to create a submatrix discretizing a circle?Python:如何创建一个离散圆的子矩阵?
【发布时间】:2016-02-20 11:23:13
【问题描述】:

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

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

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

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

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

编辑

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

【问题讨论】:

  • 这可以让我产生截断的圆圈吗?当圆心在边界上或越过边界时,它们就会出现,但仍有空间绘制圆的一部分。
  • 也许至少阅读他链接的文章的内容部分,这将回答您的问题。您还可以制作更大的网格,绘制完整的圆圈并在之后截断。

标签: python numpy multidimensional-array grid shapes


【解决方案1】:

这个函数填充了一个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 值,然后 为切片(视图)分配晶格值

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 2022-08-17
    • 2021-11-16
    • 2014-10-21
    • 2017-05-22
    • 1970-01-01
    • 2020-01-29
    相关资源
    最近更新 更多