2015-10-07 140 views
0

我是一名新手程序員。 是否有任何算法將圖像轉換爲16位或8位? 我沒有在谷歌上找到它,我很絕望。將RGB圖像轉換爲RGB 16位和8位

+1

谷歌發現了這個,也許是有用的? http://www.codeproject.com/Articles/17162/Fast-Color-Depth-Change-for-Bitmaps –

回答

0

16位rgb和8位rgb之間的唯一區別是範圍,16位的值爲0到65536,而8位的值爲0到256,因此您應該能夠分配16位rgb值由256使用整數除法(爲簡單起見),將被轉換。 (您需要決定每個平面中的值256)

+0

這根本不是真的。對8位圖像進行索引,這意味着圖像中像素的值不是顏色,而是對256色表格的引用。實際上,16位圖像在16位像素值內包含完整的RGB顏色。 – Nyerguds

+0

@Nyerguds用於Gif格式以節省空間。對於通用RGB(位圖樣式)圖像(任意顏色精度),通常將其存儲爲表示紅色通道,綠色通道和藍色通道的必需大小(8位,16位,23位......)的3個灰度位圖。 – Cjen1

+0

你在說平面圖像嗎?你知道,具有8位值的三架飛機仍然是24位圖像。它保存的方式不會改變這一點;在質量(和尺寸)方面,它與每像素RGB三元組相同。實際的8位圖像字面上每個像素只有一個字節,這不足以保存有意義的RGB信息,因此8bpp或更低的圖像[被_always_索引](https://msdn.microsoft.com/zh-cn/library /system.drawing.imaging.pixelformat)。這與它保存的文件類型完全無關;你也可以擁有8位的BMP或PNG,並且它是一樣的。 – Nyerguds

1

更改爲16位更容易。假設原始圖像在image中,您可以簡單地將其繪製到結果中。

Bitmap result = new Bitmap(image.Width, image.Height, PixelFormat.Format16bppRgb565); 
using (Graphics g = Graphics.FromImage(result)) 
{ 
    g.DrawImage(image, 0, 0, image.Width, image.Height); 
} 

不幸的是,這對索引圖像(帶有調色板的圖像,最多256色)無效。但是您可以在我的回答here中找到解決方案,請參閱ConvertPixelFormat方法。

0

轉換RGB圖像爲8位

public static Bitmap ConvertFromRGBTo8bit(this Bitmap rgbBmp) 
{ 
    // Copy image to byte Array 
    var BoundsRectSrc = new Rectangle(0, 0, rgbBmp.Width, rgbBmp.Height); 
    BitmapData bmpDataSrc = rgbBmp.LockBits(BoundsRectSrc, ImageLockMode.WriteOnly, rgbBmp.PixelFormat); 
    IntPtr ptrSrc = bmpDataSrc.Scan0; 
    int imgSizeSrc = bmpDataSrc.Stride * rgbBmp.Height; 
    byte[] rgbValues = new byte[imgSizeSrc]; 
    Marshal.Copy(ptrSrc, rgbValues, 0, imgSizeSrc); 

    // Crearte bitmap with 8 index grayscale 
    Bitmap newBmp = new Bitmap(rgbBmp.Width, rgbBmp.Height, PixelFormat.Format8bppIndexed); 
    ColorPalette ncp = newBmp.Palette; 
    for (int i = 0; i < 256; i++) 
     ncp.Entries[i] = Color.FromArgb(255, i, i, i); 
    newBmp.Palette = ncp; 

    var BoundsRectDst = new Rectangle(0, 0, rgbBmp.Width, rgbBmp.Height); 
    BitmapData bmpDataDst = newBmp.LockBits(BoundsRectDst, ImageLockMode.WriteOnly, newBmp.PixelFormat); 
    IntPtr ptrDst = bmpDataDst.Scan0; 
    int imgSizeDst = bmpDataDst.Stride * newBmp.Height; 
    byte[] grayValues = new byte[imgSizeDst]; 

    // Convert image to 8 bit according average pixel 
    if (rgbBmp.PixelFormat == PixelFormat.Format16bppRgb555 || rgbBmp.PixelFormat == PixelFormat.Format16bppRgb565) 
     for (int i = 0, j = 0; i < grayValues.Length; i++, j += 2) 
      grayValues[i] = (byte)((rgbValues[j] + rgbValues[j + 1])/2); 
    else if (rgbBmp.PixelFormat == PixelFormat.Format24bppRgb) 
     for (int i = 0, j = 0; i < grayValues.Length; i++, j += 3) 
      grayValues[i] = (byte)((rgbValues[j] + rgbValues[j + 1] + rgbValues[j + 2])/3); 
    else if (rgbBmp.PixelFormat == PixelFormat.Format32bppRgb) 
     for (int i = 0, j = 0; i < grayValues.Length; i++, j += 4) 
      grayValues[i] = (byte)((rgbValues[j] + rgbValues[j + 1] + rgbValues[j + 2] + rgbValues[j + 3])/4); 

    Marshal.Copy(grayValues, 0, ptrDst, imgSizeDst); 
    newBmp.UnlockBits(bmpDataDst); 
    rgbBmp.UnlockBits(bmpDataSrc); 

    return newBmp; 
} 
+0

儘管可以轉換爲_grayscale_ 8位。實際的顏色減少比較困難。 – Nyerguds