作者Ethan96 (他们称呼我"斗士")
看板C_Sharp
标题Re: [问题] 读灰阶影像的问题(灰阶值错误)
时间Fri Oct 5 00:05:26 2012
※ 引述《timon (Tim On Five)》之铭言:
: 大家好!我目前正在使用C#学习有关影像处理的程式撰写。
: 目前遇到一个问题困扰我很久,请各位解答一下,如下:
: Bitmap bimage = new Bitmap("test.jpg");
: int[,] grayData = new int[bimage.Width, bimage.Height];
: for (int y = 0; y < bimage.Height; y++)
: {
: for (int x = 0; x < bimage.Width; x++)
: {
: Color color = bimage.GetPixel(x, y);
: grayData[x, y] = (color.R + color.G + color.B) / 3;
: }
: }
: 测试影像为8-bit的灰阶影像,为何抓出来的灰阶值(grayData中)都是17的倍数??
: 抓出来的灰阶值都与原图不一样,都是:0, 51, 102, 153, 238...等等(17的倍数?)
: 如果是24-bit彩色影像就没这样的问题,我在网路搜寻都没有这相关的文章!
: 可能是非常基本的问题! >"< 请各位帮忙提示一下 @@"
: 感激不尽! 谢谢大家!
我是用unsafe方式作,因为之前处理图片,发现如果是4bit或8bit会跟24位元有不同方式
建议以後处理4bit或8bit用unsafe方式
private void button1_Click(object sender, EventArgs e)
{
Bitmap source = new Bitmap(@"C:\test.jpg");
int width = source.Width;
int height = source.Height;
BitmapData sourceData = source.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
//以下用指标作
unsafe
{
for (int r = 0; r < height; r++)
{
byte* pSource = (byte*)(sourceData.Scan0 + r * sourceData.Stride);
for (int c = 0; c < width; c++)
{
byte colorIndex = (byte)(((*pSource) * 0.3 + *(pSource + 1) * 0.59 +
*(pSource + 2) * 0.11));
//Console.WriteLine(colorIndex);列印
pSource += 3;
}
}
}
}
印出的值就不是17倍数了
参考看看
unsafe要去专案属性勾起来
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 1.161.196.3