2011年3月21日月曜日

16 bit グレースケール

CT, MR, CR, DR などの医用画像は殆どが16bit グレースケール画像。
PCでは8bitしか表示できないので、これを変換。
また Bitmap.SetPixel がとても遅いため LockBits を使用。

  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
DICOMオブジェクト
  1. int columns = dicomData.Columns;
  2. int rows = dicomData.Rows;
  3. int windowCenter = dicomData.WindowCenter;
  4. int windowWidth = dicomData.WindowWidth;
ビットマップ
 1byteが1pixelのグレースケール用ColorPaletteを作成する。
 3byte=1pixelのグレースケール(R=G=B)より容量減か。

  1. byte[] pixelData = dicomData.PixelData;Bitmap bitmap;
  2. ColorPalette colorPalette;
  3. for (int i = 0; i < 256; i++)
  4.     colorPalette.Entries[i] = Color.FromArgb(i, i, i);
  5. Bitmap bitmap = new Bitmap(columns, rows, PixelFormat.Format8bppIndexed);


データの挿入 ()
  1. public unsafe Bitmap Grayscale16To8()
  2.  
  3. {
  4.  
  5. int padding = bitmapData.Stride - columns;
  6.  
  7. int min = windowCenter - windowWidth / 2;
  8.  
  9. int max = windowCenter + windowWidth / 2;
  10.  
  11. fixed (byte* pData = pixelData)
  12.  
  13. {
  14.  
  15.     int pixel;
  16.  
  17.     short* ptr = (short*)pData;
  18.  
  19.     byte* pBitmap = (byte*)bitmapData.Scan0;
  20.  
  21.     int j = 0, i = 0;
  22.  
  23.     for (int y = 0; y < _rows; y++)
  24.  
  25.     {
  26.  
  27.         j = y * _columns;
  28.  
  29.         for (int x = 0; x < _columns; x++)
  30.  
  31.         {
  32.  
  33.             pixel = (int)ptr[j + x];  
  34.  
  35.             if (pixel <= min)
  36.  
  37.                 pixel = 0;
  38.  
  39.             else if (pixel >= max)
  40.  
  41.                 pixel = 255;
  42.  
  43.             else
  44.  
  45.                 pixel = (int)((pixel - min) * 255 / (max - min));
  46.  
  47.             pBitmap[i] = (byte)pixel;
  48.  
  49.             i++;
  50.  
  51.         }
  52.  
  53.         i += padding;
  54.  
  55.     }
  56.  
  57. }
  58.  
  59. bitmap.UnlockBits(bitmapData);
  60.  
  61. return bitmap;
  62.  
  63. }

0 件のコメント :

コメントを投稿