[英]ImageSharp: convert from PixelFormats to ColorSpace
I am trying to use ImageSharp for some image processing.我正在尝试使用 ImageSharp 进行一些图像处理。 I would like to get HSL values for an individual pixel.
我想获取单个像素的 HSL 值。 For that I think that I need to convert PixelFormat to a ColorSpace.
为此,我认为我需要将 PixelFormat 转换为 ColorSpace。 How do I convert to/access Hsl color space values?
如何转换/访问 Hsl 颜色空间值?
I have tried the following ColorSpaceConverter to no avail.我尝试了以下 ColorSpaceConverter 无济于事。
for (int y = 0; y < image.Height; y++)
{
Span<Rgb24> pixelRowSpan = image.GetPixelRowSpan(y);
Span<Hsl> hslRowSpan = new Span<Hsl>();
var converter = new ColorSpaceConverter();
converter.Convert(pixelRowSpan, hslRowSpan);
}
I do get the following errors:我确实收到以下错误:
error CS1503: Argument 1: cannot convert from
'System.Span<SixLabors.ImageSharp.PixelFormats.Rgb24>' to
'System.ReadOnlySpan<SixLabors.ImageSharp.ColorSpaces.CieLch>'
error CS1503: Argument 2: cannot convert from
'System.Span<SixLabors.ImageSharp.ColorSpaces.Hsl>' to 'System.Span<SixLabors.ImageSharp.ColorSpaces.CieLab>'
Rgb24
has an implicit conversion to Rgb
but as you have discovered that doesn't allow implicit conversion of spans. Rgb24
具有到Rgb
的隐式转换,但正如您所发现的那样,它不允许跨度的隐式转换。
I would allocate a pooled buffer equivalent to one row of Rgb
outside the loop and populate the buffer for each y
.我会在循环外分配一个相当于一行
Rgb
的池化缓冲区,并为每个y
填充缓冲区。
// I would probably pool these buffers.
Span<Rgb> rgb = new Rgb[image.Width];
Span<Hsl> hsl = new Hsl[image.Width];
ColorSpaceConverter converter = new();
for (int y = 0; y < image.Height; y++)
{
Span<Rgb24> row = image.GetPixelRowSpan(y);
for (int x = 0; x < row.Length; x++)
{
rgb[x] = row[x];
}
converter.Convert(rgb, hsl);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.