예제 #1
0
        private void ImageReader2Factory()
        {
            // Path to vtk data must be set as an environment variable
            // VTK_DATA_ROOT = "C:\VTK\vtkdata-5.8.0"
            vtkTesting test     = vtkTesting.New();
            string     root     = test.GetDataRoot();
            string     filePath = System.IO.Path.Combine(root, @"Data\foot\foot.mha");

            vtkImageReader2 reader = vtkImageReader2Factory.CreateImageReader2(filePath);

            if (reader.CanReadFile(filePath) == 0)
            {
                MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK);
                return;
            }
            reader.SetFileName(filePath);
            reader.Update();
            vtkImageActor actor = vtkImageActor.New();

            actor.SetInputData(reader.GetOutput());

            // Visualize
            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            // renderer
            vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();

            // set background color
            renderer.SetBackground(0.2, 0.3, 0.4);
            renderer.AddActor(actor);
        }
예제 #2
0
    // Static void method with same signature as "Main" is always
    // file base name:
    //
    /// <summary>
    /// VTK test Main method
    /// </summary>
    public static void vtkImageDataExtraTests(string[] args)
    {
        string vtkDataDir = "";

        for (int i = 0; i < args.Length; i++)
        {
            if (args[i] == "-D")
            {
                vtkDataDir = args[i + 1];
            }
        }


        //Read in bitmap and image data
        string          bmpFile = vtkDataDir + "/Data/masonry.bmp";
        vtkImageReader2 rdr     = vtkImageReader2Factory.CreateImageReader2(bmpFile);

        rdr.SetFileName(bmpFile);
        rdr.Update();
        vtkImageData idata = rdr.GetOutput();

        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(bmpFile);

        //get bytes before and after double conversion
        byte[]       bmpArr1 = getArrayFromBitmap(bmp);
        vtkImageData bmp1    = vtkImageData.FromImage(bmp, 3);
        Bitmap       bmp2    = bmp1.ToBitmap();

        return;

        byte[] bmpArr2 = getArrayFromBitmap(bmp2);

        //get bytes before and after double conversion
        byte[]       idataArr1 = getArrayFromImageData(idata);
        Bitmap       idata1    = idata.ToBitmap();
        vtkImageData idata2    = vtkImageData.FromImage(idata1, 3);

        byte[] idataArr2 = getArrayFromImageData(idata2);


        for (int i = 0; i < bmpArr1.Length; i++)
        {
            if (bmpArr1[i] != bmpArr2[i])
            {
                throw new Exception("ERROR: Images Not Identical!");
            }
        }
        for (int i = 0; i < idataArr2.Length; i++)
        {
            if (idataArr1[i] != idataArr2[i])
            {
                throw new Exception("ERROR: Images Not Identical!");
            }
        }
        rdr.Dispose();
        Console.Out.WriteLine("Passed");
    }
예제 #3
0
        public void ImportVTK()
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // Get the name of the file you want to open from the dialog
                string      fileName = openFileDialog1.FileName;
                vtkRenderer ren      = (vtkRenderer)this.myRenderWindowControl.RenderWindow.GetRenderers().GetItemAsObject(0);

                //Get rid of any props already there
                if (imgProp != null)
                {
                    ren.RemoveActor(imgProp);
                    imgProp.Dispose();
                    imgProp = null;
                }

                //Look at known file types to see if they are readable
                if (fileName.Contains(".png") ||
                    fileName.Contains(".jpg") ||
                    fileName.Contains(".jpeg") ||
                    fileName.Contains(".tif") ||
                    fileName.Contains(".slc") ||
                    fileName.Contains(".dicom") ||
                    fileName.Contains(".minc") ||
                    fileName.Contains(".bmp") ||
                    fileName.Contains(".pmn"))
                {
                    vtkImageReader2 rdr = vtkImageReader2Factory.CreateImageReader2(fileName);
                    rdr.SetFileName(fileName);
                    rdr.Update();
                    imgProp = vtkImageActor.New();

                    ((vtkImageActor)imgProp).SetInput(rdr.GetOutput());
                    rdr.Dispose();
                }

                //.vtk files need a DataSetReader instead of a ImageReader2
                //some .vtk files need a different kind of reader, but this
                //will read most and serve our purposes
                else if (fileName.Contains(".vtk") || fileName.Contains(".VTK"))
                {
                    vtkDataSetReader dataReader = vtkDataSetReader.New();
                    dataReader.SetFileName(fileName);
                    dataReader.Update();

                    vtkDataSetMapper dataMapper = vtkDataSetMapper.New();
                    dataMapper.SetInput(dataReader.GetOutput());

                    imgProp = vtkActor.New();
                    ((vtkActor)imgProp).SetMapper(dataMapper);

                    dataMapper.Dispose();
                    dataMapper = null;

                    dataReader.Dispose();
                    dataReader = null;
                }
                else
                {
                    return;
                }

                ren.RemoveAllViewProps();
                ren.AddActor(imgProp);
                ren.ResetCamera();
                ren.SetBackground(0.0, 0.46, 0.5);

                //Rerender the screen
                //NOTE: sometimes you have to drag the mouse
                //a little before the image shows up
                ren.Render();
                myRenderWindowControl.RenderWindow.Render();
                myRenderWindowControl.Refresh();
            }
        }