patmjen / pygorpho

Python library for fast 3D mathematical morphology using CUDA
https://pygorpho.readthedocs.io/
MIT License
10 stars 2 forks source link

TopHat functionality #1

Open chrisroat opened 4 years ago

chrisroat commented 4 years ago

I recently came across this library when looking for a fast GPU implementation of a white top-hat transform (result = data - dilate(erode(data))) with python bindings. The only thing I found was this repo. So, first, a big thank you for writing this and having it available in open source. It has sped up some work I'm doing on a large dataset immensely.

Do you think the top-hat transform itself would benefit from being an op in gorpho? In your expert opinion, would a new op be substantially faster than calling the dilate & erode ops sequentially, followed by the subtraction in numpy? It would save some overhead in data transfer, though I'm not sure that's the dominant issue. I'm working with blocks that are roughly 100 x 1k x 1k float32 or int16.

patmjen commented 4 years ago

After a bit of profiling, I think it is definitely possible. Especially for the flat.linear_* functions. I'll make an implementation and get back to you on this issue. If it gives a performance improvement, I'll of course add it to gorpho and pygorpho.

Thank you for checking out the library!

chrisroat commented 4 years ago

Thanks for taking this on. I haven't used the linear code yet. My structuring element is a flat ellipsoid -- would the linear code work for that use case? (I've never used non-flat structuring elements.)

And regarding a tophat op, it's not clear to me that it needs do the subtraction. Some applications may desire to smooth or do other processing on the erode(dilate(...)) result prior to subtracting it.

Also, not to make more work, but if you do erode(dilate(...)) (white tophat), it may not be much more work to do dilate(erode(...)) (black tophat)

patmjen commented 4 years ago

First, sorry for taking a while to get back.

TopHat

I have now implemented opening, closing, white tophat and black tophat for flat structuring elements. Unfortunately, I did not observe a significant speedup on my hardware. However, there is a definite benefit in memory usage, since we only have to store intermediate results for each block.

The code is available now, but I'm still dealing with some bugs on linux (somehow, the Windows version seems fine...). I expect to push the updates to PyPI later this week in version 0.7.0. When that happens, I'll close this issue. EDIT: See below.

Linear code for flat ellipsoid

Short answer: no.

Long answer: currently, pygorpho only supports approximating a ball via strel.flat_ball_approx, using the method from the cited paper (disclosure: I'm an author of that paper).

I have since generalized that method to give better approximations, and it can also be used to give an approximation of an ellipsoid. However, such an approximation won't be optimal, and will be worse the more elongated the ellipsoid is. Depending on your use case it might still be good enough, and could offer a very significant speed improvement. It is not something I plan on adding in the short term though, but if there is a use for it, I'd love to know.

patmjen commented 4 years ago

Update: I have pushed version 0.7.0 to PyPI with above mentioned changes. However, I will leave this issue open until opening, etc., is implemented for the gen.* and flat.linear_* functions.