Ejemplo n.º 1
0
        /// <summary>
        /// Draws all blobs to the specified image.
        /// </summary>
        /// <param name="img">The target image to be drawn.</param>
        public void RenderBlobs(Mat img)
        {
            if (img == null)
            {
                throw new ArgumentNullException(nameof(img));
            }

            /*
             * if (img.Empty())
             *  throw new ArgumentException("img is empty");
             * if (img.Type() != MatType.CV_8UC3)
             *  throw new ArgumentException("img must be CV_8UC3");*/
            if (Blobs == null || Blobs.Count == 0)
            {
                throw new OpenCvSharpException("Blobs is empty");
            }
            if (Labels == null)
            {
                throw new OpenCvSharpException("Labels is empty");
            }

            var height = Labels.GetLength(0);
            var width  = Labels.GetLength(1);

            img.Create(new Size(width, height), MatType.CV_8UC3);

            var colors = new Scalar[Blobs.Count];

            colors[0] = Scalar.All(0);
            for (var i = 1; i < Blobs.Count; i++)
            {
                colors[i] = Scalar.RandomColor();
            }

            using (var imgt = new Mat <Vec3b>(img))
            {
                var indexer = imgt.GetIndexer();
                for (var y = 0; y < height; y++)
                {
                    for (var x = 0; x < width; x++)
                    {
                        var labelValue = Labels[y, x];
                        indexer[y, x] = colors[labelValue].ToVec3b();
                    }
                }
            }
        }