Erosion/dilation operators

4 replies [Last post]
theo
User offline. Last seen 8 years 51 weeks ago. Offline
Joined: 05/30/2012
Posts:

Is there any support for erosion or dilation operators in the framework or support for them in the near future? I don't see anything in there but wanted to make sure before I go hacking away at shaders (I'm pretty unfamiliar with shaders).

theo
User offline. Last seen 8 years 51 weeks ago. Offline
Joined: 05/30/2012
Posts:

Ended up hacking one up, wasn't too bad.

Brad Larson
Brad Larson's picture
User offline. Last seen 4 years 22 weeks ago. Offline
Joined: 05/14/2008
Posts:

I was just working on this myself. If you'd like, I could try to incorporate your code.

jeff
User offline. Last seen 8 years 51 weeks ago. Offline
Joined: 06/04/2012
Posts:

Wrote the last post under a pseudonym, but this is really the same guy.

It's not a great implementation, but it works. It uses a square structuring element, and you can specify the size of the square and the step size of the elements in the square. For example, a dilation size of 7 and step size 2 would look like this:

1 0 1 0 1 0 1
0 0 0 0 0 0 0
1 0 1 0 1 0 1
0 0 0 [0] 0 0 0
1 0 1 0 1 0 1
0 0 0 0 0 0 0
1 0 1 0 0 0 1

Where the bracketed 0 is the center pixel. If you want to use it, I can email it to you or send you a link.

By the way, thanks for this awesome framework!

Brad Larson
Brad Larson's picture
User offline. Last seen 4 years 22 weeks ago. Offline
Joined: 05/14/2008
Posts:

I ended up implementing dilation and erosion for both single-channel images and RGB ones in the latest commits I posted yesterday. I went with a two-pass separable kernel to reduce the number of texture reads required for sampling over a rectangular area, and provided shaders for dilation sizes of 3, 5, 7, and 9. I didn't add a step size to this, but I can see an easy way to do so.

I also created opening and closing filters using pairs of the erosion and dilation filters.

See the GPUImageErosionFilter, GPUImageDilationFilter, GPUImageOpeningFilter, and GPUImageClosingFilter for my implementations. The FilterShowcase also has examples of the RGB variants of these running against live video. The 9-pixel sampling size opening and closing filters run at 10 FPS on an iPhone 4, which isn't bad.

The only downside to the separable kernel is that it doesn't allow for arbitrary shapes in the dilation area (circles, diamonds, etc.), so you always end up with rectangular features. I can see creating a second set of operations that do a one-pass filtering over an arbitrary area, but those will probably be slower.

Syndicate content