Note that there are some explanatory texts on larger screens.

plurals
  1. POConnected-component labeling algorithm optimization
    primarykey
    data
    text
    <p>I need some help with optimisation of my CCL algorithm implementation. I use it to detect black areas on the image. On a 2000x2000 it takes 11 seconds, which is pretty much. I need to reduce the running time to the lowest value possible to achieve. Also, I would be glad to know if there is any other algorithm out there which allows you to do the same thing, but faster than this one. So here is my code:</p> <pre><code> //The method returns a dictionary, where the key is the label //and the list contains all the pixels with that label public Dictionary&lt;short, LinkedList&lt;Point&gt;&gt; ProcessCCL() { Color backgroundColor = this.image.Palette.Entries[1]; //Matrix to store pixels' labels short[,] labels = new short[this.image.Width, this.image.Height]; //I particulary don't like how I store the label equality table //But I don't know how else can I store it //I use LinkedList to add and remove items faster Dictionary&lt;short, LinkedList&lt;short&gt;&gt; equalityTable = new Dictionary&lt;short, LinkedList&lt;short&gt;&gt;(); //Current label short currentKey = 1; for (int x = 1; x &lt; this.bitmap.Width; x++) { for (int y = 1; y &lt; this.bitmap.Height; y++) { if (!GetPixelColor(x, y).Equals(backgroundColor)) { //Minumum label of the neighbours' labels short label = Math.Min(labels[x - 1, y], labels[x, y - 1]); //If there are no neighbours if (label == 0) { //Create a new unique label labels[x, y] = currentKey; equalityTable.Add(currentKey, new LinkedList&lt;short&gt;()); equalityTable[currentKey].AddFirst(currentKey); currentKey++; } else { labels[x, y] = label; short west = labels[x - 1, y], north = labels[x, y - 1]; //A little trick: //Because of those "ifs" the lowest label value //will always be the first in the list //but I'm afraid that because of them //the running time also increases if (!equalityTable[label].Contains(west)) if (west &lt; equalityTable[label].First.Value) equalityTable[label].AddFirst(west); if (!equalityTable[label].Contains(north)) if (north &lt; equalityTable[label].First.Value) equalityTable[label].AddFirst(north); } } } } //This dictionary will be returned as the result //I'm not proud of using dictionary here too, I guess there //is a better way to store the result Dictionary&lt;short, LinkedList&lt;Point&gt;&gt; result = new Dictionary&lt;short, LinkedList&lt;Point&gt;&gt;(); //I define the variable outside the loops in order //to reuse the memory address short cellValue; for (int x = 0; x &lt; this.bitmap.Width; x++) { for (int y = 0; y &lt; this.bitmap.Height; y++) { cellValue = labels[x, y]; //If the pixel is not a background if (cellValue != 0) { //Take the minimum value from the label equality table short value = equalityTable[cellValue].First.Value; //I'd like to get rid of these lines if (!result.ContainsKey(value)) result.Add(value, new LinkedList&lt;Point&gt;()); result[value].AddLast(new Point(x, y)); } } } return result; } </code></pre> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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