Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As emgu website state there are primarly two strategies:</p> <p><strong>The safe (slow) way</strong></p> <p>Suppose you are working on an <code>Image&lt;Bgr, Byte&gt;</code>. You can obtain the pixel on the y-th row and x-th column by calling</p> <pre><code>Bgr color = img[y, x]; </code></pre> <p>Setting the pixel on the y-th row and x-th column is also simple</p> <pre><code>img[y,x] = color; </code></pre> <p><strong>The fast way</strong></p> <p>The Image pixels values are stored in the Data property, a 3D array. Ok so this is true but does not tell how to do it in a real scenario. So let's see some working code and then discuss performance and optimization:</p> <pre><code>Image&lt;Bgr, Byte&gt; original = newImage&lt;Bgr, byte&gt;(1024, 768); Stopwatch evaluator = newStopwatch(); int repetitions = 20; Bgr color = newBgr(100, 40, 243); evaluator.Start(); for (int run = 0; run &lt; repetitions; run++) { for (int j = 0; j &lt; original.Cols; j++) { for (int i = 0; i &lt; original.Rows; i++) { original[i, j] = color; } } } evaluator.Stop(); Console.WriteLine("Average execution time for {0} iteration \n using column per row access: {1}ms\n", repetitions, evaluator.ElapsedMilliseconds / repetitions); </code></pre> <p>So this is the average running time that you have after 20 runs using the safe slow way of setting image pixel On my machine it takes <strong>1021ms</strong>...</p> <p>So 1021 milliseconds as average time for looping and setting a number of pixels equal to 1024*768. We could have done a little bit better by looping on row by row </p> <p>So let's refactor a little bit our code and let's use the faster way using directly Image.Data property:</p> <pre><code>evaluator.Reset(); evaluator.Start(); for (int run = 0; run &lt; repetitions; run++) { for (int i = 0; i &lt; original.Rows; i++) { for (int j = 0; j &lt; original.Cols; j++) { original.Data[i, j, 0] = 100; original.Data[i, j, 1] = 40; original.Data[i, j, 2] = 243; } } } evaluator.Stop(); Console.WriteLine("Average execution time for {0} iterations \n using Data property: {1}ms\n", repetitions, evaluator.ElapsedMilliseconds / repetitions); </code></pre> <p>On my machine it takes <strong>519ms</strong>. So we have gained a performance boost of 50%. The execution time has been decreased by a factor of two.</p> <p>So looking carefully at the code keeping in mind that we are using C#, we can do a minor change that will drastically boost again our image pixel setting performance... <strong>we should not use c# property inside a loop!!!</strong></p> <pre><code>evaluator.Reset(); evaluator.Start(); byte[,,] data = original.Data; for (int run = repetitions - 1; run &gt;= 0; run--) { for (int i = original.Rows - 1; i &gt;= 0; i--) { for (int j = original.Cols - 1; j &gt;= 0; j--) { data[i, j, 0] = 100; data[i, j, 1] = 40; data[i, j, 2] = 243; } } } evaluator.Stop(); </code></pre> <p>With this latest piece of code you will have a huge performance boost <strong>(73ms)</strong> due to correct use of C# language. </p>
    singulars
    1. This table or related slice is empty.
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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