# Line structuring element in any orientations

**URL:** https://discourse.itk.org/t/line-structuring-element-in-any-orientations/2004
**Category:** Beginner Questions
**Created:** [June 29, 2019, 10:45am UTC](https://discourse.itk.org/t/line-structuring-element-in-any-orientations/2004 "2019-06-29T10:45:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Jonas](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@Jonas](https://discourse.itk.org/u/Jonas)
#### Post date: [June 29, 2019, 10:45am UTC](https://discourse.itk.org/t/line-structuring-element-in-any-orientations/2004/1 "2019-06-29T10:45:58Z")

</div>

Hello,

I’m trying to implement [“The Multiscale Bowler-Hat Transform for Vessel Enhancement in 3D Biomedical Images”](https://arxiv.org/abs/1802.05097) by creating a custom itk filter.

They use morphological grayscale openings with a combination of spheres and 3D lines structuring elements. The lines structuring elements are oriented in several directions.

I would like to know how to make 3D oriented line structuring elements with ITK.

There is the itkFlatStructuringElement, but I don’t know if the Box function is the one I’m looking for. The documentation is a bit short and the examples on the wiki have been removed with the near ITK 5.0 release.

I also found the LineIterator to draw custom lines using two points. But the result probably won’t be usable by GrayscaleMorphologicalOpeningFilter…

Any pointers on how to proceed ?

Thank you.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [June 29, 2019, 5:37pm UTC](https://discourse.itk.org/t/line-structuring-element-in-any-orientations/2004/2 "2019-06-29T17:37:25Z")

</div>

Hello @Jonas

Welcome to the ITK community! ☀

> [@Jonas](#):
>
> I’m trying to implement [“The Multiscale Bowler-Hat Transform for Vessel Enhancement in 3D Biomedical Images”](https://arxiv.org/abs/1802.05097) by creating a custom itk filter.

Awesome!

> [@Jonas](#):
>
> There is the itkFlatStructuringElement, but I don’t know if the Box function is the one I’m looking for.

Yes, you are on the right track. Depending on what you want, [`AddLine`](https://itk.org/Doxygen/html/classitk_1_1FlatStructuringElement.html#a1606ea9fe33fb81c0d2465aac9fc04bd), [`FromImage`](https://itk.org/Doxygen/html/classitk_1_1FlatStructuringElement.html#abdcac67d139bb0afe6740812d9ae912d), or [`Polygon`](https://itk.org/Doxygen/html/classitk_1_1FlatStructuringElement.html#ad34c7152241715c9d0489a264ae29615) are likely closer than `Box` for your application. The `itk::FlatStructuringElement` source code and the [ITK Software Guide](https://itk.org/ItkSoftwareGuide.pdf) have more details or a higher level overview, respectively.

Please let us know how it goes!

---

<div class="post-metadata">

### Author: ![tim-evain](https://discourse.itk.org/user_avatar/discourse.itk.org/tim-evain/32/220_2.png) [@tim-evain](https://discourse.itk.org/u/tim-evain)
#### Post date: [July 2, 2019, 1:28pm UTC](https://discourse.itk.org/t/line-structuring-element-in-any-orientations/2004/3 "2019-07-02T13:28:16Z")

</div>

Hello @Jonas

When I have to create a custom structuring element, I usually go back to the [Neighborhood](https://itk.org/Doxygen/html/classitk_1_1Neighborhood.html) base class.  
For a small example, you can see [this post](http://itk-users.7.n7.nabble.com/How-to-generate-asymmetric-line-structuring-element-td35881.html) I made a while back on the same topic.

HTH,

Tim

---

<div class="post-metadata">

### Author: ![Jonas](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@Jonas](https://discourse.itk.org/u/Jonas)
#### Post date: [July 3, 2019, 9:32am UTC](https://discourse.itk.org/t/line-structuring-element-in-any-orientations/2004/4 "2019-07-03T09:32:57Z")

</div>

Thank you both for your input.

I want to stay close to the original matlab implementation. They use points uniformly distributed on a sphere to create orientations.

This is what I did :

- Get a point generated on that sphere and his symmetric equivalent across the center (P1 & P2)
- Create a boolean image
- Use a LineIterator to draw the line between P1 and P2 in the image.
- feed the image to an itkFlatStructuringElement using the FromImage function.

It seems to work. I still have a few things to check, but it’s going in the right direction.  
I’ll keep you informed when the full filter is finished 😉

---

<div class="post-metadata">

### Author: ![Jonas](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@Jonas](https://discourse.itk.org/u/Jonas)
#### Post date: [July 5, 2019, 4:43pm UTC](https://discourse.itk.org/t/line-structuring-element-in-any-orientations/2004/5 "2019-07-05T16:43:30Z")

</div>

@matt.mccormick  
I need your input once more because I’m doing something wrong here…

> // A bit of typedef context  
> typename TInputImage::Pointer input = TInputImage::New();  
> input-\>Graft( const\_cast\<TInputImage\*\>( this-\>GetInput() ));  
> using LineSeType = FlatStructuringElement\<3\>;  
> using OpeningLineFilterType =GrayscaleMorphologicalOpeningImageFilter  
> \<TInputImage,TInputImage,LineSeType\>;  
> auto openingLineFilter = OpeningLineFilterType::New();  
> openingLineFilter-\>SetInput( input );

> // Problematic code portion  
> LineSeType lineSe;  
> lineSeImage = line3D(s,v[n],1.0,1.0,1.0);  
> // Viewing structuring element image for debug purpose  
> auto castFilter = CastImageFilter\<itk::Image\<bool,3\>,itk::Image\<int,3\>\>::New();  
> castFilter-\>SetInput(lineSeImage);  
> itk::ViewImage\<itk::Image\<int,3\>\>::View(castFilter-\>GetOutput() );  
> **openingLineFilter-\>SetKernel( lineSe.FromImage(lineSeImage) );**  
> openingLineFilter-\>Update();  
> auto imLine = openingLineFilter-\>GetOutput();  
> // viewing Opening image for debug purpose  
> itk::ViewImage::View(imLine);

From my previous post the first 3 steps are doing great. I managed to create a 3D boolean image (odd dimensions as the documentation requires) with the structuring element. As you can see in the picture below.

 ![structuringElement](https://discourse.itk.org/uploads/default/original/1X/bb536a57324af8265b7e2692817f2adb60c36f67.png)

But the output of the opening is really bad…There are big overflow values on the image boundaries (3.5 +300) and a constant value inside (128).

 ![going_wrong](https://discourse.itk.org/uploads/default/original/1X/71bd263149304e0e154f22af46ae58ba106a6d28.png)

My original picture is this one with a grayscale gradient ( to mimic vessels disappearing in the tissue)

 ![cylindres](https://discourse.itk.org/uploads/default/original/1X/2f10c988414d81baa1540c2105ddd8f6f0e39984.png)

The opening is doing fine with a ball structuring element…My error should comme for the highlighted line.  
What did I do wrong ?

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [July 9, 2019, 3:27am UTC](https://discourse.itk.org/t/line-structuring-element-in-any-orientations/2004/6 "2019-07-09T03:27:02Z")

</div>

> [@Jonas](#):
>
> openingLineFilter-\>SetKernel( lineSe.FromImage(lineSeImage) );

Since [SetKernel takes a reference](https://itk.org/Doxygen/html/classitk_1_1GrayscaleMorphologicalOpeningImageFilter.html#a423901986d1e305e36e5a88bc1a101c9) and `FromImage` is a `static` method, try assigning to an instance, e.g.:

```auto
LineSeType kernel = lineSe.FromImage(lineSeImage);
openingLineFilter->SetKernel( kernel )

```
