コード例 #1
0
    static void Main(string[] args)
    {
        try
        {
            // The itkImageBase.ReadInformation() method can be used to
            // "sniff" out the image information stored in the file.

            // Read the image file information
            itkImageInformation info = itkImageBase.ReadInformation(args[0]);
            
            // Display the IO information
            Console.WriteLine("Information --------------------------");
            Console.WriteLine(String.Format("PixelType={0}",info.PixelType));
            Console.WriteLine(String.Format("Dimension={0}",info.Dimension));
            Console.WriteLine(String.Format("Size={0}",info.Size));
            Console.WriteLine(String.Format("Spacing={0}",info.Spacing));
            Console.WriteLine(String.Format("Origin={0}",info.Origin));

            // Use file information to create and read an image
            itkImageBase image = itkImage.New(info.PixelType, info.Dimension);
            image.Read(args[0]);

            // Display some image information
            Console.WriteLine("Image --------------------------------");
            Console.WriteLine(String.Format("Name={0}",image.Name));
            Console.WriteLine(String.Format("PixelType={0}",image.PixelType));
            Console.WriteLine(String.Format("Dimension={0}",image.Dimension));
            Console.WriteLine(String.Format("Size={0}",image.Size));
            Console.WriteLine(String.Format("Spacing={0}",image.Spacing));
            Console.WriteLine(String.Format("Origin={0}",image.Origin));
            Console.WriteLine(String.Format("Buffer={0}",image.Buffer));

            // Dispose of the image
            // Note: the image will be automatically disposed when it goes
            // out of scope, however it is good practice to dispose of
            // objects when they are no longer required.
            image.Dispose();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    } // end main
    private void DoWork(object oInputName)
    {
        try
        {
            // Cast the filename to a string
            String inputName = oInputName as String;

            // Read the image information
            itkImageInformation info = itkImage.ReadInformation(inputName);

            // Ensure the pixel type is a scalar or RGB or RGBA
            if (info.PixelType.IsArray && !info.PixelType.IsColor)
            {
                String message = "The image type is not supported: {0}";
                message = String.Format(message, info.PixelType);
                throw new NotSupportedException(message);
            }

            // Create the input and output types
            itkImageBase input; itkImageBase output;
            input  = itkImage.New(itkPixelType.F,  info.Dimension);
            output = itkImage.New(itkPixelType.UC, info.Dimension);

            // Open the input
            this.WriteLine("Reading " + inputName);
            input.Read(inputName);

            // Apply the filter
            FilterType filter = FilterType.New(input, output);
            filter.Started += new itkEventHandler(FilterStarted);
            filter.Progress += new itkProgressHandler(FilterProgress);
            filter.Ended += new itkEventHandler(FilterEnded);
            filter.SetInput(input);
            filter.LowerThreshold = 100;
            filter.UpperThreshold = 255;
            filter.OutsideValue = 0;
            filter.InsideValue = 255;
            filter.Update();
            filter.GetOutput(output);

            // Clean up
            input.DisconnectPipeline();
            output.DisconnectPipeline();
            filter.Dispose();

            // Construct the output filename
            String outputPath = Path.GetDirectoryName(input.Name);
            String outputFile = Path.GetFileNameWithoutExtension(input.Name);
            if      (output.Dimension == 2) { outputFile += "_OUT.png"; }
            else if (output.Dimension > 2)  { outputFile += "_OUT.mhd"; }
            String outputName = Path.Combine(outputPath, outputFile);

            // Write the output
            this.WriteLine("Writing " + outputName);
            output.Write(outputName);                

            // Finish up
            this.ResetStatusStrip();
            input.Dispose();
            output.Dispose();
        }
        catch (Exception ex)
        {
            this.WriteLine("An unexpected error occured!");
            this.WriteLine(ex.ToString());
        }
    }