# Detecting planes in binary images

**URL:** <https://discourse.itk.org/t/detecting-planes-in-binary-images/3115>\
**Category:** Algorithms\
**Created:** [May 25, 2020, 9:47am UTC](https://discourse.itk.org/t/detecting-planes-in-binary-images/3115 "2020-05-25T09:47:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alkamid](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@alkamid](https://discourse.itk.org/u/alkamid)\
**Post date:** [May 25, 2020, 9:47am UTC](https://discourse.itk.org/t/detecting-planes-in-binary-images/3115/1 "2020-05-25T09:47:47Z")

</div>

I’m trying to detect certain type of artifacts in binary volumes I’m working with, namely step-like transitions between 0 and 1, but only in the three orthogonal planes XY/XZ/YZ. Example:

![Screenshot from 2020-05-25 09-01-14](https://discourse.itk.org/uploads/default/original/2X/4/43d5e27898016f6da60b8190b3a236def65a4bf9.png)

The surface is generally rough and doesn’t end sharply, but in this case it’s truncated in Z direction (the volume is rotated in the screenshot).

**What ITK filters can I use to do this efficiently?** I don’t need to segment those edges, I just need to know whether they exist in the volume. I would also need to somehow control the size threshold: it’s fine if a 2x2 voxel area vanishes abruplty, but a 20x20 step is something I want to know about.

A simple idea I tried is to convolve an edge-detecting kernel and threshold the output’s absolute intensity (this one is for detecting edges in Z direction only):

```python
    kernel_np = np.zeros((2, 20, 20), dtype=np.int16)
    kernel_np[0, :, :] = -1
    kernel_np[1, :, :] = 1
    kernel_sitk = sitk.GetImageFromArray(kernel_np)

    output = sitk.Convolution(input, kernel_sitk)
    output = sitk.Abs(output)
    output = sitk.IntensityWindowing(output, 200.0)

```

I think I can get it to work with some fine tuning, but it’s quite slow. The binary volumes I’m analysing are of the order of 500x500x500.

---

<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:** [May 25, 2020, 5:07pm UTC](https://discourse.itk.org/t/detecting-planes-in-binary-images/3115/2 "2020-05-25T17:07:17Z")

</div>

> [@alkamid](#):
>
> (2, 20, 20)

That is a huge kernel for convolution. Try a smaller one.

---

<div class="post-metadata">

**Author:** ![alkamid](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@alkamid](https://discourse.itk.org/u/alkamid)\
**Post date:** [May 26, 2020, 9:12am UTC](https://discourse.itk.org/t/detecting-planes-in-binary-images/3115/3 "2020-05-26T09:12:03Z")

</div>

> [@dzenanz](#):
>
> That is a huge kernel for convolution. Try a smaller one.

Good idea. I was thinking that I could filter out smaller flat areas by making the kernel big, but that’s obviously inefficient.

Can I use the fact that the volumes I’m working with are sparse (\>90% zeros) or that they’re all binary?

---

<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:** [May 26, 2020, 3:20pm UTC](https://discourse.itk.org/t/detecting-planes-in-binary-images/3115/4 "2020-05-26T15:20:04Z")

</div>

Most label volumes are sparse, and many are binary. Not easy to take advantage of that in this case. Maybe check whether any of the [morphological filters](https://itk.org/Doxygen/html/group__MathematicalMorphologyImageFilters.html) help you somehow?
