1

カメラの各ピクセルのグレースケール値を 23453... (16 ビット値) の形式で取得し、.bmp のピクセルカラーを次のように設定します。

image.SetPixel(cols, rows, color);

しかし、グレースケール値を正しい色値にするにはどうすればよいですか? では、画像はグレースケール値で正しく表示できますか?

ありがとう

4

1 に答える 1

1
 public static Bitmap getGrayscale(Bitmap hc){
        Bitmap result = new Bitmap(hc.Width, hc.Height);
        ColorMatrix colorMatrix = new ColorMatrix(new float[][]{   
            new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0.5f,0.5f,0.5f,0,0},
            new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0,0,0,1,0,0},
            new float[]{0,0,0,0,1,0}, new float[]{0,0,0,0,0,1}});

        using (Graphics g = Graphics.FromImage(result)) {
            ImageAttributes attributes = new ImageAttributes();
            attributes.SetColorMatrix(colorMatrix);
            g.DrawImage(hc, new Rectangle(0, 0, hc.Width, hc.Height),
               0, 0, hc.Width, hc.Height, GraphicsUnit.Pixel, attributes);
        }
        return result;
    }

これは実際には非常に複雑な問題です。私の例では、基本的にフィルターを作成し、それを既存のビットマップに適用します。この問題を解決しようとして、しばらく前に解決しました。

編集

VB の人々は完全な例が好きで、おそらく C# も同様です。

ここに画像の説明を入力

2 つのボタンと画像ボックスをフォームにドロップし、コードを使用します。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace gray
{
    public partial class Example : Form
    {
        public Example()
        {
            InitializeComponent();
        }

        public static Bitmap getGrayscale(Bitmap hc)
        {
            Bitmap result = new Bitmap(hc.Width, hc.Height);
            ColorMatrix colorMatrix = new ColorMatrix(new float[][]{   
            new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0.5f,0.5f,0.5f,0,0},
            new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0,0,0,1,0,0},
            new float[]{0,0,0,0,1,0}, new float[]{0,0,0,0,0,1}});

            using (Graphics g = Graphics.FromImage(result))
            {
                ImageAttributes attributes = new ImageAttributes();
                attributes.SetColorMatrix(colorMatrix);
                g.DrawImage(hc, new Rectangle(0, 0, hc.Width, hc.Height),
                   0, 0, hc.Width, hc.Height, GraphicsUnit.Pixel, attributes);
            }
            return result;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "Open Image File";
            fDialog.Filter = "PNG Files|*.png|Bitmap Files|*.bmp";
            fDialog.InitialDirectory = @"C:\";

            if (fDialog.ShowDialog() == DialogResult.OK)
                this.pictureBox1.ImageLocation = fDialog.FileName.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.pictureBox1.Image == null)
            {
                MessageBox.Show("Sorry no image to alter.");
                return;
            }
            this.pictureBox1.Image = getGrayscale((Bitmap)this.pictureBox1.Image);
        }
    }
}

はい、これは機能し、色のバランスが正しく取れています。

しかし、有効な輝度を持つ別の方法が必要な場合は、次のようにします。

    ColorMatrix colorMatrix = new ColorMatrix(new float[][]{   
                              new float[]{ 0.3f, 0.3f, 0.3f,0,0},
                              new float[]{0.59f,0.59f,0.59f,0,0},
                              new float[]{0.11f,0.11f,0.11f,0,0},
                              new float[]{    0,    0,    0,1,0,0},
                              new float[]{    0,    0,    0,0,1,0},
                              new float[]{    0,    0,    0,0,0,1}});

編集2:@Hans Passant。

ビットマップは、カラーRGBA {、緑、青、アルファ透明度}を持つピクセルで構成されます。色付きの画像からグレースケールの画像を取得するには、各ピクセルの色の値に定義済みの を掛けます。colorMatrix

通常の2 行列乗算速度は Θ(n^2) ですが、この GDI+ 線形変換では高速フーリエ変換を使用して Θ(n log(n)) で実行します。これは、より大きな画像の場合、他の方法よりもはるかに高速であることを意味します。

In値が{R、G、B、A}の入力ピクセルがあり、行列の乗算後にピクセル値の式を取得したいとしましょう。Out{A out、B out、C out、Dで呼び出します。アウト}

ここに画像の説明を入力

外:

  • A out = r(4) A + r(3) B + r(2) G + r(1) R
  • B out = g(4) A + g(3) B + g(2) G + g(1) R
  • C出力= b(4) A + b(3) B + b(2) G + b(1) R
  • D out = a(4) A + a(3) B + a(2) G + a(1) R

またはこの場合:

ここに画像の説明を入力

Out = {
  0.11 B + 0.59 G + 0.3 R,
  0.11 B + 0.59 G + 0.3 R,
  0.11 B + 0.59 G + 0.3 R,
  A,
  0,
  0
}

ここで、グレースケール画像の有効輝度の式は です0.11 blue + 0.59 green + 0.3 red。だから正しい。

于 2011-03-23T19:15:10.990 に答える