在8 bit 圖處理上 並沒有 RGB 的值可以計算 ,由於 8 bits 的圖像是 指標圖像(類似GIF)
只需要調整 調色盤,就可以快速變成灰階
以一張 100*100 8bit 的圖為例
Dim colorPalette1 as ColorPalette
Dim bmpalette As Bitmap = New Bitmap(100, 100, PixelFormat.Format8bppIndexed)
colorPalette1 = bmpalette.Palette
For i As Integer = 0 To 255
colorPalette1.Entries(i) = Color.FromArgb(i, i, i)
Next
這樣 就轉成灰階了
24bit 的,雖然用Lockbit後計算也很快,但圖檔一大,計算起來也是很恐怖,在實用上不夠好
這時可以用 ColorMatrix 來實作,速度就跟 8 bit的圖處理一樣快
(ps 24bit 沒有調色盤)
唯一需要的是 需要 2倍的記憶體
先設定等會要拿來處理的 資料型態 與 陣列
Dim cm As System.Drawing.Imaging.ColorMatrix = New System.Drawing.Imaging.ColorMatrix(New Single()() _
{New Single() {0.3, 0.3, 0.3, 0, 0}, _
New Single() {0.59, 0.59, 0.59, 0, 0}, _
New Single() {0.11, 0.11, 0.11, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Dim ia As System.Drawing.Imaging.ImageAttributes = New System.Drawing.Imaging.ImageAttributes()
ia.SetColorMatrix(cm) '轉換灰階的資料型態
Dim bm as Bitmap = New Bitmap("C:\test.bmp") '<---取得 24bit彩色圖
Dim newBm as bitmap = New Bitmap( bm.Width, bm.Height, bm.PixelFormat) '<---設定一個一樣大小的 bitmap
Dim g as Graphic = Graphic.FromImage(newBm)
Dim rect as Rectangle = New Rectangle( 0,0, bm.width, bm .Height) '<--設定要轉換圖片的大小
'再來就把原來的圖 繪到 新的圖上 順便轉成灰階
g.DrawImage( bm, rect, 0, 0, bm.width, bm.height, GraphicsUnit.Pixel, ia)
轉灰階的速度比取出 一維陣列再來計算快多了
- Jan 22 Sat 2011 10:54
GDI+ 8bit 24bit 快速灰階處理
全站熱搜
留言列表
發表留言