61

r、g、bの各コンポーネントを輝度に設定するのではなく、画像をピクセルあたり16ビットのグレースケール形式に変換する方法はありますか。私は現在ファイルからbmpを持っています。

Bitmap c = new Bitmap("filename");

cのグレースケールバージョンであるビットマップdが必要です。System.Drawing.Imaging.PixelFormatを含むコンストラクターは表示されますが、その使用方法がわかりません。私は画像処理と関連するC#ライブラリを初めて使用しますが、C#自体については中程度の経験があります。

ヘルプ、オンラインソースへの参照、ヒントまたは提案をいただければ幸いです。

編集:dはcのグレースケールバージョンです。

4

6 に答える 6

81

「グレースケールのビットマップdが必要です。System.Drawing.Imaging.PixelFormatを含むコンストラクターが表示されますが、その使用方法がわかりません。」

これを行う方法は次のとおりです

Bitmap grayScaleBP = new 
         System.Drawing.Bitmap(2, 2, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);

編集:グレースケールに変換するには

             Bitmap c = new Bitmap("fromFile");
             Bitmap d;
             int x, y;

             // Loop through the images pixels to reset color.
             for (x = 0; x < c.Width; x++)
             {
                 for (y = 0; y < c.Height; y++)
                 {
                     Color pixelColor = c.GetPixel(x, y);
                     Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
                     c.SetPixel(x, y, newColor); // Now greyscale
                 }
             }
            d = c;   // d is grayscale version of c  

switchonthecodeからのより高速なバージョンは、完全な分析のためにリンクをたどります:

public static Bitmap MakeGrayscale3(Bitmap original)
{
   //create a blank bitmap the same size as original
   Bitmap newBitmap = new Bitmap(original.Width, original.Height);

   //get a graphics object from the new image
   using(Graphics g = Graphics.FromImage(newBitmap)){

       //create the grayscale ColorMatrix
       ColorMatrix colorMatrix = new ColorMatrix(
          new float[][] 
          {
             new float[] {.3f, .3f, .3f, 0, 0},
             new float[] {.59f, .59f, .59f, 0, 0},
             new float[] {.11f, .11f, .11f, 0, 0},
             new float[] {0, 0, 0, 1, 0},
             new float[] {0, 0, 0, 0, 1}
          });

       //create some image attributes
       using(ImageAttributes attributes = new ImageAttributes()){

           //set the color matrix attribute
           attributes.SetColorMatrix(colorMatrix);

           //draw the original image on the new image
           //using the grayscale color matrix
           g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
                       0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
       }
   }
   return newBitmap;
}
于 2010-02-15T12:50:51.307 に答える
37
Bitmap d = new Bitmap(c.Width, c.Height);

for (int i = 0; i < c.Width; i++)
{
    for (int x = 0; x < c.Height; x++)
    {
        Color oc = c.GetPixel(i, x);
        int grayScale = (int)((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11));
        Color nc = Color.FromArgb(oc.A, grayScale, grayScale, grayScale);
        d.SetPixel(i, x, nc);
    }
}

このようにして、アルファチャネルも維持します。
楽しみ。

于 2010-10-23T14:44:50.953 に答える
29

ToolStripRendererクラスには、という名前の静的メソッドがありますCreateDisabledImage。その使用法は次のように簡単です。

Bitmap c = new Bitmap("filename");
Image d = ToolStripRenderer.CreateDisabledImage(c);

受け入れられた回答のマトリックスとは少し異なるマトリックスを使用し、さらに値0.7の透明度を掛けるので、効果は単なるグレースケールとは少し異なりますが、画像をグレースケールにしたい場合は、最も単純で最良の解決策。

于 2015-09-29T12:21:45.213 に答える
8

以下のコードは最も簡単な解決策です。

Bitmap bt = new Bitmap("imageFilePath");

for (int y = 0; y < bt.Height; y++)
{
    for (int x = 0; x < bt.Width; x++)
    {
        Color c = bt.GetPixel(x, y);

        int r = c.R;
        int g = c.G;
        int b = c.B;
        int avg = (r + g + b) / 3;
        bt.SetPixel(x, y, Color.FromArgb(avg,avg,avg));
    }   
}

bt.Save("d:\\out.bmp");
于 2015-01-22T16:21:10.137 に答える
7

上記の例はいずれも、8ビット(8bpp)のビットマップイメージを作成しません。画像処理などの一部のソフトウェアは、8bppのみをサポートします。残念ながら、MS.NETライブラリには解決策がありません。PixelFormat.Format8bppIndexed形式は有望に見えますが、何度も試行した後、機能させることができませんでした。

真の8ビットビットマップファイルを作成するには、適切なヘッダーを作成する必要があります。最終的に、8ビットビットマップ(BMP)ファイルを作成するためのグレースケールライブラリソリューションを見つけました。コードは非常に単純です。

Image image = Image.FromFile("c:/path/to/image.jpg");
GrayBMP_File.CreateGrayBitmapFile(image, "c:/path/to/8bpp/image.bmp");

このプロジェクトのコードはきれいにはほど遠いですが、修正が簡単な問題が1つあります。著者は、画像の解像度を10x10にハードコーディングしました。画像処理プログラムはこれが好きではありません。修正はGrayBMP_File.cs(ええ、ファンキーなファイルの命名、私は知っています)を開き、50行目と51行目を以下のコードに置き換えます。この例では、解像度を200x200に設定していますが、適切な数値に変更する必要があります。

int resX = 200;
int resY = 200;
// horizontal resolution
Copy_to_Index(DIB_header, BitConverter.GetBytes(resX * 100), 24);
// vertical resolution 
Copy_to_Index(DIB_header, BitConverter.GetBytes(resY * 100), 28);
于 2011-04-23T14:53:47.100 に答える
6

ここにいくつかの項目を要約すると、ピクセルごとのオプションがいくつかありますが、これらは単純ですが、高速ではありません。

@Luisのコメントへのリンク:(アーカイブ済み)https://web.archive.org/web/20110827032809/http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-グレースケールは素晴らしいです。

彼は3つの異なるオプションを実行し、それぞれのタイミングを含めます。

于 2011-08-16T22:22:07.917 に答える