gpt4 book ai didi

c# - 在 C# 中从没有颜色对象的像素中提取 RGB

转载 作者:行者123 更新时间:2023-11-30 16:20:25 25 4
gpt4 key购买 nike

我正在尝试从以下代码中的像素中提取 R G B 值:

for ( int i=0; i < pixeldata.length; i++)
{
IntPtr ptr = bmd.Scan0+i;
byte* pixel = (byte*)ptr;

//here is the problem :O

float r = pixel[1];
float g = pixel[2];
float b = pixel[3];
}
....

其中 bmd 是一个像素数据数组:

BitmapData bmd = source.LockBits(rect, ImageLockMode.ReadOnly, source.PixelFormat);

source 是我输入的位图,它是一个图像。我试图避免使用 Color 对象。我已经这样做了并且有效,我想用这种方式,但问题是 ptr 是一个数字,我必须从中提取 R G B。

最佳答案

这是为您提供正确答案的解决方案。

Bitmap source = new Bitmap(image);

Rectangle rect = new Rectangle(0, 0, source.Width, source.Height);

BitmapData bmd = source.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

int totalPixels = rect.Height * rect.Width;
int[] pixelData = new int[totalPixels];
for (int i = 0; i < totalPixels; i++)
{
byte* pixel = (byte*)bmd.Scan0;
pixel = pixel + (i * 4);

byte b = pixel[0];
byte g = pixel[1];
byte r = pixel[2];

int luma = (int)(r * 0.3 + g * 0.59 + b * 0.11);
pixelData[i] = luma;
}

关于c# - 在 C# 中从没有颜色对象的像素中提取 RGB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14267425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com