質問

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

Bitmap c = new Bitmap("filename");

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

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

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

役に立ちましたか?

解決

「ビットマップ d、つまりグレースケールが必要です。私は以下を含むconsrctorを参照してください 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  

からの高速バージョン コードをオンにします 完全な分析については、リンクをクリックしてください。

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
   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
   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);

   //dispose the Graphics object
   g.Dispose();
   return newBitmap;
}

他のヒント

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);
    }
}

この方法は、それはまた、アルファチャンネルを保持します。
お楽しみください。

ToolStripRendererという名前CreateDisabledImageクラスの静的メソッドは、あります。

:その使い方は同じくらい簡単です
Bitmap c = new Bitmap("filename");
Image d = ToolStripRenderer.CreateDisabledImage(c);

これは受け入れ答えの1よりも少し異なるマトリックスを使用し、さらに値0.7の透明度を掛け、その効果は、単なるグレースケールよりもわずかに異なるですが、あなたは自分のイメージがグレー表示取得したい場合、それはです最も簡単かつ最善の解決策ます。

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

にリンク@Luis'コメント:(アーカイブ)<のhref = "https://web.archive.org/web/20110827032809/http://www.switchonthecode.com/tutorials/csharp-tutorial-convert- A-カラー画像の階調」REL = "nofollowを"> https://web.archive.org/web/20110827032809/http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-カラー画像の階調には見事である。

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

の例のいずれも、上記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");

このプロジェクトのコードはかなりほど遠いですが、それは一つの小さな、簡単に修正問題で、動作します。著者は、10×10の画像解像度をハードコード化されました。画像処理プログラムはこれを好きではありません。修正はオープンGrayBMP_File.cs(ええ、ファンキーなファイルの命名、私が知っている)であり、以下のコードで行50と51を交換してください。例では、200×200の解像度を設定していますが、適切な番号に変更する必要があります。

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);

以下のコードは、最も単純な解決策である

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");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top