[STAThread] // おまじない static void Main(string[] args) { // コマンドライン引数の情報を入れる構造体 var param = new IPParams(); //コマンドライン引数の解析 var setting = new CommandLineParserSettings(Console.Error); var paser = new CommandLineParser(setting); //パースに失敗した場合停止 if (!paser.ParseArguments(args, param)) { Environment.Exit(1); } var executor = new ImageProcessRunner(param); executor.Start(); }
public ImageProcess(IPParams param) { imageData = new ImageData(); this.param = param; }
public void LoadImageFromFile(IPParams param) { try { string[] files; files = Directory.GetFiles(param.ipath, "*.bmp"); if (files.Length == 0) { files = Directory.GetFiles(param.ipath, "*.png"); } if (files.Length == 0) { files = Directory.GetFiles(param.ipath, "*.jpg"); } if (files.Length == 0) { files = Directory.GetFiles(param.ipath, "*.tiff"); } if (files.Length == 0) { files = Directory.GetFiles(param.ipath, "*.tif"); } if (files.Length == 0) { //例外: 画像がない throw new Exception("Images are not found."); } Array.Sort <String>(files); int pix; int stride; using (var image = new Bitmap(files[0])) { switch (image.PixelFormat) { case System.Drawing.Imaging.PixelFormat.Format32bppArgb: pix = 4; break; case System.Drawing.Imaging.PixelFormat.Format24bppRgb: pix = 3; break; case System.Drawing.Imaging.PixelFormat.Format8bppIndexed: pix = 1; break; default: throw new Exception("This image format is not supported."); } XSize = image.Width; YSize = image.Height; ZSize = files.Length; stride = ReadStride(image); } voxel = new bool[ZSize, YSize, XSize]; int z = 0; byte[] buffer = new byte[stride * YSize]; foreach (string filename in files) { using (var image = new Bitmap(filename)) { if (image.Width != XSize || image.Height != YSize) { //例外: 画像サイズが一致しない throw new Exception("The image size is different for each image."); } BitmapData imagedata = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat); Marshal.Copy(imagedata.Scan0, buffer, 0, buffer.Length); for (int y = YSize - 1; y >= 0; y--) { for (int x = 0; x < XSize; x++) { //voxel[z, y, x] = barr[y * stride + pix * x] >= param.binarizeThreshold ? !param.back : param.back; int val; switch (pix) { case 1: voxel[z, y, x] = buffer[y * stride + pix * x] >= param.binarizeThreshold ? !param.back : param.back; break; case 3: val = (byte)(buffer[y * stride + pix * x] * 0.114 + buffer[y * stride + pix * x + 1] * 0.587 + buffer[y * stride + pix * x + 2] * 0.299); voxel[z, y, x] = val >= param.binarizeThreshold ? !param.back : param.back; break; case 4: val = (byte)(buffer[y * stride + pix * x + 1] * 0.114 + buffer[y * stride + pix * x + 2] * 0.587 + buffer[y * stride + pix * x + 3] * 0.299); voxel[z, y, x] = val >= param.binarizeThreshold ? !param.back : param.back; break; } } } image.UnlockBits(imagedata); } GC.Collect(); z++; } } catch (Exception ex) { Console.WriteLine(ex.Message); Environment.Exit(1); } }
public void LoadImageFromFile(IPParams param) { try { string[] files; files = Directory.GetFiles(param.ipath, "*.bmp"); if (files.Length == 0) files = Directory.GetFiles(param.ipath, "*.png"); if (files.Length == 0) files = Directory.GetFiles(param.ipath, "*.jpg"); if (files.Length == 0) files = Directory.GetFiles(param.ipath, "*.tiff"); if (files.Length == 0) files = Directory.GetFiles(param.ipath, "*.tif"); if (files.Length == 0) { //例外: 画像がない throw new Exception("Images are not found."); } Array.Sort<String>(files); int pix; int stride; using (var image = new Bitmap(files[0])) { switch (image.PixelFormat) { case System.Drawing.Imaging.PixelFormat.Format32bppArgb: pix = 4; break; case System.Drawing.Imaging.PixelFormat.Format24bppRgb: pix = 3; break; case System.Drawing.Imaging.PixelFormat.Format8bppIndexed: pix = 1; break; default: throw new Exception("This image format is not supported."); } XSize = image.Width; YSize = image.Height; ZSize = files.Length; stride = ReadStride(image); } voxel = new bool[ZSize, YSize, XSize]; int z = 0; byte[] buffer = new byte[stride * YSize]; foreach (string filename in files) { using (var image = new Bitmap(filename)) { if (image.Width != XSize || image.Height != YSize) { //例外: 画像サイズが一致しない throw new Exception("The image size is different for each image."); } BitmapData imagedata = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat); Marshal.Copy(imagedata.Scan0, buffer, 0, buffer.Length); for (int y = YSize - 1; y >= 0; y--) { for (int x = 0; x < XSize; x++) { //voxel[z, y, x] = barr[y * stride + pix * x] >= param.binarizeThreshold ? !param.back : param.back; int val; switch (pix) { case 1: voxel[z, y, x] = buffer[y * stride + pix * x] >= param.binarizeThreshold ? !param.back : param.back; break; case 3: val = (byte)(buffer[y * stride + pix * x] * 0.114 + buffer[y * stride + pix * x + 1] * 0.587 + buffer[y * stride + pix * x + 2] * 0.299); voxel[z, y, x] = val >= param.binarizeThreshold ? !param.back : param.back; break; case 4: val = (byte)(buffer[y * stride + pix * x + 1] * 0.114 + buffer[y * stride + pix * x + 2] * 0.587 + buffer[y * stride + pix * x + 3] * 0.299); voxel[z, y, x] = val >= param.binarizeThreshold ? !param.back : param.back; break; } } } image.UnlockBits(imagedata); } GC.Collect(); z++; } } catch (Exception ex) { Console.WriteLine(ex.Message); Environment.Exit(1); } }
public ImageProcessRunner(IPParams param) { this.param = param; ip = new ImageProcess(param); }