[Insight-users] using spatialObjects in ManagedITK

Dan Mueller dan.muel at gmail.com
Wed Jul 23 03:28:43 EDT 2008


Hi Shady,

Please find attached the following code which I have tested and
verifies works correctly.

The differences are the following:
   1. When you create a new itkPoint or itkContinuousIndex be careful
to use itkPoint.New(3U) in the constructor. This creates a
three-dimensional empty object, rather than itkPoint.New(3) which
(may) create a 1 dimensional object with value "3".
   2. The input image must be three-dimensional (I don't think
"brain.tif" is 3-D).
   3. Ensure all vertices added to the path are inside the largest
possible region of the image. If not, you can receive the error
message you reported. (Without further investigation, I'm not sure if
this is a problem with the wrappings or with ITK itself...)

Hope this helps.

Regards, Dan

using System;
using itk;

namespace ItkPathTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // Read image
            itkImage_UC3 input = itkImage_UC3.New();
            itkImage_UC3 output = itkImage_UC3.New();
            input.Read("D:/Temp/engine.mhd");

            // Create path
            itkPolyLineParametricPath_3 path =
                itkPolyLineParametricPath_3.New();
            itkPoint point = new itkPoint(3U);
            itkContinuousIndex vertex = new itkContinuousIndex(3U);
            point[0] = 50.0;
            point[1] = 50.0;
            point[2] = 50.0;
            input.TransformPhysicalPointToContinuousIndex(point, out vertex);
            path.AddVertex(vertex);
            point[0] = 200.0;
            point[1] = 200.0;
            point[2] = 100.0;
            input.TransformPhysicalPointToContinuousIndex(point, out vertex);
            path.AddVertex(vertex);

            // Convert path to image
            itkPathToImageFilter_PLPP3IUC3 pathToImage =
                itkPathToImageFilter_PLPP3IUC3.New();
            pathToImage.Size = input.Size;
            pathToImage.Spacing = input.Spacing;
            pathToImage.Origin = input.Origin;
            pathToImage.SetInput(path);
            pathToImage.PathValue = 255;
            pathToImage.BackgroundValue = 0;
            pathToImage.UpdateLargestPossibleRegion();
            pathToImage.GetOutput(output);
            output.Write("D:/Temp/Path01.mhd");
        }
    }
}


