DEV Community

Bosley
Bosley

Posted on

Active Contours with OpenCV

During my last year of my undergraduate studies in Computer Science I did some work with automated drone control using something called Active Contours. The contour algorithm used was the research of my advisor and it was primarily my task to simply take information from said contour and translate it into drone controls. I didn't have to implement the algorithm, but it got me interested in Active Contours (snakes).
A while back while waiting to hear back on a job opportunity I decided to play with C++ and OpenCV in an attempt to get under the hood of these snake algorithms. I knew that the variant we used for the project would be a little bit beyond me until I could nail down the fundamental aspects. During that time I implemented a C++/QT snake algorithm, and it went pretty well.

The algorithm here is kind of lame. Its a simple energy minimization algorithm sourced from this paper.

Alt Text

    Energy = ∫(α(s)Econt + β(s)Ecurv + γ(s)Eimage)ds
Enter fullscreen mode Exit fullscreen mode

Long story short, you take iterate over set of points and select a "neighborhood" of points around each looking for a spot that reduces the overall energy of the contour model.

Having an itch to play with snakes again (a couple years later) I decided I might want to revisit the fundamentals and maybe see if I could make the snake a bit better. I couldn't decided if I wanted to use Rust (which I'm currently in love with) or Python (which would be slower, but a lot easier to get started).

In order to figure out which I would want to do I obviously had to implement both and see which would be my target.

Python

As you can imagine, Python was pretty simple. I used pip to install opencv and away I went. I got the implementation completed in about 45 minutes. It went well, but something about using Python for this task just rubs me wrong.

Rust

Getting started in Rust was a little bit harder. I had to use opencv-rust and fight my Mac a little bit to get things building. Once I got it building I then had to sift through the documentation to see how everything works with the generated rust code for OpenCV. It took a couple of hours but I got it working too.

Next Steps

Now that I've implemented the simple algorithm in C++/Python/Rust I think I can say for certain that I understand well how it works. I'll definitely be continuing with Rust.. I just love it too much. I'm hoping that in the coming months I'll be on my way to making some improvements and maybe even tackle Anisotropic Fractal Snakes.

Top comments (0)