简体   繁体   English

ImageSharp:从 PixelFormats 转换为 ColorSpace

[英]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.

相关问题 将 ImageSharp.Image 转换为 ImageSharp.PixelFormats.Rgba32? - Convert ImageSharp.Image to ImageSharp.PixelFormats.Rgba32? 使用ImageSharp从byte []创建图像 - Create an image from byte[] using ImageSharp ImageSharp 从流中获取主色 - ImageSharp get dominant color from a stream imagesharp尝试制作从0到9的动画gif - imagesharp trying to make an animated gif that count from 0 to 9 使用 ImageSharp 将任意图像转换为 RGB565 字节 arrays - Convert arbitrary images to RGB565 byte arrays with ImageSharp 是否可以使用 SixLabors.ImageSharp 库转换图像格式? - Is it possible to convert image formats using the SixLabors.ImageSharp Libraries? 如何从System.Window.media.PixelFormats.Gray16创建位图 - How to create Bitmap from System.Window.media.PixelFormats.Gray16 使用 ImageSharp 将包含 Bgra32 的传入缓冲区转换为 ImageSharp 图像的最佳方法是什么<rgba24>图片?</rgba24> - Using ImageSharp what's the best way to convert incoming buffer containing Bgra32 to an ImageSharp Image<Rgba24> image? 如何防止 ImageSharp Web 调整到某些尺寸? - How to prevent ImageSharp Web from resizing to certain sizes? 使用FormatConvertedBitmap从Pbgra32转换为Bgr32 PixelFormats - Converting from Pbgra32 to Bgr32 PixelFormats using FormatConvertedBitmap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM