0

ロック ビットを使用したファイルへの書き込みに問題があります。ほとんどの画像で奇妙な歪み効果を持つエッジ検出ソフトウェアに取り組んでいます。問題を特定しようとしましたが、非常にランダムに見えます。フォーマットとは関係ありませんが、機能しているように見える画像はデスクトップの壁紙用に作成された画像だけであり、その理由はよくわかりません. 私は最近ロックビットを使用してファイルに書き込むように切り替えただけなので、問題はそれにあると確信しています (ロックビットで読み取り、セットピクセルで書き込みを行っていたときは問題はありませんでした)。効果のスクリーンショットは次のとおりです。

スクリーンショット

ご覧のとおり、エッジ検出は機能しますが、画像が水平方向に歪んでおり、画像が平行四辺形になっています。

これをすべて処理するメソッドのコード スニペットを次に示します (C# で)。

private void analyze()
{
    //When the analyze button is pressed
    percentageInt = float.Parse(textBox1.Text);
    float scale = 1;

    if (comboBox1.SelectedItem == "Auto")
    {
        scale = pic.Width / pictureBox1.Width;
    }
    else if (comboBox1.SelectedItem == "1/2")
    {
        scale = 2;
    }
    else if (comboBox1.SelectedItem == "1/4")
    {
        scale = 4;
    }
    else if (comboBox1.SelectedItem == "Original")
    {
        scale = 1;
    }
    else
    {
        scale = pic.Width / pictureBox1.Width;
    }

    int tempWidth = 1;
    int tempHeight = 1;
    if (scale >= 1)
    {
        tempWidth = (int)Math.Floor(pic.Width / scale);
        tempHeight = (int)Math.Floor(pic.Height / scale);
    }
    else
    {
        tempWidth = pic.Width;
        tempHeight = pic.Height;
    }

    width = pic.Width;
    height = pic.Height;
    edgeData = new Boolean[pic.Width, pic.Height];

    img = (Bitmap)resizeImage(pic, new Size(tempWidth, tempHeight));
    pic2 = new Bitmap(tempWidth, tempHeight);
    Bitmap img2 = (Bitmap)pic2;
    Color[] pixels = null;

    BitmapData data = img.LockBits(new Rectangle(0, 0, img.Width, img.Height),
            ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

    int size = Math.Abs(data.Stride) * img.Height;
    Byte[] bytes = new byte[size];

    int scaledPercent = (int)(Math.Round(percentageInt * 255));
    Debug.WriteLine("percent " + scaledPercent);
    unsafe
    {
        Debug.WriteLine("Woah there, unsafe stuff");
        byte* prevLine = (byte*)data.Scan0;
        byte* currLine = prevLine + data.Stride;
        byte* nextLine = currLine + data.Stride;

        for (int y = 1; y < img.Height - 1; y++)
        {
            byte* pp = prevLine + 3;
            byte* cp = currLine + 3;
            byte* np = nextLine + 3;
            for (int x = 1; x < img.Width - 1; x++)
            {
                if (IsEdgeOptimized(pp, cp, np, scaledPercent))
                {
                    edgeData[x, y] = true;
                    //Debug.WriteLine("x " + x + "y " + y);

                    //img2.SetPixel(x, y, Color.Black);
                    //bytes[(y * img.Width + x) * 3 + 2] = 255;
                }
                else
                {
                    bytes[(y * img.Width + x) * 3] = 255;
                    bytes[(y * img.Width + x) * 3 + 1] = 255;
                    bytes[(y * img.Width + x) * 3 + 2] = 255;
                    //img2.SetPixel(x, y, Color.White);
                }
                pp += 3; cp += 3; np += 3;
            }
            prevLine = currLine;
            currLine = nextLine;
            nextLine += data.Stride;
        }
    }
    System.Runtime.InteropServices.Marshal.Copy(bytes, 0, data.Scan0, size);
    img.UnlockBits(data);
    pictureBox2.Image = img;
} // end analyze

では、何が問題を引き起こしているのでしょうか。どうすれば修正できますか? 詳細が必要な場合は、お気軽にコメントしてください。

4

1 に答える 1

2

ストライド x 高さバイトでバイト バッファーを初期化しています。

int size = Math.Abs(data.Stride) * img.Height;
Byte[] bytes = new byte[size];

ただし、書き込むときに(ストライドの代わりに)幅を使用します。

bytes[(y * img.Width + x) * 3] = 255;
于 2013-10-05T22:18:41.010 に答える