Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What version of Emgu CV are you using? I couldn't find a 2.4.3 version of it.</p> <p><strong>Pretty sure your code is not the problem</strong>.</p> <p>Seems possible that the Emgu.CV.Image constructor might have a concurrency issue (either in the managed wrapper or the unmanaged code). The way the managed data array is handled in the Emgu CV trunk seems solid, there is some unmanaged data allocated during the image constructor which I suppose might have gone wrong.</p> <p>What happens if you try:</p> <ul> <li>Moving <code>Images[i] = GetRandomImage();</code> outside of the parallel For().</li> <li>Slapping a <code>lock()</code> around the <code>Image</code> constructor in <code>GetRandomImage()</code></li> </ul> <p>I noticed there's a closed bug report of someone having a similar issues (calls to image constructor occuring in parallel but images themselves not shared among threads) <a href="http://sourceforge.net/p/emgucv/support-requests/7/" rel="nofollow">here</a>.</p> <p>[Edit]</p> <p>Yes this is a strange one. I can reproduce with the stock 2.4.2 version and OpenCV binaries. </p> <p>It only seems to crash for me if the number of threads in the parallel for exceeds the number of cores which is >2 for me.. would be interesting to know how many cores are on your test system.</p> <p>Also I only get the crash when the code is not attached to the debugger and Optimize Code is enabled - have you ever observed it in release mode with the debugger attached? </p> <p>As the SmoothBilateral function is CPU bound, using MaxDegreeOfParallelism more than the number of cores doesn't really add any benefit so there's a perfect workaround assuming what I found about the number if threads vs cores is also true for your rig (sods law predicts: it isn't).</p> <p>So my guess is there is a concurrency/volatile issue in Emgu that only manifests when JIT optimisation is run, and when the GC is moving managed data around. But, as you say, there are no obvious unpinned-pointer-to-managed-object issues in the Emgu code.</p> <p>Although I still can't explain it properly, here's what I found so far:</p> <p>With the GC.Collect + console logs removed, the calls to GetRandomImage() serialised, and the code run outside of MSVC I couldn't reproduce the issue (although this may have just reduced the frequency):</p> <pre><code> public static int Width = 750; public static int Height = 750; ... int N = 500; int Threads = 11; Images = new Emgu.CV.Image&lt;Rgb, float&gt;[N]; Console.WriteLine("Start"); ParallelOptions po = new ParallelOptions(); po.MaxDegreeOfParallelism = Threads; for (int i = 0; i &lt; N; i++) { Images[i] = GetRandomImage(); } System.Threading.Tasks.Parallel.For(0, N, po, new Action&lt;int&gt;((i) =&gt; { //Console.WriteLine("CallingSmooth"); Images[i].SmoothBilatral(15, 50, 50); //Console.WriteLine("SmoothCompleted"); })); Console.WriteLine("End"); </code></pre> <p>I added a timer to fire GC.Collect outside of the parallel for, but still more often than it would fire normally:</p> <pre><code> var t = new System.Threading.Timer((dummy) =&gt; { GC.Collect(); }, null, 100,100); </code></pre> <p>And with this change I still can't reproduce the issue, although GC collect is being called less consistently than in your demo as the thread pool is busy, also there are no (or very few) managed allocations occuring in the main loop for it to collect. Uncommenting the console logs around the <strong>SmoothBilatral</strong> call then repros the error fairly swiftly (by giving GC something to collect I guess).</p> <p>[Another edit]</p> <p>The <a href="http://public.cranfield.ac.uk/c5354/teaching/dip/opencv/manual/opencv242refman.pdf" rel="nofollow">OpenCV 2.4.2 reference manual</a> states that cvSmooth is deprecated AND that "Median and bilateral filters work with 1- or 3-channel <em>8-bit images</em> and can not process images in-place."... not very encouraging!</p> <p>I find that using median filter on byte or float images and bilateral on byte images works fine, and I can't see why any CLR/GC issues woudn't affect those cases too.</p> <p>So despite the strange effects on the C# test program I still reckon this is an Emgu/OpenCV bug.</p> <p>If you haven't already, you should test with opencv binaries that you've <a href="http://docs.opencv.org/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.html#windows-visual-studio-how-to" rel="nofollow">compiled yourself</a>, if it still fails convert your test to C++.</p> <p>N.b. that OpenCV has its <a href="http://answers.opencv.org/question/3730/how-to-use-parallel_for/" rel="nofollow">own parallelism implementation</a> which would probably work out faster.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload