Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="blobs"></param>
        /// <param name="imgSrc"></param>
        /// <param name="imgDst"></param>
        /// <param name="mode"></param>
        /// <param name="alpha"></param>
        public static void PerformMany(CvBlobs blobs, Mat imgSrc, Mat imgDst, RenderBlobsModes mode, double alpha)
        {
            if (blobs == null)
            {
                throw new ArgumentNullException(nameof(blobs));
            }
            if (imgSrc == null)
            {
                throw new ArgumentNullException(nameof(imgSrc));
            }
            if (imgDst == null)
            {
                throw new ArgumentNullException(nameof(imgDst));
            }
            if (imgDst.Type() != MatType.CV_8UC3)
            {
                throw new ArgumentException("'img' must be a 3-channel U8 image.");
            }
            if (blobs.Labels == null)
            {
                throw new NotSupportedException("blobs.Labels == null");
            }

            var palette = new Dictionary <int, Scalar>();

            if ((mode & RenderBlobsModes.Color) == RenderBlobsModes.Color)
            {
                int colorCount = 0;
                foreach (var kv in blobs)
                {
                    Hsv2Rgb((colorCount * 77) % 360, 0.5, 1.0, out var r, out var g, out var b);
                    colorCount++;
                    palette[kv.Key] = new Scalar(b, g, r);
                }
            }

            foreach (var kv in blobs)
            {
                Scalar color = default;
                if (palette.ContainsKey(kv.Key))
                {
                    color = palette[kv.Key];
                }
                PerformOne(blobs.Labels, kv.Value, imgSrc, imgDst, mode, color, alpha);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Draws or prints information about blobs. (cvRenderBlobs)
        /// </summary>
        /// <param name="blobs">List of blobs.</param>
        /// <param name="imgSource">Input image (depth=IPL_DEPTH_8U and num. channels=3).</param>
        /// <param name="imgDest">Output image (depth=IPL_DEPTH_8U and num. channels=3).</param>
        /// <param name="mode">Render mode. By default is CV_BLOB_RENDER_COLOR|CV_BLOB_RENDER_CENTROID|CV_BLOB_RENDER_BOUNDING_BOX|CV_BLOB_RENDER_ANGLE.</param>
        /// <param name="alpha">If mode CV_BLOB_RENDER_COLOR is used. 1.0 indicates opaque and 0.0 translucent (1.0 by default).</param>
        public static void RenderBlobs(CvBlobs blobs, Mat imgSource, Mat imgDest, RenderBlobsModes mode, double alpha = 1.0)
        {
            if (blobs == null)
            {
                throw new ArgumentNullException(nameof(blobs));
            }
            if (imgSource == null)
            {
                throw new ArgumentNullException(nameof(imgSource));
            }
            if (imgDest == null)
            {
                throw new ArgumentNullException(nameof(imgDest));
            }

            BlobRenderer.PerformMany(blobs, imgSource, imgDest, mode, alpha);
        }
Beispiel #3
0
        /// <summary>
        /// Draws or prints information about a blob.
        /// </summary>
        /// <param name="labels">Label data.</param>
        /// <param name="blob">Blob.</param>
        /// <param name="imgSource">Input image (depth=IPL_DEPTH_8U and num. channels=3).</param>
        /// <param name="imgDest">Output image (depth=IPL_DEPTH_8U and num. channels=3).</param>
        /// <param name="mode">Render mode. By default is CV_BLOB_RENDER_COLOR|CV_BLOB_RENDER_CENTROID|CV_BLOB_RENDER_BOUNDING_BOX|CV_BLOB_RENDER_ANGLE.</param>
        /// <param name="color">Color to render (if CV_BLOB_RENDER_COLOR is used).</param>
        /// <param name="alpha">If mode CV_BLOB_RENDER_COLOR is used. 1.0 indicates opaque and 0.0 translucent (1.0 by default).</param>
        public static void RenderBlob(LabelData labels, CvBlob blob, Mat imgSource, Mat imgDest,
                                      RenderBlobsModes mode, Scalar color, double alpha = 1.0)
        {
            if (labels == null)
            {
                throw new ArgumentNullException(nameof(labels));
            }
            if (blob == null)
            {
                throw new ArgumentNullException(nameof(blob));
            }
            if (imgSource == null)
            {
                throw new ArgumentNullException(nameof(imgSource));
            }
            if (imgDest == null)
            {
                throw new ArgumentNullException(nameof(imgDest));
            }

            BlobRenderer.PerformOne(labels, blob, imgSource, imgDest, mode, color, alpha);
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="labels"></param>
        /// <param name="blob"></param>
        /// <param name="imgSrc"></param>
        /// <param name="imgDst"></param>
        /// <param name="mode"></param>
        /// <param name="color"></param>
        /// <param name="alpha"></param>
        public static void PerformOne(LabelData labels, CvBlob blob, Mat imgSrc, Mat imgDst,
                                      RenderBlobsModes mode, Scalar color, double alpha)
        {
            if (labels == null)
            {
                throw new ArgumentNullException(nameof(labels));
            }
            if (blob == null)
            {
                throw new ArgumentNullException(nameof(blob));
            }
            if (imgSrc == null)
            {
                throw new ArgumentNullException(nameof(imgSrc));
            }
            if (imgDst == null)
            {
                throw new ArgumentNullException(nameof(imgDst));
            }
            if (imgDst.Type() != MatType.CV_8UC3)
            {
                throw new ArgumentException("'img' must be a 3-channel U8 image.");
            }

            if ((mode & RenderBlobsModes.Color) == RenderBlobsModes.Color)
            {
                var pSrc = imgSrc.GetGenericIndexer <Vec3b>();
                var pDst = imgDst.GetGenericIndexer <Vec3b>();

                for (int r = blob.MinY; r <= blob.MaxY; r++)
                {
                    for (int c = blob.MinX; c <= blob.MaxX; c++)
                    {
                        if (labels[r, c] == blob.Label)
                        {
                            byte v0 = (byte)((1.0 - alpha) * pSrc[r, c].Item0 + alpha * color.Val0);
                            byte v1 = (byte)((1.0 - alpha) * pSrc[r, c].Item1 + alpha * color.Val1);
                            byte v2 = (byte)((1.0 - alpha) * pSrc[r, c].Item2 + alpha * color.Val2);
                            pDst[r, c] = new Vec3b(v0, v1, v2);
                        }
                    }
                }
            }

            if (mode != RenderBlobsModes.None)
            {
                if ((mode & RenderBlobsModes.BoundingBox) == RenderBlobsModes.BoundingBox)
                {
                    Cv2.Rectangle(
                        imgDst,
                        new Point(blob.MinX, blob.MinY),
                        new Point(blob.MaxX, blob.MaxY),
                        new Scalar(255, 0, 0));
                }
                if ((mode & RenderBlobsModes.Angle) == RenderBlobsModes.Angle)
                {
                    double angle      = blob.Angle();
                    double lengthLine = Math.Max(blob.MaxX - blob.MinX, blob.MaxY - blob.MinY) / 2.0;
                    double x1         = blob.Centroid.X - lengthLine * Math.Cos(angle);
                    double y1         = blob.Centroid.Y - lengthLine * Math.Sin(angle);
                    double x2         = blob.Centroid.X + lengthLine * Math.Cos(angle);
                    double y2         = blob.Centroid.Y + lengthLine * Math.Sin(angle);
                    Cv2.Line(imgDst, new Point((int)x1, (int)y1), new Point((int)x2, (int)y2),
                             new Scalar(0, 255, 0));
                }
                if ((mode & RenderBlobsModes.Centroid) == RenderBlobsModes.Centroid)
                {
                    Cv2.Line(imgDst,
                             new Point((int)blob.Centroid.X - 3, (int)blob.Centroid.Y),
                             new Point((int)blob.Centroid.X + 3, (int)blob.Centroid.Y),
                             new Scalar(255, 0, 0));
                    Cv2.Line(imgDst,
                             new Point((int)blob.Centroid.X, (int)blob.Centroid.Y - 3),
                             new Point((int)blob.Centroid.X, (int)blob.Centroid.Y + 3),
                             new Scalar(255, 0, 0));
                }
            }
        }
Beispiel #5
0
 /// <summary>
 /// Draws or prints information about a blob.
 /// </summary>
 /// <param name="labels">Label data.</param>
 /// <param name="blob">Blob.</param>
 /// <param name="imgSource">Input image (depth=IPL_DEPTH_8U and num. channels=3).</param>
 /// <param name="imgDest">Output image (depth=IPL_DEPTH_8U and num. channels=3).</param>
 /// <param name="mode">Render mode. By default is CV_BLOB_RENDER_COLOR|CV_BLOB_RENDER_CENTROID|CV_BLOB_RENDER_BOUNDING_BOX|CV_BLOB_RENDER_ANGLE.</param>
 public static void RenderBlob(LabelData labels, CvBlob blob, Mat imgSource, Mat imgDest,
                               RenderBlobsModes mode)
 {
     RenderBlob(labels, blob, imgSource, imgDest, mode, Scalar.White, 1.0);
 }
Beispiel #6
0
 /// <summary>
 /// Draws or prints information about blobs. (cvRenderBlobs)
 /// </summary>
 /// <param name="imgSource">Input image (depth=IPL_DEPTH_8U and num. channels=3).</param>
 /// <param name="imgDest">Output image (depth=IPL_DEPTH_8U and num. channels=3).</param>
 /// <param name="mode">Render mode. By default is CV_BLOB_RENDER_COLOR|CV_BLOB_RENDER_CENTROID|CV_BLOB_RENDER_BOUNDING_BOX|CV_BLOB_RENDER_ANGLE.</param>
 /// <param name="alpha">If mode CV_BLOB_RENDER_COLOR is used. 1.0 indicates opaque and 0.0 translucent (1.0 by default).</param>
 public void RenderBlobs(Mat imgSource, Mat imgDest, RenderBlobsModes mode, double alpha)
 {
     CvBlobLib.RenderBlobs(this, imgSource, imgDest, mode, alpha);
 }
Beispiel #7
0
 /// <summary>
 /// Draws or prints information about blobs. (cvRenderBlobs)
 /// </summary>
 /// <param name="imgSource">Input image (depth=IPL_DEPTH_8U and num. channels=3).</param>
 /// <param name="imgDest">Output image (depth=IPL_DEPTH_8U and num. channels=3).</param>
 /// <param name="mode">Render mode. By default is CV_BLOB_RENDER_COLOR|CV_BLOB_RENDER_CENTROID|CV_BLOB_RENDER_BOUNDING_BOX|CV_BLOB_RENDER_ANGLE.</param>
 public void RenderBlobs(Mat imgSource, Mat imgDest, RenderBlobsModes mode)
 {
     CvBlobLib.RenderBlobs(this, imgSource, imgDest, mode);
 }