Accord.Imaging.RecursiveBlobCounter.BuildObjectsMap C# (CSharp) Method

BuildObjectsMap() protected method

Actual objects map building.
The method supports 8 bpp indexed grayscale images and 24/32 bpp color images.
Unsupported pixel format of the source image.
protected BuildObjectsMap ( UnmanagedImage image ) : void
image UnmanagedImage Unmanaged image to process.
return void
        protected override void BuildObjectsMap(UnmanagedImage image)
        {
            this.stride = image.Stride;

            // check pixel format
            if ((image.PixelFormat != PixelFormat.Format8bppIndexed) &&
                 (image.PixelFormat != PixelFormat.Format24bppRgb) &&
                 (image.PixelFormat != PixelFormat.Format32bppRgb) &&
                 (image.PixelFormat != PixelFormat.Format32bppArgb) &&
                 (image.PixelFormat != PixelFormat.Format32bppPArgb))
            {
                throw new UnsupportedImageFormatException("Unsupported pixel format of the source image.");
            }

            // allocate temporary labels array
            tempLabels = new int[(ImageWidth + 2) * (ImageHeight + 2)];
            // fill boundaries with reserved value
            for (int x = 0, mx = ImageWidth + 2; x < mx; x++)
            {
                tempLabels[x] = -1;
                tempLabels[x + (ImageHeight + 1) * (ImageWidth + 2)] = -1;
            }
            for (int y = 0, my = ImageHeight + 2; y < my; y++)
            {
                tempLabels[y * (ImageWidth + 2)] = -1;
                tempLabels[y * (ImageWidth + 2) + ImageWidth + 1] = -1;
            }

            // initial objects count
            ObjectsCount = 0;

            // do the job
            unsafe
            {
                byte* src = (byte*)image.ImageData.ToPointer();
                int p = ImageWidth + 2 + 1;

                if (image.PixelFormat == PixelFormat.Format8bppIndexed)
                {
                    int offset = stride - ImageWidth;

                    // for each line
                    for (int y = 0; y < ImageHeight; y++)
                    {
                        // for each pixel
                        for (int x = 0; x < ImageWidth; x++, src++, p++)
                        {
                            // check for non-labeled pixel
                            if ((*src > backgroundThresholdG) && (tempLabels[p] == 0))
                            {
                                ObjectsCount++;
                                LabelPixel(src, p);
                            }
                        }
                        src += offset;
                        p += 2;
                    }
                }
                else
                {
                    pixelSize = Bitmap.GetPixelFormatSize(image.PixelFormat) / 8;
                    int offset = stride - ImageWidth * pixelSize;

                    // for each line
                    for (int y = 0; y < ImageHeight; y++)
                    {
                        // for each pixel
                        for (int x = 0; x < ImageWidth; x++, src += pixelSize, p++)
                        {
                            // check for non-labeled pixel
                            if ((
                                    (src[RGB.R] > backgroundThresholdR) ||
                                    (src[RGB.G] > backgroundThresholdG) ||
                                    (src[RGB.B] > backgroundThresholdB)
                                  ) &&
                                (tempLabels[p] == 0))
                            {
                                ObjectsCount++;
                                LabelColorPixel(src, p);
                            }
                        }
                        src += offset;
                        p += 2;
                    }
                }
            }

            // allocate labels array
            ObjectLabels = new int[ImageWidth * ImageHeight];

            for (int y = 0; y < ImageHeight; y++)
                Array.Copy(tempLabels, (y + 1) * (ImageWidth + 2) + 1, ObjectLabels, y * ImageWidth, ImageWidth);
        }