In this tutorial you will learn how to create a 3D object.
Start Visual Studio 2005 or later
Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.MedicalViewer Imports Leadtools.Medical3D Imports Leadtools.Dicom[C#]
using Leadtools; using Leadtools.Codecs; using Leadtools.MedicalViewer; using Leadtools.Medical3D; using Leadtools.Dicom;
InitClass()
Private Sub InitClass() RasterSupport.SetLicense(RasterSupportType.Dicom, " Your Dicom Key") RasterSupport.SetLicense(RasterSupportType.Medical, "Your Medical Key") RasterSupport.SetLicense(RasterSupportType.Medical3d, "Your Medical 3D Key") ' Create a new instance of the codecs class, which will be used to load the images. Dim _codecs As RasterCodecs = New RasterCodecs() ' Create a new instance of the Medical Viewer, the layout will be divided to 2X2. Dim viewer As MedicalViewer = New MedicalViewer(2, 2) ' Fit the view to the whole form viewer.Dock = DockStyle.Fill ' Create the 3D control that will hold the 3D object. Dim control3D As Medical3DControl = New Medical3DControl() ' Add and set 3D action control3D.AddAction(MedicalViewerActionType.WindowLevel) control3D.SetAction(MedicalViewerActionType.WindowLevel, MedicalViewerMouseButtons.Left, MedicalViewerActionFlags.Active) ' Load any multi frame image (with number of frames more than 3) and add it to the Image property of the cell. Dim object3D As Medical3DObject = New Medical3DObject() ' Add the newly created 3D object to the control. control3D.ObjectsContainer.Objects.Add(object3D) ' Add the cell above to the MedicalViewer. viewer.Cells.Add(control3D) ' add the viewer to the from control. Controls.Add(viewer) End Sub[C#]
void InitClass() { RasterSupport.SetLicense(RasterSupportType.Dicom, " Your Dicom Key"); RasterSupport.SetLicense(RasterSupportType.Medical, "Your Medical Key"); RasterSupport.SetLicense(RasterSupportType.Medical3d, "Your Medical 3D Key"); // Create a new instance of the codecs class, which will be used to load the images. RasterCodecs _codecs = new RasterCodecs(); // Create a new instance of the Medical Viewer, the layout will be divided to 2X2. MedicalViewer viewer = new MedicalViewer(2, 2); // Fit the view to the whole form viewer.Dock = DockStyle.Fill; // Create the 3D control that will hold the 3D object. Medical3DControl control3D = new Medical3DControl(); // Add and set 3D action control3D.AddAction(MedicalViewerActionType.WindowLevel); control3D.SetAction(MedicalViewerActionType.WindowLevel, MedicalViewerMouseButtons.Left, MedicalViewerActionFlags.Active); // Load any multi frame image (with number of frames more than 3) and add it to the Image property of the cell. Medical3DObject object3D = new Medical3DObject(); // Add the newly created 3D object to the control. control3D.ObjectsContainer.Objects.Add(object3D); // the the cell above to the MedicalViewer. viewer.Cells.Add(control3D); // Add the viewer to the from control. Controls.Add(viewer); }
InitializeComponent().
Add the following code to your project right under the initclass()
. This code will load a specific study and series from the downloaded DICOMDIR file. Note:There is an sample dialog “SeriesBrowser” that can be found in the LEADTOOLS 17.5\Examples\DotNet\<VB or CS>\Common folder, which will do all the work below. For more information, refer to the Main3DDemo source code.
Private _studyElement As DicomElement Private _seriesElement As DicomElement Private _seriesManager As MedicalViewerSeriesManager Private _imageDataList As List(Of MedicalViewerImageData) Private doubleArray As Double() Private patientElement As DicomElement Private referenceUID As String Private imageElement As DicomElement Private output As MedicalViewerSeriesManager ' Find the study using the Study instance UID, and return it's DicomElement if the study is found Private Function FindStudy(ByVal ds As DicomDataSet, ByVal studyInstanceUID As String) As DicomElement ' get the parent element. Dim patientElement As DicomElement = ds.GetFirstKey(Nothing, True) Dim studyElement As DicomElement = Nothing Dim studyInformationElement As DicomElement = Nothing Dim studyID As String studyElement = ds.GetChildKey(patientElement) studyElement = ds.GetChildElement(studyElement, True) Do While Not studyElement Is Nothing studyInformationElement = ds.FindFirstElement(studyElement, DicomTag.StudyInstanceUID, True) If Not studyInformationElement Is Nothing Then studyID = ds.GetConvertValue(studyInformationElement) If studyID = studyInstanceUID Then Return studyInformationElement End If End If studyElement = ds.GetNextKey(studyElement, True) studyElement = ds.GetChildElement(studyElement, True) Loop Return Nothing End Function ' Find the series using the Series instance UID, and return it's DicomElement if the series is found Private Function FindSeries(ByVal ds As DicomDataSet, ByVal studyElement As DicomElement, ByVal seriesInstanceUID As String) As DicomElement Dim seriesElement As DicomElement = Nothing Dim seriesInformationElement As DicomElement = Nothing Dim seriesID As String seriesElement = ds.GetChildKey(studyElement) seriesElement = ds.GetChildElement(seriesElement, True) Do While Not seriesElement Is Nothing seriesInformationElement = ds.FindFirstElement(seriesElement, DicomTag.SeriesInstanceUID, True) If Not seriesInformationElement Is Nothing Then seriesID = ds.GetConvertValue(seriesInformationElement) If seriesID = seriesInstanceUID Then Return seriesInformationElement End If End If seriesElement = ds.GetNextKey(seriesElement, True) seriesElement = ds.GetChildElement(seriesElement, True) Loop Return Nothing End Function ' return the first frame file name of the series. Private Function GetFirstImageName(ByVal ds As DicomDataSet, ByVal seriesElement As DicomElement, ByVal directoryPath As String, <System.Runtime.InteropServices.Out()> ByRef imageElement As DicomElement) As String Dim imageIDElement As DicomElement = Nothing imageElement = ds.GetChildKey(seriesElement) imageElement = ds.GetChildElement(imageElement, True) Do While Not imageElement Is Nothing imageIDElement = ds.FindFirstElement(imageElement, DicomTag.ReferencedFileID, True) If Not imageIDElement Is Nothing Then Return directoryPath & "\" & ds.GetConvertValue(imageIDElement) End If Loop Return "" End Function ' return the next frame file name of the series. Private Function GetNextImageName(ByVal ds As DicomDataSet, ByVal directoryPath As String, ByRef imageElement As DicomElement) As String Dim nextImageElement As DicomElement = Nothing imageElement = ds.GetNextKey(imageElement, True) imageElement = ds.GetChildElement(imageElement, True) Do While Not imageElement Is Nothing nextImageElement = ds.FindFirstElement(imageElement, DicomTag.ReferencedFileID, True) If Not imageElement Is Nothing Then Dim echoElement As DicomElement = ds.FindFirstElement(imageElement, DicomTag.EchoNumber, True) Return directoryPath & "\" & ds.GetConvertValue(nextImageElement) End If Loop Return "" End Function ' This will load the dicom data set information and save it into a new instance of the class MedicalViewerImageData. Private Function AddImageToImageArray(ByVal ds As DicomDataSet, ByVal index As Integer, ByVal imagePath As String, <System.Runtime.InteropServices.Out()> ByRef echoNumber As Integer) As Boolean echoNumber = -1 Dim imageData As MedicalViewerImageData = New MedicalViewerImageData() patientElement = ds.FindFirstElement(Nothing, DicomTag.ImagePositionPatient, True) doubleArray = ds.GetDoubleValue(patientElement, 0, 3) imageData.ImagePosition = Point3D.FromDoubleArray(doubleArray) imageData.Data = imagePath imageData.EchoNumber = echoNumber patientElement = ds.FindFirstElement(Nothing, DicomTag.FrameOfReferenceUID, True) referenceUID = ds.GetConvertValue(patientElement) imageData.FrameOfReferenceUID = referenceUID patientElement = ds.FindFirstElement(Nothing, DicomTag.ImageOrientationPatient, True) imageData.ImageOrientation = ds.GetConvertValue(patientElement) patientElement = ds.FindFirstElement(Nothing, DicomTag.PixelSpacing, True) doubleArray = ds.GetDoubleValue(patientElement, 0, 2) imageData.PixelSpacing = New Point2D(CSng(doubleArray(0)), CSng(doubleArray(1))) patientElement = ds.FindFirstElement(Nothing, DicomTag.InstanceNumber, True) If Not patientElement Is Nothing Then imageData.InstanceNumber = Convert.ToInt32(ds.GetConvertValue(patientElement)) End If patientElement = ds.FindFirstElement(Nothing, DicomTag.InstanceCreationTime, True) If Not patientElement Is Nothing Then imageData.CaptureTime = Convert.ToDateTime(ds.GetConvertValue(patientElement)) End If _imageDataList.Add(imageData) Return True End Function Public Function Load_Chest_CT_Compressed() As MedicalViewerSeriesManager Dim fileName As String = "C:\Leadtools_DICOMDIR\" Dim studyInstanceUID As String = "1.3.12.2.1107.5.99.2.5562.4.0.575080331232768" Dim seriesInstanceUID As String = "1.3.12.2.1107.5.99.2.5562.4.0.575080411334689" Return LoadSeries(fileName, studyInstanceUID, seriesInstanceUID, 158) End Function ' this will load the series specified by the seriesInstanceUID and studyInstanceUID from the file specified in fileName Private Function LoadSeries(ByVal fileName As String, ByVal studyInstanceUID As String, ByVal seriesInstanceUID As String, ByVal count As Integer) As MedicalViewerSeriesManager DicomEngine.Startup() Dim ds As DicomDataSet = New DicomDataSet() ds.Load(fileName & "DICOMDIR", DicomDataSetLoadFlags.None) Dim directoryPath As String = fileName ' Here the program will search for the study with the specified studyInstanceUID. _studyElement = FindStudy(ds, studyInstanceUID) ' Here the program will search for the series with the specified seriesInstanceUID. _seriesElement = FindSeries(ds, _studyElement, seriesInstanceUID) ' create a new instance of the MedicalViewerSeriesManager which will be used to sort the images, in order to create the correct 3D object. _seriesManager = New MedicalViewerSeriesManager() ' create an array of MedicalViewerImageData. This class will be used to save the frame information. This information will be used to sort the images. _imageDataList = New List(Of MedicalViewerImageData)() Dim dicomDataSet As DicomDataSet Dim imageIndex As Integer Dim imagePath As String Dim echoNumber As Integer = 0 ' Now the program will go through each frame in the series imagePath = GetFirstImageName(ds, _seriesElement, directoryPath, imageElement) imageIndex = 0 Do While imageIndex < count Try ' each image in the series will be loaded. dicomDataSet = New DicomDataSet() dicomDataSet.Load(imagePath, DicomDataSetLoadFlags.None) ' The program will load it's information and save it into a new instance of the class MedicalViewerImageData. AddImageToImageArray(dicomDataSet, imageIndex, imagePath, echoNumber) dicomDataSet.Dispose() ' go to the next image. imagePath = GetNextImageName(ds, directoryPath, imageElement) Catch exception As System.Exception System.Diagnostics.Debug.Assert(False, exception.Message) Throw End Try imageIndex += 1 Loop ' sort the images, based on its data. _seriesManager.Sort(_imageDataList) DicomEngine.Shutdown() Return _seriesManager End Function
[C#]
DicomElement _studyElement; DicomElement _seriesElement; MedicalViewerSeriesManager _seriesManager; List<MedicalViewerImageData> _imageDataList; double[] doubleArray; DicomElement patientElement; string referenceUID; DicomElement imageElement; MedicalViewerSeriesManager output; // Find the study using the Study instance UID, and return it's DicomElement if the study is found private DicomElement FindStudy(DicomDataSet ds, string studyInstanceUID) { // get the parent element. DicomElement patientElement = ds.GetFirstKey(null, true); DicomElement studyElement = null; DicomElement studyInformationElement = null; string studyID; studyElement = ds.GetChildKey(patientElement); studyElement = ds.GetChildElement(studyElement, true); while (studyElement != null) { studyInformationElement = ds.FindFirstElement(studyElement, DicomTag.StudyInstanceUID, true); if (studyInformationElement != null) { studyID = ds.GetConvertValue(studyInformationElement); if (studyID == studyInstanceUID) return studyInformationElement; } studyElement = ds.GetNextKey(studyElement, true); studyElement = ds.GetChildElement(studyElement, true); } return null; } // Find the series using the Series instance UID, and return it's DicomElement if the series is found private DicomElement FindSeries(DicomDataSet ds, DicomElement studyElement, string seriesInstanceUID) { DicomElement seriesElement = null; DicomElement seriesInformationElement = null; string seriesID; seriesElement = ds.GetChildKey(studyElement); seriesElement = ds.GetChildElement(seriesElement, true); while (seriesElement != null) { seriesInformationElement = ds.FindFirstElement(seriesElement, DicomTag.SeriesInstanceUID, true); if (seriesInformationElement != null) { seriesID = ds.GetConvertValue(seriesInformationElement); if (seriesID == seriesInstanceUID) return seriesInformationElement; } seriesElement = ds.GetNextKey(seriesElement, true); seriesElement = ds.GetChildElement(seriesElement, true); } return null; } // return the first frame file name of the series. private string GetFirstImageName(DicomDataSet ds, DicomElement seriesElement, string directoryPath, out DicomElement imageElement) { DicomElement imageIDElement = null; imageElement = ds.GetChildKey(seriesElement); imageElement = ds.GetChildElement(imageElement, true); while (imageElement != null) { imageIDElement = ds.FindFirstElement(imageElement, DicomTag.ReferencedFileID, true); if (imageIDElement != null) { return directoryPath + "\\" + ds.GetConvertValue(imageIDElement); } } return ""; } // return the next frame file name of the series. private string GetNextImageName(DicomDataSet ds, string directoryPath, ref DicomElement imageElement) { DicomElement nextImageElement = null; imageElement = ds.GetNextKey(imageElement, true); imageElement = ds.GetChildElement(imageElement, true); while (imageElement != null) { nextImageElement = ds.FindFirstElement(imageElement, DicomTag.ReferencedFileID, true); if (imageElement != null) { DicomElement echoElement = ds.FindFirstElement(imageElement, DicomTag.EchoNumber, true); return directoryPath + "\\" + ds.GetConvertValue(nextImageElement); } } return ""; } // This will load the dicom data set information and save it into a new instance of the class MedicalViewerImageData. private bool AddImageToImageArray(DicomDataSet ds, int index, string imagePath, out int echoNumber) { echoNumber = -1; MedicalViewerImageData imageData = new MedicalViewerImageData(); patientElement = ds.FindFirstElement(null, DicomTag.ImagePositionPatient, true); doubleArray = ds.GetDoubleValue(patientElement, 0, 3); imageData.ImagePosition = Point3D.FromDoubleArray(doubleArray); imageData.Data = imagePath; imageData.EchoNumber = echoNumber; patientElement = ds.FindFirstElement(null, DicomTag.FrameOfReferenceUID, true); referenceUID = ds.GetConvertValue(patientElement); imageData.FrameOfReferenceUID = referenceUID; patientElement = ds.FindFirstElement(null, DicomTag.ImageOrientationPatient, true); imageData.ImageOrientation = ds.GetConvertValue(patientElement); patientElement = ds.FindFirstElement(null, DicomTag.PixelSpacing, true); doubleArray = ds.GetDoubleValue(patientElement, 0, 2); imageData.PixelSpacing = new Point2D((float)doubleArray[0], (float)doubleArray[1]); patientElement = ds.FindFirstElement(null, DicomTag.InstanceNumber, true); if (patientElement != null) imageData.InstanceNumber = Convert.ToInt32(ds.GetConvertValue(patientElement)); patientElement = ds.FindFirstElement(null, DicomTag.InstanceCreationTime, true); if (patientElement != null) imageData.CaptureTime = Convert.ToDateTime(ds.GetConvertValue(patientElement)); _imageDataList.Add(imageData); return true; } public MedicalViewerSeriesManager Load_Chest_CT_Compressed() { string fileName = @"C:\Leadtools_DICOMDIR\"; string studyInstanceUID = "1.3.12.2.1107.5.99.2.5562.4.0.575080331232768"; string seriesInstanceUID = "1.3.12.2.1107.5.99.2.5562.4.0.575080411334689"; return LoadSeries(fileName, studyInstanceUID, seriesInstanceUID, 158); } // this will load the series specified by the seriesInstanceUID and studyInstanceUID from the file specified in fileName private MedicalViewerSeriesManager LoadSeries(string fileName, string studyInstanceUID, string seriesInstanceUID, int count) { DicomEngine.Startup(); DicomDataSet ds = new DicomDataSet(); ds.Load(fileName + "DICOMDIR", DicomDataSetLoadFlags.None); string directoryPath = fileName; // Here the program will search for the study with the specified studyInstanceUID. _studyElement = FindStudy(ds, studyInstanceUID); // Here the program will search for the series with the specified seriesInstanceUID. _seriesElement = FindSeries(ds, _studyElement, seriesInstanceUID); // create a new instance of the MedicalViewerSeriesManager which will be used to sort the images, in order to create the correct 3D object. _seriesManager = new MedicalViewerSeriesManager(); // create an array of MedicalViewerImageData. This class will be used to save the frame information. This information will be used to sort the images. _imageDataList = new List<MedicalViewerImageData>(); DicomDataSet dicomDataSet; int imageIndex; string imagePath; int echoNumber = 0; // Now the program will go through each frame in the series imagePath = GetFirstImageName(ds, _seriesElement, directoryPath, out imageElement); for (imageIndex = 0; imageIndex < count; imageIndex++) { try { // each image in the series will be loaded. dicomDataSet = new DicomDataSet(); dicomDataSet.Load(imagePath, DicomDataSetLoadFlags.None); // The program will load it's information and save it into a new instance of the class MedicalViewerImageData. AddImageToImageArray(dicomDataSet, imageIndex, imagePath, out echoNumber); dicomDataSet.Dispose(); // go to the next image. imagePath = GetNextImageName(ds, directoryPath, ref imageElement); } catch (System.Exception exception) { System.Diagnostics.Debug.Assert(false, exception.Message); throw; } } // sort the images, based on its data. _seriesManager.Sort(_imageDataList); DicomEngine.Shutdown(); return _seriesManager; }
Private Sub Load3DObjectUsingDICOMDIR(ByVal object3D As Medical3DObject) ' start up the codes, which allows loading various images. Dim _codecs As RasterCodecs = New RasterCodecs() ' Load the CT dicom dir and return arranged stack of images. Dim output As MedicalViewerSeriesManager = Load_Chest_CT_Compressed() ' Initialize loading the object. by specfiying the number of slices that will construct the 3D object. object3D.MemoryEfficientInit(output.Stacks(0).Items.Count) ' loop through the images and add them one by one to the created 3D object Dim image As RasterImage Dim depth As Integer = output.Stacks(0).Items.Count Dim index As Integer index = 0 Do While index < depth image = _codecs.Load(CStr(output.Stacks(0).Items(index).Data), 0, CodecsLoadByteOrder.BgrOrGrayOrRomm, 1, 1) object3D.MemoryEfficientSetFrame(image, index, output.Stacks(0).Items(index).ImagePosition, True) index += 1 Loop ' wrap up the creation process by calling this method. object3D.MemoryEfficientEnd(output.Stacks(0).Items(0).ImageOrientationArray, output.Stacks(0).PixelSpacing) End Sub Private Sub Load2DCell(ByVal cell As MedicalViewerMultiCell) ' start up the codes, which allows loading various images. Dim _codecs As RasterCodecs = New RasterCodecs() ' Load the CT dicom dir and return arranged stack of images. Dim output As MedicalViewerSeriesManager = Load_Chest_CT_Compressed() ' loop through the images and add them one by one to the final image Dim image As RasterImage = Nothing Dim depth As Integer = output.Stacks(0).Items.Count Dim index As Integer index = 0 Do While index < depth If image Is Nothing Then image = _codecs.Load(CStr(output.Stacks(0).Items(index).Data), 0, CodecsLoadByteOrder.BgrOrGrayOrRomm, 1, 1) Else image.AddPage(_codecs.Load(CStr(output.Stacks(0).Items(index).Data), 0, CodecsLoadByteOrder.BgrOrGrayOrRomm, 1, 1)) End If index += 1 Loop cell.Image = image End Sub[C#]
void Load3DObjectUsingDICOMDIR(Medical3DObject object3D) { // start up the codes, which allows loading various images. RasterCodecs _codecs = new RasterCodecs(); // Load the CT dicom dir and return arranged stack of images. MedicalViewerSeriesManager output = Load_Chest_CT_Compressed(); // Initialize loading the object. by specfiying the number of slices that will construct the 3D object. object3D.MemoryEfficientInit(output.Stacks[0].Items.Count); // loop through the images and add them one by one to the created 3D object RasterImage image; int depth = output.Stacks[0].Items.Count; int index; for (index = 0; index < depth; index++) { image = _codecs.Load((string)output.Stacks[0].Items[index].Data, 0, CodecsLoadByteOrder.BgrOrGrayOrRomm, 1, 1); object3D.MemoryEfficientSetFrame(image, index, output.Stacks[0].Items[index].ImagePosition, true); } // wrap up the creation process by calling this method. object3D.MemoryEfficientEnd(output.Stacks[0].Items[0].ImageOrientationArray, output.Stacks[0].PixelSpacing); } void Load2DCell(MedicalViewerMultiCell cell) { // start up the codes, which allows loading various images. RasterCodecs _codecs = new RasterCodecs(); // Load the CT dicom dir and return arranged stack of images. MedicalViewerSeriesManager output = Load_Chest_CT_Compressed(); // loop through the images and add them one by one to the final image RasterImage image = null; int depth = output.Stacks[0].Items.Count; int index; for (index = 0; index < depth; index++) { if (image == null) { image = _codecs.Load((string)output.Stacks[0].Items[index].Data, 0, CodecsLoadByteOrder.BgrOrGrayOrRomm, 1, 1); } else image.AddPage(_codecs.Load((string)output.Stacks[0].Items[index].Data, 0, CodecsLoadByteOrder.BgrOrGrayOrRomm, 1, 1)); } cell.Image = image; }
Load3DObjectUsingDICOMDIR(object3D)[C#]
Load3DObjectUsingDICOMDIR(object3D);
Note: For more information on how to acquire the above unlock keys, please contact LEAD Technologies support.
Note: If you encounter an "Invalid File Format" or "Feature Not Supported" exception, please refer to the topic Invalid File Format/Feature Not Supported.