【问题标题】:Filling pixels under or above some function在某些功能下方或上方填充像素
【发布时间】:2021-04-03 00:39:25
【问题描述】:

似乎是一个简单的问题,但我就是想不通。

我有一个配置文件,我在其中声明了一些函数。它看起来像这样:

"bandDefinitions" : [
    {
        "0": ["x^2 + 2*x + 5 - y", "ABOVE"]
    },
    {
        "0": ["sin(6*x) - y", "UNDER"]
    },
    {
        "0": ["tan(x) - y", "ABOVE"]
    }
]

这些函数应该生成 3 张图像。每个图像都应根据方程的解填充,并提供位置(下或上)。我需要将坐标系移动到图像的中心,所以我将-y 添加到等式中。应填充的图像部分应为白色,另一部分应为黑色。

为了解释我的意思,我提供了二次函数和 sin 函数的图像。

我正在做的是求解x in [-W/2, W/2] 的方程并将解存储到数组中,如下所示:

#Generates X axis dots and solves an expression which defines a band
#Coordinate system is moved to the center of the image
def __solveKernelDefinition(self, f):
    xAxis = range(-kernelSize, kernelSize)
    dots = []

    for x in xAxis:
        sol = f(x, kernelSize/2)
        dots.append(sol)

    print(dots)
    return dots

我正在测试某个像素是否应该像这样涂成白色:

def shouldPixelGetNoise(y, x, i, currentBand):
    shouldGetNoise = True

    for bandKey in currentBand.bandDefinition.keys():
        if shouldGetNoise:
            pixelSol = currentBand.bandDefinition[bandKey][2](x, y)
            renderPos = currentBand.bandDefinition[bandKey][1]
            bandSol = currentBand.bandDefinition[bandKey][0]
            shouldGetNoise = shouldGetNoise and pixelSol <= bandSol[i] if renderPos == Position.UNDER else pixelSol >= bandSol[i]
        else:
            break

    return shouldGetNoise

def kernelNoise(kernelSize, num_octaves, persistence, currentBand, dimensions=2):
    simplex = SimplexNoise(num_octaves, persistence, dimensions)
    data = []

    for i in range(kernelSize):
        data.append([])
        i1 = i - int(kernelSize / 2)

        for j in range(kernelSize):
            j1 = j - int(kernelSize / 2)
            if(shouldPixelGetNoise(i1, j1, i, currentBand)):
                noise = normalize(simplex.fractal(i, j, hgrid=kernelSize))
                data[i].append(noise * 255)
            else:
                data[i].append(0)

我只能得到凸二次函数的良好输出。如果我尝试将它们结合起来,我会得到一个黑色的图像。 Sin 根本不起作用。我看到这种蛮力方法不会把我带到任何地方,所以我想知道我应该使用什么算法来生成这些类型的图像?

【问题讨论】:

    标签: python-3.x image-processing numeric numerical-computing dynamic-image-generation


    【解决方案1】:

    据我了解,您想绘制函数并在这些函数的 aboveunder 中填充。您可以通过在numpy 中创建一个网格(即二维笛卡尔坐标系)并在网格上定义您的函数来轻松做到这一点。

    import numpy as np
    import matplotlib.pyplot as plt
    max_ax = 100
    
    resolution_x = max_ax/5
    resolution_y = max_ax/20
    y,x = np.ogrid[-max_ax:max_ax+1, -max_ax:max_ax+1]
    y,x = y/resolution_y, x/resolution_x
    
    func1 = x**2 + 2*x + 5  <= -y
    
    
    resolution_x = max_ax
    resolution_y = max_ax
    y,x = np.ogrid[-max_ax:max_ax+1, -max_ax:max_ax+1]
    y,x = y/resolution_y, x/resolution_x
    
    func2 = np.sin(6*x) <= y
    func3 = np.tan(x) <= -y
    
    
    
    fig,ax = plt.subplots(1,3)
    ax[0].set_title('f(x)=x**2 + 2*x + 5')
    ax[0].imshow(func1,cmap='gray')
    ax[1].set_title('f(x)=sin(6*x)')
    ax[1].imshow(func2,cmap='gray')
    ax[2].set_title('f(x)=tan(x)')
    ax[2].imshow(func3,cmap='gray')
    plt.show()
    
    

    这是你要找的吗?

    编辑:我调整了 x 轴和 y 轴的限制。因为,例如,sin(x) 在 [-1,1] 范围之外没有多大意义。

    【讨论】:

    • 没错,但我不能只是绘制它。我必须创建一个使用这些函数裁剪的单独图像。图像将被粘贴到一个大的黑色背景上,这就是原因。
    • 它们是我目前的图像。如您所见,我正在调用imshow 来显示它们。这不是你需要的吗?
    猜你喜欢
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 2019-04-29
    相关资源
    最近更新 更多