private void ReadJPEG() { // 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\beach.jpg"); //Read the image vtkJPEGReader reader = vtkJPEGReader.New(); if (reader.CanReadFile(filePath) == 0) { MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK); return; } reader.SetFileName(filePath); reader.Update(); // Visualize vtkImageViewer2 imageViewer = vtkImageViewer2.New(); imageViewer.SetInputConnection(reader.GetOutputPort()); // get a reference to the renderwindow of our renderWindowControl1 vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow; // renderer vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer(); // set background color renderer.SetBackground(0.2, 0.3, 0.4); imageViewer.SetRenderer(renderer); }
private vtkImageActor GetMapImage() { string filePath = this.folderPath + "\\map.jpg"; vtkImageData imageData; //Read the image vtkJPEGReader jpegReader = new vtkJPEGReader(); if (jpegReader.CanReadFile(filePath) == 0) { Console.WriteLine("ERROR"); return(null); } jpegReader.SetFileName(filePath); jpegReader.Update(); imageData = jpegReader.GetOutput(); //imageData.SetExtent(0, 1236, 0, 870, 0, 0); //imageData.SetSpacing(1236, 870, 0.01); imageData.SetOrigin(0, 0, 0); vtkImageActor imageActor = new vtkImageActor(); imageActor.SetInput(imageData); imageActor.SetScale(1f / horizontalRes); double[] bounds = imageActor.GetBounds(); return(imageActor); }
private void WriteJPEG() { // 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\test_jpeg.jpg"); int[] extent = new int[] { 0, 99, 0, 99, 0, 0 }; vtkImageCanvasSource2D imageSource = vtkImageCanvasSource2D.New(); imageSource.SetExtent(extent[0], extent[1], extent[2], extent[3], extent[4], extent[5]); imageSource.SetScalarTypeToUnsignedChar(); imageSource.SetNumberOfScalarComponents(3); imageSource.SetDrawColor(127, 45, 255); imageSource.FillBox(0, 99, 0, 99); imageSource.SetDrawColor(255, 255, 255); imageSource.FillBox(40, 70, 20, 50); imageSource.Update(); vtkImageCast castFilter = vtkImageCast.New(); castFilter.SetOutputScalarTypeToUnsignedChar(); castFilter.SetInputConnection(imageSource.GetOutputPort()); castFilter.Update(); vtkJPEGWriter writer = vtkJPEGWriter.New(); writer.SetFileName(filePath); writer.SetInputConnection(castFilter.GetOutputPort()); writer.Write(); // Read and display file for verification that it was written correctly vtkJPEGReader reader = vtkJPEGReader.New(); if (reader.CanReadFile(filePath) == 0) { MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK); return; } reader.SetFileName(filePath); reader.Update(); // Visualize vtkImageViewer2 imageViewer = vtkImageViewer2.New(); imageViewer.SetInputConnection(reader.GetOutputPort()); // get a reference to the renderwindow of our renderWindowControl1 vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow; // renderer vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer(); // set background color renderer.SetBackground(0.2, 0.3, 0.4); imageViewer.SetRenderer(renderer); renderer.ResetCamera(); }
/// <summary> /// Load image files into vtkImageData. /// </summary> /// <param name="path">Path to images.</param> /// <param name="extension">Image extension.</param> /// <returns></returns> public static vtkImageData VTKLoader(string path, string extension) { /*DEPRECATED!!*/ //Output vtkImageData data = vtkImageData.New(); //Get files from path DirectoryInfo d = new DirectoryInfo(@path); FileInfo[] files = d.GetFiles(); vtkStringArray allfiles = vtkStringArray.New(); //Iterate over files and read image data foreach (FileInfo file in files) { //Fullfile string fullfile = Path.Combine(path, file.Name); allfiles.InsertNextValue(fullfile); } if (extension == ".png") { vtkPNGReader reader = vtkPNGReader.New(); reader.SetFileNames(allfiles); reader.Update(); data = reader.GetOutput(); reader.Dispose(); } if (extension == ".jpg") { vtkJPEGReader reader = vtkJPEGReader.New(); reader.SetFileNames(allfiles); reader.Update(); data = reader.GetOutput(); reader.Dispose(); } if (extension == ".bmp") { vtkBMPReader reader = vtkBMPReader.New(); reader.SetFileNames(allfiles); reader.Update(); data = reader.GetOutput(); reader.Dispose(); } data.SetScalarTypeToUnsignedChar(); data.Update(); return(data); }
public void ReadImageIntoObject(RenderWindowControl renderWindowControl, nvmImageModel camera) { vtkRenderWindow renderWindow = renderWindowControl.RenderWindow; vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer(); string filePath = Path.Combine(tempDirectory, $"{camera.fileName}"); vtkJPEGReader reader = vtkJPEGReader.New(); reader.SetFileName(filePath); reader.Update(); // Treba poriesit ako nasmerovat obrazky bez pokazenia textury var vectoris = Vector3.Transform(new Vector3(0, 0, 1), camera.quaternion); vtkPlaneSource planeSource = vtkPlaneSource.New(); vtkTexture texture = new vtkTexture(); texture.SetInputConnection(reader.GetOutputPort()); vtkTransform transform = new vtkTransform(); transform.RotateX(180); texture.SetTransform(transform); vtkTextureMapToPlane plane = new vtkTextureMapToPlane(); plane.SetInputConnection(planeSource.GetOutputPort()); planeSource.SetCenter(camera.cameraCenter.X, camera.cameraCenter.Y, camera.cameraCenter.Z); vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); mapper.SetInputConnection(plane.GetOutputPort()); vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); actor.SetTexture(texture); renderer.SetBackground(0.2, 0.3, 0.4); renderer.AddActor(actor); }
int build3DViewFull() { Kitware.VTK.RenderWindowControl rw = new Kitware.VTK.RenderWindowControl(); vtkRenderWindow _renwin = rw.RenderWindow; vtkRenderer _render = _renwin.GetRenderers().GetFirstRenderer(); _renwin.AddRenderer(_render); vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor(); iren.SetRenderWindow(_renwin); // 新建文件读取对象,常见的有vtkBMPReader、vtkDICOMImageReader、vtkJPEGReader等 vtkJPEGReader jpegReader = new vtkJPEGReader(); // 不同的reader需要设置的参数是不同的 因此本例仅适合jpegreader jpegReader.SetFilePrefix("C:/Users/DawnWind/Desktop/000/"); // 要打开的路径 jpegReader.SetFilePattern("%s%d.jpg"); // 图片文件名格式,此处为 0.jpg 1.jpg ... jpegReader.SetDataByteOrderToLittleEndian(); jpegReader.SetDataSpacing(1, 1, 1.4); // 设置图片中像素比,我理解得不清楚,具体请百度之 jpegReader.SetFileNameSliceSpacing(1); jpegReader.SetDataExtent(0, 209, 0, 209, 0, 29); // 这里因为在000文件夹里面有0.jpg ~ 29.jpg,所以设置为 0,29 // 每张图片的长宽为210 * 210 因此设置为0,209 jpegReader.Update(); // update这里要注意一下,对于VTK在默认情况下是在最后操作时候才一次性刷新 // 也就是说如果没有自动刷新的话,在一些中间过程中是无法获得到数据的,因为没update进去 vtkContourFilter skinExtractor = new vtkContourFilter(); skinExtractor.SetInputConnection(jpegReader.GetOutputPort()); skinExtractor.SetValue(200, 100); //值越大,保留的部分越少。 //重新计算法向量 vtkPolyDataNormals skinNormals = new vtkPolyDataNormals(); skinNormals.SetInputConnection(skinExtractor.GetOutputPort()); skinNormals.SetFeatureAngle(60.0); //Specify the angle that defines a sharp edge. //If the difference in angle across neighboring polygons is greater than this value, //the shared edge is considered "sharp". //create triangle strips and/or poly-lines 为了更快的显示速度 vtkStripper skinStripper = new vtkStripper(); skinStripper.SetInputConnection(skinNormals.GetOutputPort()); vtkPolyDataMapper skinMapper = new vtkPainterPolyDataMapper(); skinMapper.SetInputConnection(skinStripper.GetOutputPort()); skinMapper.ScalarVisibilityOff(); //这样不会带颜色 vtkActor skin = new vtkActor(); skin.SetMapper(skinMapper); // An outline provides context around the data. // 一个围绕在物体的立体框,可以先忽略 /* * vtkOutlineFilter> outlineData = * vtkOutlineFilter>::New(); * outlineData.SetInputConnection(dicomReader.GetOutputPort()); * * vtkPolyDataMapper> mapOutline = * vtkPolyDataMapper>::New(); * mapOutline.SetInputConnection(outlineData.GetOutputPort()); * * vtkActor> outline = * vtkActor>::New(); * outline.SetMapper(mapOutline); * outline.GetProperty().SetColor(0,0,0); * * aRenderer.AddActor(outline); */ // It is convenient to create an initial view of the data. The FocalPoint // and Position form a vector direction. Later on (ResetCamera() method) // this vector is used to position the camera to look at the data in // this direction. vtkCamera aCamera = new vtkCamera(); aCamera.SetViewUp(0, 0, -1); aCamera.SetPosition(0, 1, 0); aCamera.SetFocalPoint(0, 0, 0); aCamera.ComputeViewPlaneNormal(); aCamera.Azimuth(30.0); aCamera.Elevation(30.0); // Actors are added to the renderer. An initial camera view is created. // The Dolly() method moves the camera towards the FocalPoint, // thereby enlarging the image. _render.AddActor(skin); _render.SetActiveCamera(aCamera); _render.ResetCamera(); aCamera.Dolly(1.5); // Set a background color for the renderer and set the size of the // render window (expressed in pixels). _render.SetBackground(.2, .3, .4); _renwin.SetSize(640, 480); // Note that when camera movement occurs (as it does in the Dolly() // method), the clipping planes often need adjusting. Clipping planes // consist of two planes: near and far along the view direction. The // near plane clips out objects in front of the plane; the far plane // clips out objects behind the plane. This way only what is drawn // between the planes is actually rendered. _render.ResetCameraClippingRange(); // Initialize the event loop and then start it. iren.Initialize(); iren.Start(); return(0); }
static void ReadSupport(string Spath) { vtkJPEGReader reader = vtkJPEGReader.New(); reader.SetFileName(Spath); reader.Update(); // Visualize _ImageViewer = vtkImageViewer2.New(); _ImageViewer.SetInputConnection(reader.GetOutputPort()); _SliceStatusMapper = vtkTextMapper.New(); _SliceStatusMapper.SetInputConnection(reader.GetOutputPort()); vtkActor2D sliceStatusActor = vtkActor2D.New(); sliceStatusActor.SetMapper(_SliceStatusMapper); vtkRenderWindow renderWindow = vtkRenderWindow.New(); //Display in full screen renderWindow.SetFullScreen(1); vtkInteractorStyleImage interactorStyle = vtkInteractorStyleImage.New(); renderWindow.GetRenderers().InitTraversal(); vtkRenderer ren; while ((ren = renderWindow.GetRenderers().GetNextItem()) != null) { renderWindow.AddRenderer(ren); } _ImageViewer.SetRenderWindow(renderWindow); _ImageViewer.GetRenderer().AddActor2D(sliceStatusActor); _ImageViewer.SetSlice(_SupportSlice); iren = vtkRenderWindowInteractor.New(); iren.SetRenderWindow(renderWindow); renderWindow.Render(); _ColorLevel = 60; _ImageViewer.SetColorLevel(_BlackLevel); _ImageViewer.Render(); Down(_period, 170); Backward(_period, 1); for (int i = 0; i < _SupportLayers; i++) { System.Threading.Thread.Sleep(_layertime); // delay Backward(_period, 1); } _ImageViewer.SetColorLevel(_BlackLevel); _ImageViewer.Render(); Forward(_period, _MovedLayers); Console.WriteLine(_MovedLayers); if (reader != null) { reader.Dispose(); } if (_ImageViewer != null) { _ImageViewer.Dispose(); } if (_SliceStatusMapper != null) { _SliceStatusMapper.Dispose(); } if (sliceStatusActor != null) { sliceStatusActor.Dispose(); } if (renderWindow != null) { renderWindow.Dispose(); } if (interactorStyle != null) { interactorStyle.Dispose(); } if (ren != null) { ren.Dispose(); } if (iren != null) { iren.Dispose(); } }