Beispiel #1
0
        /// <summary> Looks for the connected components and extracts data
        /// </summary>
        private void FindConnectedComponents()
        {
            graph = new ObjectGraph();

            switch (connectivity)
            {
                case eConnectivity.TWOD_4: detect4(); break;
                case eConnectivity.THREED_6: detect6(); break;
                case eConnectivity.TWOD_8: detect8(); break;
                case eConnectivity.TWOD_24: detect24(); break;
                case eConnectivity.THREED_26: detect26(); break;
                default: throw new Exception("Connectivity must be 4, 6, 8, 24 or 26");
            }

            /* determine the final labels */
            int numCom = 0;
            int index = -1;

            Dictionary<int, ConnectedVoxels> hm = new Dictionary<int, ConnectedVoxels>();
            int key;
            for (int k = 0; k < depth; k++)
                for (int j = 0; j < height; j++)
                    for (int i = 0; i < width; i++)
                        if (labeledOutput[++index] != 0f)
                        {
                            key = graph.getAncestorLabel((int)labeledOutput[index]);

                            if (!hm.ContainsKey(key)) hm.Add(key, new ConnectedVoxels(++numCom,
                                                                                    labeledOutputImage,
                                                                                    image,
                                                                                    connectivity));

                            labeledOutput[index] = hm[key].Label;
                            hm[key].Add(index, i, j, k);
                        }

            //display(labeledOutput);
            if (numCom == 0) return;

            /* use the volume constraint */
            ConnectedVoxels[] foundObjectsBefore = new ConnectedVoxels[numCom];
            ArrayList foundObjectsAfter = new ArrayList();
            foreach (ConnectedVoxels fo in hm.Values)
                foundObjectsBefore[fo.Label - 1] = fo;
            int[] relb = new int[numCom]; // relabel array
            for (int i = 0; i < numCom; i++)
                relb[i] = i + 1;
            int reNumCom = numCom;
            for (int i = 0; i < numCom; i++)
                if (foundObjectsBefore[i].Volume < min || foundObjectsBefore[i].Volume > max)// || coarr[i].IsOnEdge)
                {
                    relb[i] = 0;
                    for (int j = i + 1; j < numCom; j++)
                    {
                        relb[j]--;
                        foundObjectsBefore[j].Label--;
                    }
                    reNumCom--;
                }
                else
                {
                    foundObjectsAfter.Add(foundObjectsBefore[i]);
                }

            // relabel with the constraint
            for (int i = 0; i < labeledOutput.Length; i++)
            {
                index = (int)labeledOutput[i];
                if (index > 0 && index != relb[index - 1])
                    labeledOutput[i] = relb[index - 1];
            }

            if (reNumCom == 0) return;

            this.cc = (ConnectedVoxels[])foundObjectsAfter.ToArray(typeof(ConnectedVoxels));
        }
Beispiel #2
0
 public void Add(ConnectedVoxels cc)
 {
 }