# Optimization of mathematical morphology functions of OpenCV

Greetings.

I am currently working on a project with Qt (SDK 1.2.1) and OpenCV (2.2.0) under Windows 7.

I recently presented a problem that I think is complex (or at least more complex than those faced so far) and as I do not wear long time to be working with Qt + OpenCV (I'm still a novice at both libraries), I'm not sure how solve or rather, what are my possible tools to solve it. We next sought to explain:

In my project I'm working mainly with mathematical morphology functions of OpenCV (erosion, dilation, opening, ect.). The images digital I'm working with are images of histological samples of breast tissue (on these images apply morphological operations); these images in particular are quite large, about 4000 * 3000 pixels, and consequently the calculation time of the operations morphological is very long ... eg:

• Do dilatation with 20 iterations and a rectangular structuring element of 3 * 3 takes about 7 seconds.
• Make opening with 10 iterations and an elliptical structuring element of 15 * 15 is about 5 minutes.

So, my specific question is that do TO REDUCE THE TIME it takes to apply the different morphological operations. I thought about using parallelization (multithreaded programming), but I have several questions in the following areas:

1. Is it possible with OpenCV parallelization, since I think I must own routines parallelize the OpenCV (erosion, dilation, ect.)?
2. What tasks should be parallelized and how?
3. What parallelization library use?

To the last question I've read in some places (including the page OpenCV) on TBB, but I do not entirely clear what exactly is TBB?, and how it works and/or use?.

Is the TBB library as Pthreads or OpenMP parallelization?, Ie, do I have to schedule myself/encode parallelized manually as tasks (with all that that implies)?

or, conversely ... Does the parallelization is automatic and transparent? ... as I understand it.

Thanks in advance for any help and / or suggestions.

edit retag close merge delete

Sort by ยป oldest newest most voted

Maybe you can reduce the time by applying less iterations but with a larger structuring element, since the effect is the same ( see also http://answers.opencv.org/question/6240/equivalence-between-structural-elements/. Note that (if I interprete the code correctly) this optimization is already done in OpenCV, however only if the kernel has no zero-elements ( see imgproc/src/morph.cpp l.1165 - OpenCV.2.4.4).

To answer your questions: Yes, it should be possible to parallelize them, since every morphological operation is applied on a block of the image (Note again that this is (partially) already done in OpenCV if you have TBB and TEGRA_OPTIMIZATION.). Thus, maybe you can divide your image in overlapping blocks and apply the morphological operation on each block in parallel.

For the parallelization I suggest to use OpenCV's parallel_for_ ( see http://answers.opencv.org/question/3730/how-to-use-parallel_for/ for the usage) which afaik internally uses TBB. TBB is a C++ library for parallelization and yes you need to implement your code in a slight different way, but the rest works completely automatic (see http://threadingbuildingblocks.org to learn more about TBB).

more

Official site

GitHub

Wiki

Documentation