title: How to use optimal parallelization for multi template matching with OpenCvSharp?
body: <p>For a multi Template Matching with OpenCvSharp I want to use <code>Parallel.ForEach()</code> and limit the maximal number of threads used to <strong>n-1</strong> so at least one thread remains free for other possible occuring tasks (n is the total nr of CPU-level threads available).</p>
  <p>E.g.: My system has a 4 Core CPU with 2 Threads per Core. So the max amount here should be 7.</p>
  <p>How can I do this without hardcoding it? (It should work also for other PCs with another number of threads)</p>
  <pre><code>   // Use a maximum of n-1 threads
     int maxNrOfThreads = 7;
  
     Parallel.ForEach(this.Symbols.Keys, new ParallelOptions { MaxDegreeOfParallelism = maxNrOfThreads }, key  =&gt; {
        //EXECUTION (template match every symbol to a given image)
     });
  </code></pre>
  <p>Here is the execution part for who may be interessted:</p>
  <pre><code>Mat grayTemplate = this.Symbols[key].GrayscaledSymbol;
  
  Mat res = new Mat(grayImage.Rows - grayTemplate.Rows + 1,grayImage.Cols - grayTemplate.Cols + 1, MatType.CV_32FC1);
  
  Cv2.MatchTemplate(grayImage, grayTemplate, res, TemplateMatchModes.CCoeffNormed);
  
  Cv2.Threshold(res, res, MIN_ACCURACY, 1.0, ThresholdTypes.Tozero);
  
  while (true)
  {
     double minval, maxval, threshold = MIN_ACCURACY;
     OpenCvSharp.Point minloc, maxloc;
     Cv2.MinMaxLoc(res, out minval, out maxval, out minloc, out maxloc);
  
     if (maxval &gt;= threshold)
     {
         // Add bounding box to result object
         ret.AddResult(key,maxloc.X,maxloc.Y,grayTemplate.Width,grayTemplate.Height);
  
        // Fill in the res Mat so you don't find the same area again in the MinMaxLoc
        Rect outRect;
        Cv2.FloodFill(res, maxloc, new Scalar(0), out outRect, new Scalar(0.1), new Scalar(1.0));
     }
     else
     {
        break;
     }
  }
  </code></pre>