PROWAREtech








.NET: Access the Pixels of an Image Using SixLabors.ImageSharp v3.0
How to access each pixel's color information using SixLabors.ImageSharp, written in C#.
It may be necessary to access the color information of as image, such as preparing data for a convolutional neural network.
See how to crop an image to a square.
using SixLabors.ImageSharp;
var inputs = new double[28 * 28];
using (var bmp = Image.Load<Rgb24>("file.jpg"))
{
if (bmp.Width != 28 && bmp.Height != 28)
bmp.Mutate(x => x.Resize(28, 28));
bmp.ProcessPixelRows(accessor =>
{
for (int y = 0; y < 28; y++)
{
Span pixelRow = accessor.GetRowSpan(y);
for (int x = 0; x < 28; x++)
{
ref Rgb24 pixel = ref pixelRow[x];
inputs[y * 28 + x] = 1 - (pixel.R + pixel.G + pixel.B) / 3 / 255.0;
}
}
});
}
Comment