Ejemplo n.º 1
0
    /// <summary>
    /// Computes the image projection and clusters that projection
    /// </summary>
    /// <param name="region">The image to perform this operation on</param>
    /// <param name="horizontal">If true the clustering is performed on a horizontal projection; on a vertical one otherwise</param>
    /// <returns>The number of clusters found and the index of the largest cluster</returns>
    private int[] ComputeClusters(IplImage region, bool horizontal, out double[] projectionOut)
    {
        int dim = horizontal ? region.Width : region.Height;

        double[] projection = new double[dim];
        for (int i = 0; i < projection.Length; i++)
        {
            projection[i] = ((horizontal ? region.GetCol(i) : region.GetRow(i)).Sum().Val0 / 255) / dim;
        }

        int inCluster = 0, clusters = 0;
        int lastCluster = 0, lastClusterCount = 0;

        for (int i = 0; i < projection.Length - 1; i++)
        {
            if (projection[i] > 0.03 && inCluster == 0)
            {
                inCluster++;
            }
            else if (projection[i] < 0.01 && inCluster > 0)
            {
                clusters++;
                if (lastClusterCount < inCluster)
                {
                    lastCluster      = i;
                    lastClusterCount = inCluster;
                }
                inCluster = 0;
            }
        }

        projectionOut = projection;
        return(new int[] { clusters, lastCluster });
    }