0

よし、よし、n00b の助けを借りる時間だ。
私は C# でこのコードに取り組んできました (正確には Mono)。画像を取得し、ピクセルごとに分解し、R、G、および B の値を色座標としてテキスト ファイルに書き込むように設計されています。
問題は、処理が得られないことです。赤の 0 (for() ループのすぐ上にあるパーセント関数) と空のファイルを取得するだけです。テキスト/座標は生成されません。わかりやすくするために、コード自体の下にコンソール出力を投稿します。

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace Image_Interpreter_I
{

class MainClass
{
    public static void Main (string[] args)
    {
            Console.WriteLine ("Enter filepath for bitmap image");
            Console.ForegroundColor = ConsoleColor.Blue;
            string filepath = Console.ReadLine ();
            Console.ResetColor();
            Bitmap image1 = new Bitmap(System.Drawing.Image.FromFile(filepath, true));
            Console.WriteLine ("Enter filepath for file interpretation output");
            Console.ForegroundColor = ConsoleColor.Blue;
            string filepath2 = Console.ReadLine();
            Console.ResetColor();
            int i;
            i = 1;
            int ImW, ImH;
            string ImWstr, ImHstr;
            Console.WriteLine ("Enter width of image:");
            Console.ForegroundColor = ConsoleColor.Blue;
            ImWstr = Console.ReadLine ();
            Console.ResetColor ();
            ImW = Convert.ToInt16 (ImWstr);
            Console.WriteLine ("Enter height of image:");
            Console.ForegroundColor = ConsoleColor.Blue;
            ImHstr = Console.ReadLine ();
            ImH = Convert.ToInt16 (ImHstr);
            decimal percent;
            percent = (i / (ImH * ImW)) * 100;
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine (percent);
            Console.ResetColor ();
            for (int y = 1; y <= ImH; y++)
            {
                for (int x = 1; x <= ImW; x++)
                {
                    Color pixelColor = image1.GetPixel(x,y);
                    byte r = pixelColor.R;
                    byte g = pixelColor.G;
                    byte b = pixelColor.B;
                    string pixData = String.Format ("({0},{1},{2}", new object[] {r, g, b});
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath2))
                    {
                        file.WriteLine(pixData);

                    }
                }
            }
        i++;    
}
 }
 }

コンソール出力:

ビットマップ画像のファイルパスを入力してください
/Users/user0/Desktop/IMG_1528.JPG
ファイル解釈出力のファイルパスを入力してください
/Users/user0/Desktop/imageData.txt
画像の幅を入力してください:
2448 画像の高さを入力してください: 3264 0

4

3 に答える 3

2

まず、各ループ エントリで新しいストリーム ライターを作成しています。これは、毎回既存のファイルを上書きし、速度も遅くなります。最初にこのストリームを作成してから、処理を行います。

using (var file = new System.IO.StreamWriter(filepath2))
{
    // for loops
}

第二に、ドキュメントによると: Bitmap.GetPixelピクセルは 0 からWidth-1/にインデックス付けさHeight-1れます。0 から開始し、ループ終了条件をy < ImHおよびに変更しますx < ImW

もう 1 つのこと - 将来のためにコードのフォーマットを改善し、コードを理解しやすく読みやすいようにメソッドを抽出します。

于 2013-05-31T09:16:47.277 に答える
0
        static void Main(string[] args)
    {
        string filepath = @"C:\SomePath\SomeFile.JPG";
        Bitmap image1 = new Bitmap(System.Drawing.Image.FromFile(filepath, true));
        string filepath2 = @"C:\temp\myFile.txt";

        using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath2))
        {
            for (int y = 0; y < image1.Height; y++)
            {
                for (int x = 0; x < image1.Width; x++)
                {
                    Color pixelColor = image1.GetPixel(x, y);
                    byte r = pixelColor.R;
                    byte g = pixelColor.G;
                    byte b = pixelColor.B;

                    string pixData = String.Format("({0},{1},{2})", new object[] { r, g, b });

                    file.WriteLine(pixData);
                }
            }
        }
    }
于 2013-05-31T09:16:32.487 に答える