【问题标题】:3D Perlin Noise Normalize function in C#C# 中的 3D Perlin Noise Normalize 函数
【发布时间】:2020-11-12 22:48:16
【问题描述】:

过去一两周我一直在制作个性化的 Perlin 噪声生成器(注意我说的是个性化是因为我不想使用其他生成器),但我不是一个超级熟练的程序员,而且它真的很慢.为了加快速度,我一直在研究 C#,因为它接近于 python 和 java,这是我最好的两种语言,而不是 C。问题是,我用 python 编写了整个生成器,这不是我最擅长的语言,如果我用 java 编写它,我会更容易将它转换为 C#。

现在我正在尝试将我的生成器直接从 python 转换为 C#,这在大多数情况下我可以很容易地完成,但我对我的导师为我编写的一些东西有点不确定。即,这个归一化函数:

# np is numpy
def normalize(img):
    img_copy=img*1.0
    img_copy-=np.min(img_copy)
    img_copy/=np.max(img_copy)
    img_copy*=255.9999
    return np.uint8(img_copy)

我不知道 C# 是否可以在没有过多 for 循环的情况下完成这种几乎瞬时的列表理解,而且我对 NumSharp 也不太了解,我会用它来代替 numpy。

如何在 C# 中编写此函数,以及如何使用 NumSharp 等效的 numpy 函数 zeros()、max()、min() 和 cv2 函数 resize?

附:如果您需要更多上下文,我在 repl.it 上有程序。 https://repl.it/@JoshuaFavorite/PerlinNoiseGenerator#main.py

编辑:显然不清楚我的 python 程序是否完全正常运行,我不需要任何帮助,我需要 C# 方面的帮助。尤其是瞬时矩阵乘法、矩阵统计和用 python 很容易完成的事情。

【问题讨论】:

    标签: python c# numpy optimization perlin-noise


    【解决方案1】:

    抱歉,我不懂 C#,但这是在 Python OpenCV 中生成 perlin 噪声的一种方法

     - Start with a black image
     - Iterate generating a noise image at different dimensions according to power law
     - Resize it
     - Attenuate it
     - Add it to the previous iteration
     - Scale to range 0 to 255 as integer
     - Save the results
    

    import cv2
    import skimage.exposure
    import numpy as np
    from numpy.random import default_rng
    rng = default_rng()
    
    # define argument
    wd = 500                # width output
    ht = 500                # height of output
    base = 2                #integer>1; typically 2 to 4; frequency=base^(octave level)
    startlevel = 1          #starting octave level; integer>0
    endlevel = 6            #ending octave level; nominally 5 or 6
    atten = 0               #persistence=1/atten; amp=persist^(j-1); atten=0 -> amp=1/j; j=1,2...; atten=2 is good, also
    
    # compute larger of wd and ht
    max_dim = max(wd,ht)
    
    #compute start dim as base^(startlevel)
    start_dim = base**startlevel
    
    # compute end dim as base^(endlevel)
    end_dim = base**endlevel
    
    # create zero frequency black base to which to add octaves
    result = np.zeros((max_dim,max_dim),dtype=np.float32)
    
    # process octaves
    j = 1
    for i in range (startlevel,endlevel):
        if atten == 0:
            amp = 1/j
        else:
            amp = 1/(atten**(j-1))
        print("Processing Octave Level:",i," and Amplitude:",amp)
        
        # create noise image, attenuate it, combine with zero frequency initial level
        dim = base**i
        noise = rng.integers(0,255,(dim,dim),np.uint8,True).astype(np.float32)
        
        # resize to max_dim and attenuate
        noise = amp * cv2.resize(noise, (max_dim,max_dim), interpolation=cv2.INTER_CUBIC).astype(np.float32)
        
        # add to result
        result = cv2.add(result, noise)
        
        # stop if end_dim for given endlevel is larger than max_dim
        if end_dim > max_dim:
            break
                
        # increment 
        i += 1
        j += 1
    
    # scale to range 0 to 255, and crop to desired dimensions
    result = skimage.exposure.rescale_intensity(result, in_range='image', out_range=(0,255)).clip(0,255).astype(np.uint8)
    result = result[0:ht, 0:wd]
    
    # save result
    cv2.imwrite('perlin.jpg', result)
    
    # show results
    cv2.imshow('perlin', result)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    Atten 0、Startlevel 1 和 Endlevel 6:

    Atten 0、Startlevel 1 和 Endlevel 5:

    Atten 2,Startlevel 1 和 Endlevel 6:

    【讨论】:

    • 好吧,我已经在 python 中创建了整个东西(为了清楚起见,它有效,我什至将我完成的程序附加到问题中)。我的问题是我正在尝试将其翻译成 c#,但我不知道如何有效地执行规范化功能。
    • 另外,我使用了 Worley 噪声的变体,因此我可以有几种不同的噪声模式。它很慢的事实可能是由于我拒绝使用插值。
    猜你喜欢
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2012-03-21
    • 1970-01-01
    • 2012-03-23
    相关资源
    最近更新 更多