0

StackOverflow 様

この例のように、別の画像 (imgInput) から「チェッカーボード画像」(imgOutput) を作成するアルゴリズムを作成しています。

画像のすべてのピクセル (500x500 ピクセル) を 1 つずつチェックし、画像を 10x10 ピクセルの 2500 個のボックスに分割します。画像を描画するためではなく、平均的な RGB カラーを計算するためのアルゴリズムを既に作成しました。これはコードです:

    public class PixelMatrix
    {
        public int X;
        public int Y;
        public int R;
        public int G;
        public int B;
    }

    public class RGBMatrix
    {
        public int R;
        public int G;
        public int B;
    }

public Bitmap fncRasterize(Bitmap imgInput)
        {


            Bitmap imgOutput = new Bitmap(imgInput, 500, 500);
            imgOutput.Save("test.bmp");
            PixelMatrix[] arrWindows = new PixelMatrix[2500];
            RGBMatrix[] arrRGB = new RGBMatrix[100];

            Graphics gfx = Graphics.FromImage(imgOutput);


            int WindowCount = 1;
            int PixelCount = 1;

            int WindowX;
            int WindowY;

            int PixelX;
            int PixelY;

            int avrgR = 0;
            int avrgG = 0;
            int avrgB = 0;

            int tempcounter = 0;


            for (WindowY = 1; WindowY <= 50; WindowY++)
            {

                for (WindowX = 1; WindowX <= 50; WindowX++)
                {
                    PixelCount = 1;
                    avrgR = 0;

                    for (PixelY = 1;  PixelY <= 10; PixelY++)
                    {
                        for (PixelX = 1; PixelX <= 10; PixelX++)
                        {
                            MessageBox.Show("R:" + imgOutput.GetPixel(1, 1).R + " G:" + imgOutput.GetPixel(1, 1).G + " B:" +imgOutput.GetPixel(1, 1).B);

                            arrRGB[PixelCount].R = (int)imgOutput.GetPixel((WindowX * 10 - 10 + PixelX), (WindowY * 10 - 10 + PixelY)).R;
                            arrRGB[PixelCount].G = (int)imgOutput.GetPixel((WindowX * 10 - 10 + PixelX), (WindowY * 10 - 10 + PixelY)).G;
                            arrRGB[PixelCount].B = (int)imgOutput.GetPixel((WindowX * 10 - 10 + PixelX), (WindowY * 10 - 10 + PixelY)).B;

                            // This is just to test
                            tempcounter = +tempcounter;
                            lblProgress.Text = tempcounter.ToString();
                        }
                    }

                    for (int tempx = 1; tempx <= 100; tempx++)
                    {
                        avrgR = +arrRGB[tempx].R;
                        avrgG = +arrRGB[tempx].G;
                        avrgB = +arrRGB[tempx].B;
                    }
                    arrWindows[WindowCount].R = (avrgR / 100);
                    arrWindows[WindowCount].G = (avrgG / 100);
                    arrWindows[WindowCount].B = (avrgB / 100);

                    WindowCount = +1;

                }
            }




            return imgOutput;

        }

アルゴリズムの実行が開始されると、次の行で NullReferenceException エラーが発生します。

 arrRGB[PixelCount].R = imgOutput.GetPixel(tempR.X, tempR.Y).R;
 arrRGB[PixelCount].G = imgOutput.GetPixel(tempG.X, tempG.Y).G;
 arrRGB[PixelCount].B = imgOutput.GetPixel(tempG.X, tempG.Y).B;

その上の行 (Messagebox ステートメント) は問題なく RGB 値を返します。誰かが私にこれがなぜなのか説明してもらえますか? これは本当にイライラします。

4

2 に答える 2

0

タイプ RGBMatrix の要素を含む 100 個のスロットを持つ配列を宣言します。
ただし、インスタンスのプロパティにアクセスする前に、これらの各要素を作成し、インスタンスを正しいスロットに割り当てる必要があります。

for (PixelX = 1; PixelX <= 10; PixelX++)
{
    MessageBox.Show("R:" + imgOutput.GetPixel(1, 1).R + " G:" + imgOutput.GetPixel(1, 1).G + " B:" +imgOutput.GetPixel(1, 1).B);

    arrRGB[PixelCount] = new RBGMatrix();   // This create the instance

    arrRGB[PixelCount].R = (int).....
    arrRGB[PixelCount].G = (int)....
    arrRGB[PixelCount].B = (int)....

    // This is just to test
    tempcounter = +tempcounter;
    lblProgress.Text = tempcounter.ToString();
}

または他の方法

RGBMatrix mx = new new RBGMatrix();   
mx,R = = (int).....
....
arrRGB[PixelCount] = mx;
于 2013-06-01T20:07:38.157 に答える