2008/7/22 Shady Shidfar <shady_shidfar at yahoo.com>:
> Hi Dan,
>
> Thanks for the file, I guess I'll wait for you to solve the problem :-). Now
> instead of using SpatialObjects I'm trying to
> use itkPolyLineParametricPath  to draw contours on my image, then use
> itkPathToImageFilter to display them on the image. The problem is that I get
> a weird message. Could you please have a look at the following code and see
> what the problem is.
>
>
>
> the error is: Attempted to read or write protected memory. This is often an
> indication that other memory is corrupt.
>
>
>
> using
>
> System;
>
> using
>
> System.Collections.Generic;
>
> using
>
> System.Linq;
>
> using
>
> System.Windows.Forms;
>
> namespace
>
> InteractiveHierarchicalSegmentationDotNet
>
> {
>
> static class Program
>
> {
>
> /// <summary>
>
> /// The main entry point for the application.
>
> /// </summary>
>
> [
>
> STAThread]
>
> static void Main()
>
> {
>
> //Application.EnableVisualStyles();
>
> //Application.SetCompatibleTextRenderingDefault(false);
>
> //Application.Run(new Form1());
>
> Application.Run(new TestForm());
>
> }
>
> }
>
> }
>
>
>
>
>
>
>
>
>
> using
>
> System;
>
> using
>
> System.Collections.Generic;
>
> using
>
> System.ComponentModel;
>
> using
>
> System.Data;
>
> using
>
> System.Drawing;
>
> using
>
> System.Linq;
>
> using
>
> System.Text;
>
> using
>
> System.Windows.Forms;
>
> using
>
> vtk;
>
> using
>
> itk;
>
> namespace
>
> InteractiveHierarchicalSegmentationDotNet
>
> {
>
> public partial class TestForm : Form
>
> {
>
> public TestForm()
>
> {
>
> InitializeComponent();
>
> try
>
> {
>
> // Read ITK image
>
> itkImage_UC3 inputImage = itkImage_UC3.New();
>
> inputImage.Read(
>
> "C:/brain.tif");
>
> inputImage.DisconnectPipeline();
>
> // Import ITK image to VTK
>
> itkImageToVTKImageFilter itk2vtk =
>
> itkImageToVTKImageFilter.New(inputImage);
>
> itk2vtk.SetInput(inputImage);
>
> itk2vtk.Update();
>
> vtkImageData data = itk2vtk.GetOutput();
>
> vtkImageActor actor = new vtkImageActor();
>
> actor.SetInput(data);
>
> Console.WriteLine(actor.ToString());
>
> vtkRenderer vtkRenderer1 = new vtkRenderer();
>
> this.vtkFormsWindowControl1.GetRenderWindow().AddRenderer(vtkRenderer1);
>
> vtkRenderer1.AddActor(actor);
>
>
>
> // define and display a path
>
> itkPoint origin = inputImage.Origin;
>
> itkSpacing spacing = inputImage.Spacing;
>
> itkSize size = inputImage.Size;
>
> itkPoint point = new itkPoint(3);
>
> itkPolyLineParametricPath_3 path = itkPolyLineParametricPath_3.New();
>
> point[0] = origin[0] + spacing[0] * size[0];
>
> point[1] = origin[1] + spacing[1] * size[1];
>
> //point[3] = origin[2] + spacing[2] * size[2];
>
> itkContinuousIndex cindex = new itkContinuousIndex(3);
>
> inputImage.TransformPhysicalPointToContinuousIndex(origin,
>
> out cindex);
>
> path.AddVertex(cindex);
>
> inputImage.TransformPhysicalPointToContinuousIndex(point,
>
> out cindex);
>
> path.AddVertex(cindex);
>
> itkPathToImageFilter_PLPP3IUC3 path2ImageFilter =
> itkPathToImageFilter_PLPP3IUC3.New();
>
> path2ImageFilter.SetInput(path);
>
> path2ImageFilter.PathValue = 200;
>
> path2ImageFilter.GetOutput(inputImage);
>
> }
>
> catch (Exception ex)
>
> {
>
> Console.WriteLine(ex);
>
> }
>
> }
>
> }
>
> }
>
>
>
>
>
> I can send you the whole project if it helps.
>
>
>
> Cheers, Shaady
>
>
>
>
>
> ----- Original Message ----
> From: Dan Mueller <dan.muel at gmail.com>
> To: Shady Shidfar <shady_shidfar at yahoo.com>
> Sent: Friday, 18 July, 2008 3:21:07 PM
> Subject: Re: [Insight-users] using spatialObjects in ManagedITK
>
> Hi Shady,
>
> SpatialObjects are a whole separate part of ITK, take for example the
> SpatialObjectPoint class which is a whole new entity. Because the
> wrapping used by ManagedITK is not automated, infrastructure (in the
> form of type and wrapper templates) must be provided. Wrapping
> SpatialObjects requires a whole new set of infrastructure classes
> which I have not had time to implement yet. Some of the classes were
> easy to wrap and therefore have been done so. The other classes (like
> BlobSpatialObject) are more complex and have not been done. In
> summary: it is not quick and easy to wrap these classes.
>
> However, there is a (quick) hack which may help you. TubeSpatialObject
> is also rather complex, but I have managed to hack together a wrapper.
> If you open "managed_itkTubeSpatialObject.cmake" you will see a lot of
> hacked code. You can do something similar with BlobSpatialObject.
>
> I have attached a file which should get you started. I have *not*
> configured/compiled this file; there ==WILL== be errors. The
> SetPoints() method is just a rough guess I quick slapped together in
> notepad... Like I said, this is a hack until I get the time to do it
> properly.
>
> I might be able to take a look at the issue myself in the coming
> weeks, but I am really busy at the moment. Hopefully this will be
> enough to get you started if you really really need it...
>
> Regards, Dan
>
> 2008/7/18 Shady Shidfar <shady_shidfar at yahoo.com>:
>> Thanks Dan,
>> So is there anyway to wrap them myself them and add them to manageITK?
>>
>> thanks
>> Shady
>> ----- Original Message ----
>> From: Dan Mueller <dan.muel at gmail.com>
>> To: Shady Shidfar <shady_shidfar at yahoo.com>
>> Cc: insight users <insight-users at itk.org>
>> Sent: Tuesday, 15 July, 2008 2:29:19 PM
>> Subject: Re: [Insight-users] using spatialObjects in ManagedITK
>>
>> Hi Shady,
>>
>> Unfortunately ManagedITK does not yet wrap all SpatialObjects. The
>> following *are* wrapped:
>>  itkEllipseSpatialObject
>>  itkGroupSpatialObject
>>  itkPlaneSpatialObject
>>  itkSceneSpatialObject
>>  itkSpatialObject
>>  itkSpatialObjectReader
>>  itkSpatialObjectWriter
>>  itkTubeSpatialObject
>>  itkTubeSpatialObjectPoint
>>
>> Sorry for the bad news.
>>
>> Regards, dan
>>
>> 2008/7/15 Shady Shidfar <shady_shidfar at yahoo.com>:
>>> Hi Dan,
>>>
>>>
>>>
>>> I'm having a problem with spatial objects.I need to draw objects on my
>>> image
>>> so decided to work with itkSpacialObjects. I am trying  to get the
>>> following
>>> C++ code to work with managedITK, but I can't find itkBlobSpatialObject
>>> and
>>> itkSpatialObjectPoint.
>>>
>>>
>>>
>>> I've added ManagedITK.SpatialObjects and ManagedITK.Common as reference.
>>> Are
>>> they enough?
>>>
>>>
>>>
>>> Thanks for your help
>>>
>>> Shaady
>>>
>>>
>>>
>>> #if
>>>
>>> defined(_MSC_VER)
>>>
>>> #pragma
>>>
>>> warning ( disable : 4786 )
>>>
>>> #endif
>>>
>>>
>>>
>>> #include
>>>
>>> "itkBlobSpatialObject.h"
>>>
>>>
>>>
>>> #include
>>>
>>> "itkSpatialObjectPoint.h"
>>>
>>> // Software Guide : EndCodeSnippet
>>>
>>> int
>>>
>>> main( int, char *[] )
>>>
>>> {
>>>
>>> typedef itk::BlobSpatialObject<3> BlobType;
>>>
>>> typedef BlobType::Pointer BlobPointer;
>>>
>>> typedef itk::SpatialObjectPoint<3> BlobPointType;
>>>
>>>
>>>
>>> BlobType::PointListType list;
>>>
>>> for( unsigned int i=0; i<4; i++)
>>>
>>> {
>>>
>>> BlobPointType p;
>>>
>>> p.SetPosition(i,i+1,i+2);
>>>
>>> p.SetRed(1);
>>>
>>> p.SetGreen(0);
>>>
>>> p.SetBlue(0);
>>>
>>> p.SetAlpha(1.0);
>>>
>>> list.push_back(p);
>>>
>>> }
>>>
>>>
>>>
>>> BlobPointer blob = BlobType::New();
>>>
>>> blob->GetProperty()->SetName(
>>>
>>> "My Blob");
>>>
>>> blob->SetId(1);
>>>
>>> blob->SetPoints(list);
>>>
>>>
>>>
>>> BlobType::PointListType pointList = blob->GetPoints();
>>>
>>> std::cout <<
>>>
>>> "The blob contains " << pointList.size();
>>>
>>> std::cout <<
>>>
>>> " points" << std::endl;
>>>
>>>
>>>
>>> BlobType::PointListType::const_iterator it = blob->GetPoints().begin();
>>>
>>> while(it != blob->GetPoints().end())
>>>
>>> {
>>>
>>> std::cout <<
>>>
>>> "Position = " << (*it).GetPosition() << std::endl;
>>>
>>> std::cout <<
>>>
>>> "Color = " << (*it).GetColor() << std::endl;
>>>
>>> it++;
>>>
>>> }
>>>
>>>
>>>
>>> return 0;
>>>
>>> }
>>>
>>> ________________________________
>>> Not happy with your email address?
>>> Get the one you really want - millions of new email addresses available
>>> now
>>> at Yahoo!
>>> _______________________________________________
>>> Insight-users mailing list
>>> Insight-users at itk.org
>>> http://www.itk.org/mailman/listinfo/insight-users
>>>
>>>
>>
>> ________________________________
>> Not happy with your email address?
>> Get the one you really want - millions of new email addresses available
>> now
>> at Yahoo!
>
> ________________________________
> Not happy with your email address?
> Get the one you really want - millions of new email addresses available now
> at Yahoo!


More information about the Insight-users mailing list