Introduction
- 2D noise is often represented as a grayscale image.
- It is often useful to recolor this image so it can be used as a texture.
Code
public static unsafe Mat RecolorNoise(Mat mat, Color lowColor, Color highColor)
{
if (mat.NumberOfChannels != 1 || mat.Depth != DepthType.Cv8U)
{
throw new Exception("Input mat must be grayscale");
}
Mat gradient = CreateLinearGradient(256, lowColor, highColor);
byte* buffer = (byte*)mat.DataPointer.ToPointer();
byte* end = buffer + mat.Width * mat.Height;
byte* kGradient = (byte*)gradient.DataPointer.ToPointer();
Mat recolored = new Mat(mat.Width, mat.Height, DepthType.Cv8U, 3);
byte* kRecolored = (byte*)recolored.DataPointer.ToPointer();
while (buffer != end)
{
int gradientOffset = *buffer * 3;
*kRecolored++ = kGradient[gradientOffset];
*kRecolored++ = kGradient[gradientOffset + 1];
*kRecolored++ = kGradient[gradientOffset + 2];
buffer++;
}
return recolored;
}
Usage
static void Main(string[] args)
{
Mat noise = GetFractalOpenSimplexNoise(500, 500, 1 / 128f, 3, 0.5f);
Mat sky = RecolorNoise(noise, Color.FromArgb(67, 150, 228), Color.White);
CvInvoke.Imshow("test", sky);
CvInvoke.WaitKey(0);
}
Output