よし、よし、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