コード例 #1
0
        public static Cluster DetectCluster(DataImage resultImage, int xOrigin, int yOrigin, byte minColor, byte maxColor)
        {
            result = resultImage;
            data = result.data;
            pixelsChecked = new bool[data.Length];
            colorMin = minColor;
            colorMax = maxColor;
            height = result.Height;
            width = result.Width;

            ranges = new RangeQueue();
            currentCluster = new Cluster(resultImage);

            //////
            if (!(result[xOrigin, yOrigin] >= minColor && result[xOrigin, yOrigin] <= maxColor))
                throw new InvalidOperationException("Point is not within color range");
            /////\
            LinearDetect(ref xOrigin, ref yOrigin);

            while (ranges.size > 0)
            {
                DetectionRange range = ranges.Dequeue();

                int upY = range.y - 1;
                int downY = range.y + 1;

                int downIndex = resultImage.GetDataIndex(range.x1, downY);
                int upIndex = resultImage.GetDataIndex(range.x1, upY);

                int currentIndex;

                for (int x = range.x1; x <= range.x2; x++)
                {
                    currentIndex = result.GetDataIndex(x, upY);
                    if (range.y > 0 && (!pixelsChecked[upIndex]) && PixelIsWithinColorRange(ref currentIndex))
                        LinearDetect(ref x, ref upY);

                    currentIndex = result.GetDataIndex(x, downY);
                    if (range.y < (height - 1) && (!pixelsChecked[downIndex]) && PixelIsWithinColorRange(ref currentIndex))
                        LinearDetect(ref x, ref downY);
                    downIndex += height;
                    upIndex += height;
                }
            }
            Array.Resize<int>(ref currentCluster.indexes, currentCluster.Length);
            Array.Sort(currentCluster.Indexes);
            return currentCluster;
        }