# How to deal with HoughTransform2DLinesImageFilter GetLines() method in python

**URL:** <https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861>\
**Category:** Beginner Questions\
**Created:** [May 11, 2019, 2:03pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861 "2019-05-11T14:03:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![nick](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://discourse.itk.org/u/nick)\
**Post date:** [May 11, 2019, 2:03pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/1 "2019-05-11T14:03:23Z")

</div>

Dear All,

I’m trying to use HoughTransform2DLinesImageFilter in python. However, I cannot understand how to use the output of the GetLines() method.  
AFAIU, the filter should return a list o the lines detected: how do I iterate over this list? How do access to the parameters of the lines?

Thanks

---

<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:** [May 12, 2019, 2:54pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/2 "2019-05-12T14:54:42Z")

</div>

Hello @nick,

Welcome to the ITK community! 🐍 🔗 🙂

Could you please share a reproducible version of your code / data? We will be able to help with the specifics, then.

Thanks!  
Matt

---

<div class="post-metadata">

**Author:** ![nick](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://discourse.itk.org/u/nick)\
**Post date:** [May 12, 2019, 8:57pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/3 "2019-05-12T20:57:39Z")

</div>

Dear @matt.mccormick thank you for your welcome and your reply.

What I’m trying to do, is to apply the hough transform for lines to an image and get the lines found by the algorithm.

Here is a sample of the code I’m using

houghF = itk.HoughTransform2DLinesImageFilter[itk.F, itk.F].New()  
houghF.SetInput(edges)  
houghF.SetAngleResolution(100)  
houghF.SetNumberOfLines(2)  
houghF.Update()  
DetectedLines=houghF.GetLines()  
I expected the var “DetectedLines” to be a list/array of 2 entries. However, it is ‘SwigPyObject’.  
My question is: how do I access to the lines contained in the var “DetectedLines”?

Thanks

---

<div class="post-metadata">

**Author:** ![nick](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://discourse.itk.org/u/nick)\
**Post date:** [May 20, 2019, 1:42pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/4 "2019-05-20T13:42:04Z")

</div>

Dear All,

I’m still not able to understand my previous question… anyone could give me a hint on how to continue with this filter?

thanks

---

<div class="post-metadata">

**Author:** ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)\
**Post date:** [May 20, 2019, 3:11pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/5 "2019-05-20T15:11:31Z")

</div>

Hi @nick

I just looked at your question, together with my [LKEB](http://www.lkeb.nl) colleague Patrick de Koning, who has experience using the SWIG Python wrapper of ITK. Unfortunately, we haven’t found the answer to your question. 🙁

`DetectedLines` wraps a C++ reference to an `std::list` of `LineSpatialObject` pointers. I can imagine that the wrapping would have been easier if `std::vector` was used, instead of `std::list`. For example, the Python wrapper of ITK’s VectorContainer _does_ provide full access to the underlying `std::vector`.

However, SWIG **does** support `std::list` so I don’t really know why it does not work: [https://github.com/swig/swig/blob/rel-4.0.0/Lib/python/std\_list.i](https://github.com/swig/swig/blob/rel-4.0.0/Lib/python/std_list.i)

Bradley @blowekamp do you have a clue?

---

<div class="post-metadata">

**Author:** ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)\
**Post date:** [May 20, 2019, 8:43pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/6 "2019-05-20T20:43:24Z")

</div>

I created an issue on the issue tracker and will submit a tentative patch soon: [https://github.com/InsightSoftwareConsortium/ITK/issues/918](https://github.com/InsightSoftwareConsortium/ITK/issues/918)

---

<div class="post-metadata">

**Author:** ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)\
**Post date:** [May 20, 2019, 9:00pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/7 "2019-05-20T21:00:44Z")

</div>

I created a PR [here](https://github.com/InsightSoftwareConsortium/ITK/pull/921). I need to update the test with an image that would give a reasonable result for this filter. @nick: Do you have any simple line image that you could share that we could integrate as a test in ITK?

---

<div class="post-metadata">

**Author:** ![nick](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://discourse.itk.org/u/nick)\
**Post date:** [May 21, 2019, 8:12am UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/8 "2019-05-21T08:12:55Z")

</div>

Thank you guys for your effort!  
@fbudin I have no realistic line image that I can share. I can generate binary lines like this ![line](https://discourse.itk.org/uploads/default/original/1X/c6b7891fec58e90383a87d4ee148c62add9f4a45.png). Do you think that this will be useful as a basic test?

Thanks

---

<div class="post-metadata">

**Author:** ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)\
**Post date:** [May 21, 2019, 1:00pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/9 "2019-05-21T13:00:12Z")

</div>

That’s perfect for the simple example that I wanted to implement. It is just to check that it is possible to use this filter in Python.

---

<div class="post-metadata">

**Author:** ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)\
**Post date:** [May 21, 2019, 2:17pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/10 "2019-05-21T14:17:56Z")

</div>

FYI, I updated the PR using the image you provided to add a very simple test that verifies that we can access the output of the filter in Python: [https://github.com/InsightSoftwareConsortium/ITK/pull/920](https://github.com/InsightSoftwareConsortium/ITK/pull/920)

---

<div class="post-metadata">

**Author:** ![nick](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://discourse.itk.org/u/nick)\
**Post date:** [May 23, 2019, 1:18pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/11 "2019-05-23T13:18:32Z")

</div>

@fbudin Thank you for your work.

This morning I downloaded the latest build, typing  
python -m pip install itk --upgrade --no-index -f [https://github.com/InsightSoftwareConsortium/ITKPythonPackage/releases/tag/latest](https://github.com/InsightSoftwareConsortium/ITKPythonPackage/releases/tag/latest)

I tried to run the code I sent above.  
However, when I try to access to DetectedLines[0] I got a segfault…

Is there something I’m missing?

Thanks

P.S.

I’m using python 3.6 on a 64 Ubuntu 16.04

---

<div class="post-metadata">

**Author:** ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)\
**Post date:** [May 23, 2019, 2:24pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/12 "2019-05-23T14:24:28Z")

</div>

> [@nick](#):
>
> I tried to run the code I sent above.  
> However, when I try to access to DetectedLines[0] I got a segfault…

Just curious… were you able to query the number of detected lines? Something like `DetectedLines.size()`?

---

<div class="post-metadata">

**Author:** ![nick](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://discourse.itk.org/u/nick)\
**Post date:** [May 23, 2019, 2:37pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/13 "2019-05-23T14:37:26Z")

</div>

@Niels_Dekker: I still get a segfault when I try to get the number of lines (both with lines.size() and len(lines) ).

---

<div class="post-metadata">

**Author:** ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)\
**Post date:** [May 23, 2019, 2:43pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/14 "2019-05-23T14:43:01Z")

</div>

> [@nick](#):
>
> I still get a segfault when I try to get the number of lines (both with lines.size() and len(lines) ).

That’s a pity!

My personal interest is more towards the “circles” version, `HoughTransform2DCirclesImageFilter`. Could you possible check if that one has the same problem, with `GetCircles`?

---

<div class="post-metadata">

**Author:** ![nick](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://discourse.itk.org/u/nick)\
**Post date:** [May 23, 2019, 3:03pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/15 "2019-05-23T15:03:02Z")

</div>

@Niels_Dekker. I did a simple test with the HoughTransform2DCirclesImageFilter and I still get segfault.  
Could you try to reproduce this?

---

<div class="post-metadata">

**Author:** ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)\
**Post date:** [May 23, 2019, 8:13pm UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/16 "2019-05-23T20:13:02Z")

</div>

Interesting. I just installed the latest package using the command you posted to make sure I was reproducing your steps. I tried both with python2.7 and python3.6. It worked for both. I ran the test that I added in ITK [here](https://github.com/fbudin69500/ITK/blob/2e1f684ce98c3e28dbc65360c5bc0d945b40661a/Modules/Filtering/ImageFeature/wrapping/test/HoughTransform2DLinesImageFilterTest.py) using the line image that is available in this thread.

---

<div class="post-metadata">

**Author:** ![nick](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://discourse.itk.org/u/nick)\
**Post date:** [May 24, 2019, 8:52am UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/17 "2019-05-24T08:52:39Z")

</div>

Dear @fbudin @Niels_Dekker

I investigated the problem I little more: as you pointed out the example works, the segfault comes when running the code inside a function.  
I attached a little snippet that reproduces this issue.[test.py](https://discourse.itk.org/uploads/default/original/1X/39a140e09d93b885454dbbc758c79254f5826340.py) (536 Bytes)

Could you reproduce it? Is there something wrong in what I’m doing?

Thanks

---

<div class="post-metadata">

**Author:** ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)\
**Post date:** [May 24, 2019, 9:44am UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/18 "2019-05-24T09:44:42Z")

</div>

> [@nick](#):
>
> I investigated the problem I little more: as you pointed out the example works, the segfault comes when running the code inside a function.

Ah, I see:

```
def RunHough(itkimage):
    hough = itk.HoughTransform2DLinesImageFilter[itk.F, itk.F].New()
    hough.SetInput(itkimage)
    ...
    hough.Update()
    return hough.GetLines()

# generate an empty image with one spot
...
lines2=RunHough(itkimage)
print(len(lines2)) // <=== segfault here

```

Looks like the problem here is that the list of lines is _owned_ by the `hough` object. The `hough` object could be destroyed when the `RunHough` function has returned, and in that case, the list has gone, too.

The most obvious fix to me would be to change the return type in C++:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/v5.0.0/Modules/Filtering/ImageFeature/include/itkHoughTransform2DLinesImageFilter.h#L135>

I think your problem would be fixed by returning the list of lines _by value_, instead of _by reference_ (`&`).

By the way, I would prefer to return an `std::vector`, instead of an `std::list`.

---

<div class="post-metadata">

**Author:** ![nick](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://discourse.itk.org/u/nick)\
**Post date:** [May 24, 2019, 11:34am UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/19 "2019-05-24T11:34:11Z")

</div>

> [@Niels\_Dekker](#):
>
> LinesListType

Dear @Niels_Dekker,

I think that you are right: that doesn’t happen for the images (e.g. the accumulator) as they are handled with Smart Pointer.  
I’m not sure if modify the method to return a copy of the object is a good idea: I think it could break some code developed by other users… is there a way in python to return a copy of this kind of objects?

Thanks

---

<div class="post-metadata">

**Author:** ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)\
**Post date:** [May 24, 2019, 11:45am UTC](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861/20 "2019-05-24T11:45:38Z")

</div>

> [@nick](#):
>
> I’m not sure if modify the method to return a copy of the object is a good idea: I think it could break some code developed by other users… is there a way in python to return a copy of this kind of objects?

At the client side: sure, you could create a new Python list inside your `RunHough` function, and copy each line object (which is if fact a smart pointer) into your list, which you will then return. That should work just fine.

At the Python wrapper side: it might be possible to do return “by value” for `GetLines()`, even when the C++ code says “by reference”. But personally I wouldn’t really like to introduce such an inconsistency between the C++ API and the Python wrapping.

At the C++ side: _theoretically_ it might break some C++ user code when the list of lines is returned by value, instead of by reference, but I don’t expect that it will affect many users. What particular use case do you think about?

[Next page](https://discourse.itk.org/t/how-to-deal-with-houghtransform2dlinesimagefilter-getlines-method-in-python/1861.md?page=2)
