/// <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, RenderBlobsMode 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 & RenderBlobsMode.Color) == RenderBlobsMode.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); } }
/// <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, RenderBlobsMode 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); }
/// <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, RenderBlobsMode 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); }
/// <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, RenderBlobsMode mode, double alpha) { if (blobs == null) { throw new ArgumentNullException("blobs"); } if (imgSrc == null) { throw new ArgumentNullException("imgSrc"); } if (imgDst == null) { throw new ArgumentNullException("imgDst"); } if (imgDst.Type() != MatType.CV_8UC3) { throw new ArgumentException("'img' must be a 3-channel U8 image."); } var palette = new Dictionary <int, Scalar>(); if ((mode & RenderBlobsMode.Color) == RenderBlobsMode.Color) { int colorCount = 0; foreach (var kv in blobs) { double r, g, b; Hsv2Rgb((colorCount * 77) % 360, 0.5, 1.0, out r, out g, out b); colorCount++; palette[kv.Key] = new Scalar(b, g, r); } } foreach (var kv in blobs) { PerformOne(blobs.Labels, kv.Value, imgSrc, imgDst, mode, palette[kv.Key], alpha); } }
/// <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, IplImage imgSource, IplImage imgDest, RenderBlobsMode mode, double alpha) { if (blobs == null) { throw new ArgumentNullException("blobs"); } if (imgSource == null) { throw new ArgumentNullException("imgSource"); } if (imgDest == null) { throw new ArgumentNullException("imgDest"); } BlobRenderer.PerformMany(blobs, imgSource, imgDest, mode, alpha); }
/// <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> public static void RenderBlobs(CvBlobs blobs, IplImage imgSource, IplImage imgDest, RenderBlobsMode mode) { RenderBlobs(blobs, imgSource, imgDest, mode, 1.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 unsafe void PerformOne(LabelData labels, CvBlob blob, IplImage imgSrc, IplImage imgDst, RenderBlobsMode mode, CvScalar color, double alpha) { if (labels == null) { throw new ArgumentNullException("labels"); } if (blob == null) { throw new ArgumentNullException("blob"); } if (imgSrc == null) { throw new ArgumentNullException("imgSrc"); } if (imgDst == null) { throw new ArgumentNullException("imgDst"); } if (imgDst.Depth != BitDepth.U8 || imgDst.NChannels != 3) { throw new ArgumentException("'img' must be a 3-channel U8 image."); } if ((mode & RenderBlobsMode.Color) == RenderBlobsMode.Color) { int stepSrc = imgSrc.WidthStep; int stepDst = imgDst.WidthStep; int offsetSrc = 0; int offsetDst = 0; CvRect roiSrc = imgSrc.ROI; CvRect roiDst = imgDst.ROI; if (roiSrc.Size != imgSrc.Size) { offsetSrc = roiSrc.Y * stepSrc + roiSrc.X; } if (roiDst.Size != imgDst.Size) { offsetDst = roiDst.Y * stepDst + roiDst.X; } byte *pSrc = (byte *)imgSrc.ImageData + offsetSrc + (blob.MinY * stepSrc); byte *pDst = (byte *)imgDst.ImageData + offsetDst + (blob.MinY * stepDst); for (int r = blob.MinY; r < blob.MaxY; r++) { for (int c = blob.MinX; c < blob.MaxX; c++) { if (labels[r, c] == blob.Label) { pDst[c * 3 + 0] = (byte)((1.0 - alpha) * pSrc[c + 0] + alpha * color.Val0); pDst[c * 3 + 1] = (byte)((1.0 - alpha) * pSrc[c + 1] + alpha * color.Val1); pDst[c * 3 + 2] = (byte)((1.0 - alpha) * pSrc[c + 2] + alpha * color.Val2); } } pSrc += stepSrc; pDst += stepDst; } } if (mode != RenderBlobsMode.None) { if ((mode & RenderBlobsMode.BoundingBox) == RenderBlobsMode.BoundingBox) { Cv.Rectangle( imgDst, new CvPoint(blob.MinX, blob.MinY), new CvPoint(blob.MaxX - 1, blob.MaxY - 1), new CvColor(255, 0, 0)); } if ((mode & RenderBlobsMode.Angle) == RenderBlobsMode.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); Cv.Line(imgDst, new CvPoint((int)x1, (int)y1), Cv.Point((int)x2, (int)y2), new CvColor(0, 255, 0)); } if ((mode & RenderBlobsMode.Centroid) == RenderBlobsMode.Centroid) { Cv.Line(imgDst, new CvPoint((int)blob.Centroid.X - 3, (int)blob.Centroid.Y), new CvPoint((int)blob.Centroid.X + 3, (int)blob.Centroid.Y), new CvColor(0, 0, 255)); Cv.Line(imgDst, new CvPoint((int)blob.Centroid.X, (int)blob.Centroid.Y - 3), new CvPoint((int)blob.Centroid.X, (int)blob.Centroid.Y + 3), new CvColor(0, 0, 255)); } } }
/// <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(IplImage imgSource, IplImage imgDest, RenderBlobsMode mode) { CvBlobLib.RenderBlobs(this, imgSource, imgDest, mode); }
/// <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, IplImage imgSource, IplImage imgDest, RenderBlobsMode mode, CvScalar color, double alpha) { if (labels == null) throw new ArgumentNullException("labels"); if (blob == null) throw new ArgumentNullException("blob"); if (imgSource == null) throw new ArgumentNullException("imgSource"); if (imgDest == null) throw new ArgumentNullException("imgDest"); BlobRenderer.PerformOne(labels, blob, imgSource, imgDest, mode, color, alpha); }
/// <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, RenderBlobsMode mode) { CvBlobLib.RenderBlobs(this, imgSource, imgDest, mode); }
/// <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, RenderBlobsMode 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); }
/// <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, RenderBlobsMode mode) { RenderBlob(labels, blob, imgSource, imgDest, mode, Scalar.White, 1.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 unsafe void PerformOne(LabelData labels, CvBlob blob, Mat imgSrc, Mat imgDst, RenderBlobsMode mode, Scalar color, double alpha) { if (labels == null) throw new ArgumentNullException("labels"); if (blob == null) throw new ArgumentNullException("blob"); if (imgSrc == null) throw new ArgumentNullException("imgSrc"); if (imgDst == null) throw new ArgumentNullException("imgDst"); if (imgDst.Type() != MatType.CV_8UC3) throw new ArgumentException("'img' must be a 3-channel U8 image."); if ((mode & RenderBlobsMode.Color) == RenderBlobsMode.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 != RenderBlobsMode.None) { if ((mode & RenderBlobsMode.BoundingBox) == RenderBlobsMode.BoundingBox) { Cv2.Rectangle( imgDst, new Point(blob.MinX, blob.MinY), new Point(blob.MaxX - 1, blob.MaxY - 1), new Scalar(255, 0, 0)); } if ((mode & RenderBlobsMode.Angle) == RenderBlobsMode.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 & RenderBlobsMode.Centroid) == RenderBlobsMode.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)); } } }
/// <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, RenderBlobsMode mode, double alpha) { if (blobs == null) throw new ArgumentNullException("blobs"); if (imgSrc == null) throw new ArgumentNullException("imgSrc"); if (imgDst == null) throw new ArgumentNullException("imgDst"); if (imgDst.Type() != MatType.CV_8UC3) throw new ArgumentException("'img' must be a 3-channel U8 image."); var palette = new Dictionary<int, Scalar>(); if ((mode & RenderBlobsMode.Color) == RenderBlobsMode.Color) { int colorCount = 0; foreach (var kv in blobs) { double r, g, b; Hsv2Rgb((colorCount*77) % 360, 0.5, 1.0, out r, out g, out b); colorCount++; palette[kv.Key] = new Scalar(b, g, r); } } foreach (var kv in blobs) { PerformOne(blobs.Labels, kv.Value, imgSrc, imgDst, mode, palette[kv.Key], alpha); } }
/// <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 unsafe void PerformOne(LabelData labels, CvBlob blob, IplImage imgSrc, IplImage imgDst, RenderBlobsMode mode, CvScalar color, double alpha) { if (labels == null) throw new ArgumentNullException("labels"); if (blob == null) throw new ArgumentNullException("blob"); if (imgSrc == null) throw new ArgumentNullException("imgSrc"); if (imgDst == null) throw new ArgumentNullException("imgDst"); if (imgDst.Depth != BitDepth.U8 || imgDst.NChannels != 3) throw new ArgumentException("'img' must be a 3-channel U8 image."); if ((mode & RenderBlobsMode.Color) == RenderBlobsMode.Color) { int stepSrc = imgSrc.WidthStep; int stepDst = imgDst.WidthStep; int offsetSrc = 0; int offsetDst = 0; CvRect roiSrc = imgSrc.ROI; CvRect roiDst = imgDst.ROI; if (roiSrc.Size != imgSrc.Size) offsetSrc = roiSrc.Y * stepSrc + roiSrc.X; if (roiDst.Size != imgDst.Size) offsetDst = roiDst.Y * stepDst + roiDst.X; byte* pSrc = (byte*)imgSrc.ImageData + offsetSrc + (blob.MinY * stepSrc); byte* pDst = (byte*)imgDst.ImageData + offsetDst + (blob.MinY * stepDst); for (int r = blob.MinY; r < blob.MaxY; r++) { for (int c = blob.MinX; c < blob.MaxX; c++) { if (labels[r, c] == blob.Label) { pDst[c*3 + 0] = (byte) ((1.0 - alpha)*pSrc[c + 0] + alpha*color.Val0); pDst[c*3 + 1] = (byte) ((1.0 - alpha)*pSrc[c + 1] + alpha*color.Val1); pDst[c*3 + 2] = (byte) ((1.0 - alpha)*pSrc[c + 2] + alpha*color.Val2); } } pSrc += stepSrc; pDst += stepDst; } } if (mode != RenderBlobsMode.None) { if ((mode & RenderBlobsMode.BoundingBox) == RenderBlobsMode.BoundingBox) { Cv.Rectangle( imgDst, new CvPoint(blob.MinX, blob.MinY), new CvPoint(blob.MaxX - 1, blob.MaxY - 1), new CvColor(255, 0, 0)); } if ((mode & RenderBlobsMode.Angle) == RenderBlobsMode.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); Cv.Line(imgDst, new CvPoint((int)x1, (int)y1), Cv.Point((int)x2, (int)y2), new CvColor(0, 255, 0)); } if ((mode & RenderBlobsMode.Centroid) == RenderBlobsMode.Centroid) { Cv.Line(imgDst, new CvPoint((int)blob.Centroid.X - 3, (int)blob.Centroid.Y), new CvPoint((int)blob.Centroid.X + 3, (int)blob.Centroid.Y), new CvColor(0, 0, 255)); Cv.Line(imgDst, new CvPoint((int)blob.Centroid.X, (int)blob.Centroid.Y - 3), new CvPoint((int)blob.Centroid.X, (int)blob.Centroid.Y + 3), new CvColor(0, 0, 255)); } } }
/// <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, IplImage imgSrc, IplImage imgDst, RenderBlobsMode mode, double alpha) { if (blobs == null) throw new ArgumentNullException("blobs"); if (imgSrc == null) throw new ArgumentNullException("imgSrc"); if (imgDst == null) throw new ArgumentNullException("imgDst"); if (imgDst.Depth != BitDepth.U8 || imgDst.NChannels != 3) throw new ArgumentException("'img' must be a 3-channel U8 image."); var palette = new Dictionary<int, CvColor>(); if ((mode & RenderBlobsMode.Color) == RenderBlobsMode.Color) { int colorCount = 0; foreach (var kv in blobs) { double r, g, b; Hsv2Rgb((colorCount*77) % 360, 0.5, 1.0, out r, out g, out b); colorCount++; palette[kv.Key] = new CvColor((int)r, (int)g, (int)b); } } foreach (var kv in blobs) { PerformOne(blobs.Labels, kv.Value, imgSrc, imgDst, mode, palette[kv.Key], alpha); } }
/// <summary> /// Draws or prints information about a blob. /// </summary> /// <param name="imgLabel">Label image (depth=IPL_DEPTH_LABEL and num. channels=1).</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> public static void RenderBlob(IplImage imgLabel, CvBlob blob, IplImage imgSource, IplImage imgDest, RenderBlobsMode mode, CvScalar color) { RenderBlob(imgLabel, blob, imgSource, imgDest, mode, color, 1.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, RenderBlobsMode 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); }
/// <summary> /// Draws or prints information about blobs. (cvRenderBlobs) /// </summary> /// <param name="imgLabel">Label image (depth=IPL_DEPTH_LABEL and num. channels=1).</param> /// <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> public static void RenderBlobs(IplImage imgLabel, CvBlobs blobs, IplImage imgSource, IplImage imgDest, RenderBlobsMode mode) { RenderBlobs(imgLabel, blobs, imgSource, imgDest, mode, 1.0); }
/// <summary> /// Draws or prints information about a blob. /// </summary> /// <param name="imgLabel">Label image (depth=IPL_DEPTH_LABEL and num. channels=1).</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(IplImage imgLabel, CvBlob blob, IplImage imgSource, IplImage imgDest, RenderBlobsMode mode) { RenderBlob(imgLabel, blob, imgSource, imgDest, mode, CvColor.White, 1.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, IplImage imgSource, IplImage imgDest, RenderBlobsMode mode, double alpha) { if (blobs == null) throw new ArgumentNullException("blobs"); if (imgSource == null) throw new ArgumentNullException("imgSource"); if (imgDest == null) throw new ArgumentNullException("imgDest"); BlobRenderer.PerformMany(blobs, imgSource, imgDest, mode, alpha); }
/// <summary> /// Draws or prints information about a blob. /// </summary> /// <param name="imgLabel">Label image (depth=IPL_DEPTH_LABEL and num. channels=1).</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(IplImage imgLabel, CvBlob blob, IplImage imgSource, IplImage imgDest, RenderBlobsMode mode, CvScalar color, double alpha) { if (imgLabel == null) throw new ArgumentNullException("imgLabel"); if (blob == null) throw new ArgumentNullException("blob"); if (imgSource == null) throw new ArgumentNullException("imgSource"); if (imgDest == null) throw new ArgumentNullException("imgDest"); CvBlobInvoke.cvb_cvRenderBlob(imgLabel.CvPtr, blob.CvPtr, imgSource.CvPtr, imgDest.CvPtr, mode, color, alpha); }
/// <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, IplImage imgSource, IplImage imgDest, RenderBlobsMode mode) { RenderBlob(labels, blob, imgSource, imgDest, mode, CvColor.White, 1.0); }
/// <summary> /// Draws or prints information about blobs. (cvRenderBlobs) /// </summary> /// <param name="imgLabel">Label image (depth=IPL_DEPTH_LABEL and num. channels=1).</param> /// <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(IplImage imgLabel, CvBlobs blobs, IplImage imgSource, IplImage imgDest, RenderBlobsMode mode, double alpha) { if (imgLabel == null) throw new ArgumentNullException("imgLabel"); if (blobs == null) throw new ArgumentNullException("blobs"); if (imgSource == null) throw new ArgumentNullException("imgSource"); if (imgDest == null) throw new ArgumentNullException("imgDest"); CvBlobInvoke.cvb_cvRenderBlobs(imgLabel.CvPtr, blobs.CvPtr, imgSource.CvPtr, imgDest.CvPtr, mode, alpha); }
/// <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> public static void RenderBlob(LabelData labels, CvBlob blob, IplImage imgSource, IplImage imgDest, RenderBlobsMode mode, CvScalar color) { RenderBlob(labels, blob, imgSource, imgDest, mode, color, 1.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, RenderBlobsMode 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 & RenderBlobsMode.Color) == RenderBlobsMode.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 != RenderBlobsMode.None) { if ((mode & RenderBlobsMode.BoundingBox) == RenderBlobsMode.BoundingBox) { Cv2.Rectangle( imgDst, new Point(blob.MinX, blob.MinY), new Point(blob.MaxX, blob.MaxY), new Scalar(255, 0, 0)); } if ((mode & RenderBlobsMode.Angle) == RenderBlobsMode.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 & RenderBlobsMode.Centroid) == RenderBlobsMode.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)); } } }
/// <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, IplImage imgSource, IplImage imgDest, RenderBlobsMode mode, CvScalar color, double alpha) { if (labels == null) { throw new ArgumentNullException("labels"); } if (blob == null) { throw new ArgumentNullException("blob"); } if (imgSource == null) { throw new ArgumentNullException("imgSource"); } if (imgDest == null) { throw new ArgumentNullException("imgDest"); } BlobRenderer.PerformOne(labels, blob, imgSource, imgDest, mode, color, alpha); }
/// <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(IplImage imgSource, IplImage imgDest, RenderBlobsMode mode, Double alpha) { CvBlobLib.RenderBlobs(this, imgSource, imgDest, mode, alpha); }
/// <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, IplImage imgSrc, IplImage imgDst, RenderBlobsMode mode, double alpha) { if (blobs == null) { throw new ArgumentNullException("blobs"); } if (imgSrc == null) { throw new ArgumentNullException("imgSrc"); } if (imgDst == null) { throw new ArgumentNullException("imgDst"); } if (imgDst.Depth != BitDepth.U8 || imgDst.NChannels != 3) { throw new ArgumentException("'img' must be a 3-channel U8 image."); } var palette = new Dictionary <int, CvColor>(); if ((mode & RenderBlobsMode.Color) == RenderBlobsMode.Color) { int colorCount = 0; foreach (var kv in blobs) { double r, g, b; Hsv2Rgb((colorCount * 77) % 360, 0.5, 1.0, out r, out g, out b); colorCount++; palette[kv.Key] = new CvColor((int)r, (int)g, (int)b); } } foreach (var kv in blobs) { PerformOne(blobs.Labels, kv.Value, imgSrc, imgDst, mode, palette[kv.Key], alpha); } }