Re: [問題] 讀灰階影像的問題(灰階值錯誤)

看板C_Sharp作者 (他們稱呼我"鬥士")時間11年前 (2012/10/05 00:05), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/6 (看更多)
※ 引述《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
文章代碼(AID): #1GRRF9EF (C_Sharp)
討論串 (同標題文章)
文章代碼(AID): #1GRRF9EF (C_Sharp)