Open Simplex Noise To Mat

Open Simplex Noise To Mat

Introduction

  • Generates an openCV mat using Open Simplex noise
  • Uses the excellent implementation of open simplex noise is found here

Code

public static unsafe Mat GetOpenSimplexNoise(int width, int height, float frequency)
{
    OpenSimplexNoise noise = new OpenSimplexNoise();

    Mat mat = new Mat(width, height, DepthType.Cv8U, 1);
    byte* buffer = (byte*)mat.DataPointer.ToPointer();

    for (int j = 0; j < mat.Height; j++)
    {
        for (int i = 0; i < mat.Width; i++)
        {
            float v = (float)noise.Evaluate(i * frequency, j * frequency);

            //normalize it from (-1, 1) to (0,255)
            v = (v+1) * 255 / 2;

            *buffer++ = (byte)v;
        }
    }

    return mat;
}

Usage

static void Main(string[] args)
{
    Mat mat = GetOpenSimplexNoise(1000, 1000, 1 / 32f);
    CvInvoke.Imshow("test", mat);
    CvInvoke.WaitKey(0);
}

Output