예제 #1
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="image">Color image in label image format</param>
        /// <param name="dictionary">Color dictionary lookup for interpreting the colors in the source image into labels</param>
        public unsafe LabelImage(RGBImage image, LabelDictionary dictionary)
        {
            _handler = new ShortArrayHandler(image.Rows, image.Columns, 1);
            short index = 1;

            short[, ,] lookup = new short[256, 256, 256];
            if (dictionary != null)
            {
                Dictionary <Color, short> labelLookup = dictionary.LabelLookup;
                foreach (Color c in labelLookup.Keys)
                {
                    lookup[c.R, c.G, c.B] = labelLookup[c];
                }
            }
            else
            {
                for (int R = 0; R < 256; R++)
                {
                    for (int G = 0; G < 256; G++)
                    {
                        for (int B = 0; B < 256; B++)
                        {
                            lookup[R, G, B] = -1;
                        }
                    }
                }
            }

            fixed(byte *imageSrc = image.RawArray)
            {
                fixed(short *labelsSrc = RawArray)
                {
                    byte * imagePtr  = imageSrc;
                    short *labelsPtr = labelsSrc;

                    int count = image.Rows * image.Columns;

                    while (count-- > 0)
                    {
                        byte  R     = *imagePtr++;
                        byte  G     = *imagePtr++;
                        byte  B     = *imagePtr++;
                        short label = lookup[R, G, B];
                        if (label < 0)
                        {
                            label           = index;
                            lookup[R, G, B] = index++;
                        }
                        *labelsPtr++ = label;
                    }
                }
            }
        }
예제 #2
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="rows">Number of rows in the image</param>
 /// <param name="columns">Number of columns in the image</param>
 public LabelImage(int rows, int columns)
 {
     _handler = new ShortArrayHandler(rows, columns, 1);
 }