# itk::LineSpatialObject to itk::Image using itk::SpatialObjectToImageFilter?

**URL:** https://discourse.itk.org/t/itk-linespatialobject-to-itk-image-using-itk-spatialobjecttoimagefilter/7326
**Category:** Algorithms
**Created:** [November 25, 2024, 1:37am UTC](https://discourse.itk.org/t/itk-linespatialobject-to-itk-image-using-itk-spatialobjecttoimagefilter/7326 "2024-11-25T01:37:44Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)
#### Post date: [November 25, 2024, 1:37am UTC](https://discourse.itk.org/t/itk-linespatialobject-to-itk-image-using-itk-spatialobjecttoimagefilter/7326/1 "2024-11-25T01:37:44Z")

</div>

Hello Everyone,

Using itk::SpatialObjectToImageFilter works beautifully, if I have an itk::EllipseSpatialObject, because an ellipse has dimensions in the object space. BTW, full respect to Julien Jomier for using the _Composite pattern_ here (if it was Julien using the pattern) which makes it so easy to create composite objects — I looked through `blame` in the repo — the first mention of the Composite pattern use in `itk::SpatialObject.h` leads to Julien, but it could have been someone before him laying down the solid software engineering foundation for ITK, some 23 years back. @matt.mccormick — you’d probably know who it was back then. Maybe Luis Ibanez?

 ![image](https://discourse.itk.org/uploads/default/original/2X/6/6c0adbaa6d75e7216add3a53c2ac9c5d86da46fc.png)

Anyhow… Say, what if I have an `itk::SpatialObject` instance made of a bunch of `itk::LinkeSpatialObject` instances, and then I’d want that to be converted to an image? The lines don’t have any thickness, it appears, and they just don’t even show in the output image when fed as input to the `itk::SpatialObjectToImageFilter` — the image comes up blank, with just background in it.

My use case in this instance is to generate a grid (made of lines) and turn it into an image and apply a (non-rigid) transform to it obtained from image registration.

Or, _is there a better/simpler way of generating a grid of lines in an image_? Maybe using `itk::TubeSpatialObject`? But that doesn’t seem have any thickness/diameter properties either. 🤔

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [November 25, 2024, 1:54pm UTC](https://discourse.itk.org/t/itk-linespatialobject-to-itk-image-using-itk-spatialobjecttoimagefilter/7326/2 "2024-11-25T13:54:16Z")

</div>

Hello @constantine,

The filter you are looking for is [GridImageSource](https://itk.org/Doxygen/html/classitk_1_1GridImageSource.html).

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [November 25, 2024, 5:07pm UTC](https://discourse.itk.org/t/itk-linespatialobject-to-itk-image-using-itk-spatialobjecttoimagefilter/7326/3 "2024-11-25T17:07:15Z")

</div>

For arbitrary lines, one could use [BresenhamLine](https://itk.org/Doxygen/html/classitk_1_1BresenhamLine.html) or [LineIterator](https://itk.org/Doxygen/html/classitk_1_1LineIterator.html) to convert a line into voxels/pixels.

---

<div class="post-metadata">

### Author: ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)
#### Post date: [November 26, 2024, 5:22am UTC](https://discourse.itk.org/t/itk-linespatialobject-to-itk-image-using-itk-spatialobjecttoimagefilter/7326/4 "2024-11-26T05:22:59Z")

</div>

@zivy — thank you so much! And I’m baffled how I overlooked this `itk::GridImageSource`, because last night I was `"grid"-CTL-F-ing` though the ITK Doxygen Class Index, and yet I didn’t notice it… 🙄

This works like magic, and with the Insight Journal paper ([https://doi.org/10.54294/ze1ssz](https://doi.org/10.54294/ze1ssz)) — it is so clear and totally gourmet. This is all it takes:

```auto
import itk
import matplotlib.pyplot as plt

grid_source = itk.GridImageSource[itk.Image[itk.UC, 2]].New()
grid_source.SetSize([1024, 1024])
grid_source.SetSpacing([22/1024, 22/1024])
grid_source.SetGridSpacing([1, 1])
grid_source.SetSigma([0.05,0.05])
grid_source.Update()

grid_img = grid_source.GetOutput()
plt.imshow(grid_img)
plt.show()

```

to produce a fine (as in spectacular) grid:

![image](https://discourse.itk.org/uploads/default/original/2X/1/185cf346254e1277d2135beedd214b2e33179257.png)

However, I’m running into a bit of trouble, when I try to swap the default kernel function, which is `itk::GaussianKernelFunction`, i.e.:

```auto
print(grid_source.GetKernelFunction())
GaussianKernelFunction (000002214CB21630)
  RTTI typeinfo: class itk::GaussianKernelFunction<double>
...

```

to, say, `itk::BSplineKernelFunction` — it appears neither of the kernel function classes are Python-wrapped (apart from the `itk::KernelFunctionBase`) where Python’s `itk` package file listing shows only this:

 ![image](https://discourse.itk.org/uploads/default/original/2X/9/96a29586865bb22b313a64da73c583295a5f7758.png)

My follow-up question is this: _Is there a way around it — somehow creating (or borrowing) an instance of `BSplineKernelFunction` and passing it to the `GridImageSource` object — short of doing my own ITK Python wrapping?_

---

<div class="post-metadata">

### Author: ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)
#### Post date: [November 26, 2024, 5:24am UTC](https://discourse.itk.org/t/itk-linespatialobject-to-itk-image-using-itk-spatialobjecttoimagefilter/7326/5 "2024-11-26T05:24:27Z")

</div>

Ah, those classes too look very handy, perhaps for more involved use cases. Thank you, @dzenanz!
