From 422c07aa7645c20f48aec952052e3acfbf3c3e39 Mon Sep 17 00:00:00 2001 From: shimat Date: Sat, 6 Feb 2021 10:51:51 +0900 Subject: [PATCH] remove Blob --- OpenCvSharp.sln | 29 +- nuget/OpenCvSharp4.nuspec | 17 +- src/OpenCvSharp.Blob/BlobRenderer.cs | 207 ----- src/OpenCvSharp.Blob/CvBlob.cs | 292 ------- src/OpenCvSharp.Blob/CvBlobConst.cs | 136 ---- src/OpenCvSharp.Blob/CvBlobLib.cs | 515 ------------- src/OpenCvSharp.Blob/CvBlobs.cs | 721 ------------------ src/OpenCvSharp.Blob/CvChainCode.cs | 44 -- src/OpenCvSharp.Blob/CvContourChainCode.cs | 144 ---- src/OpenCvSharp.Blob/CvContourPolygon.cs | 443 ----------- src/OpenCvSharp.Blob/CvTrack.cs | 73 -- src/OpenCvSharp.Blob/CvTracks.cs | 150 ---- src/OpenCvSharp.Blob/Key.snk | Bin 596 -> 0 bytes src/OpenCvSharp.Blob/LabelData.cs | 121 --- src/OpenCvSharp.Blob/Labeller.cs | 356 --------- src/OpenCvSharp.Blob/OpenCvSharp.Blob.csproj | 48 -- .../Properties/AssemblyInfo.cs | 18 - src/OpenCvSharp.Blob/ProximityMatrix.cs | 97 --- src/OpenCvSharp.Blob/ReadMe.txt | 3 - src/OpenCvSharp.Blob/RenderBlobsMode.cs | 52 -- src/OpenCvSharp.Blob/RenderTracksMode.cs | 29 - 21 files changed, 2 insertions(+), 3493 deletions(-) delete mode 100644 src/OpenCvSharp.Blob/BlobRenderer.cs delete mode 100644 src/OpenCvSharp.Blob/CvBlob.cs delete mode 100644 src/OpenCvSharp.Blob/CvBlobConst.cs delete mode 100644 src/OpenCvSharp.Blob/CvBlobLib.cs delete mode 100644 src/OpenCvSharp.Blob/CvBlobs.cs delete mode 100644 src/OpenCvSharp.Blob/CvChainCode.cs delete mode 100644 src/OpenCvSharp.Blob/CvContourChainCode.cs delete mode 100644 src/OpenCvSharp.Blob/CvContourPolygon.cs delete mode 100644 src/OpenCvSharp.Blob/CvTrack.cs delete mode 100644 src/OpenCvSharp.Blob/CvTracks.cs delete mode 100644 src/OpenCvSharp.Blob/Key.snk delete mode 100644 src/OpenCvSharp.Blob/LabelData.cs delete mode 100644 src/OpenCvSharp.Blob/Labeller.cs delete mode 100644 src/OpenCvSharp.Blob/OpenCvSharp.Blob.csproj delete mode 100644 src/OpenCvSharp.Blob/Properties/AssemblyInfo.cs delete mode 100644 src/OpenCvSharp.Blob/ProximityMatrix.cs delete mode 100644 src/OpenCvSharp.Blob/ReadMe.txt delete mode 100644 src/OpenCvSharp.Blob/RenderBlobsMode.cs delete mode 100644 src/OpenCvSharp.Blob/RenderTracksMode.cs diff --git a/OpenCvSharp.sln b/OpenCvSharp.sln index 412ef7c74..ccd3fbcd8 100644 --- a/OpenCvSharp.sln +++ b/OpenCvSharp.sln @@ -14,8 +14,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenCvSharp", "src\OpenCvSh EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenCvSharp.Tests", "test\OpenCvSharp.Tests\OpenCvSharp.Tests.csproj", "{FFBBCF99-97F0-4F81-AAF6-8D851A8E1D2E}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenCvSharp.Blob", "src\OpenCvSharp.Blob\OpenCvSharp.Blob.csproj", "{82AFDA65-515E-4EC0-A415-77D8A6711508}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenCvSharp.Extensions", "src\OpenCvSharp.Extensions\OpenCvSharp.Extensions.csproj", "{B4B78BB2-1B7C-4CF2-BC72-43789EEDCE00}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OpenCvSharpExtern", "src\OpenCvSharpExtern\OpenCvSharpExtern.vcxproj", "{8E7279F8-F801-4672-B42F-1ED2C68B16A4}" @@ -26,7 +24,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tool", "tool", "{A6E578C0-A EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenCvSharp.ReleaseMaker", "tool\OpenCvSharp.ReleaseMaker\OpenCvSharp.ReleaseMaker.csproj", "{E2C8C528-B7C7-40AF-BB7F-1147A41E2E23}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenCvSharp.NupkgBetaRemover", "tool\OpenCvSharp.NupkgBetaRemover\OpenCvSharp.NupkgBetaRemover.csproj", "{CC19F9A5-01A7-4BDF-B34C-CF56F46A474A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenCvSharp.NupkgBetaRemover", "tool\OpenCvSharp.NupkgBetaRemover\OpenCvSharp.NupkgBetaRemover.csproj", "{CC19F9A5-01A7-4BDF-B34C-CF56F46A474A}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "uwpOpenCvSharpExtern", "src\uwpOpenCvSharpExtern\uwpOpenCvSharpExtern.vcxproj", "{BD5471E5-7B55-5192-8DA4-042B66AF71AE}" EndProject @@ -96,30 +94,6 @@ Global {FFBBCF99-97F0-4F81-AAF6-8D851A8E1D2E}.Release-JP|x64.Build.0 = Release|Any CPU {FFBBCF99-97F0-4F81-AAF6-8D851A8E1D2E}.Release-JP|x86.ActiveCfg = Release|Any CPU {FFBBCF99-97F0-4F81-AAF6-8D851A8E1D2E}.Release-JP|x86.Build.0 = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Debug|Any CPU.Build.0 = Debug|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Debug|ARM.ActiveCfg = Debug|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Debug|ARM.Build.0 = Debug|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Debug|x64.ActiveCfg = Debug|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Debug|x64.Build.0 = Debug|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Debug|x86.ActiveCfg = Debug|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Debug|x86.Build.0 = Debug|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release|Any CPU.ActiveCfg = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release|Any CPU.Build.0 = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release|ARM.ActiveCfg = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release|ARM.Build.0 = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release|x64.ActiveCfg = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release|x64.Build.0 = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release|x86.ActiveCfg = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release|x86.Build.0 = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release-JP|Any CPU.ActiveCfg = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release-JP|Any CPU.Build.0 = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release-JP|ARM.ActiveCfg = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release-JP|ARM.Build.0 = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release-JP|x64.ActiveCfg = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release-JP|x64.Build.0 = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release-JP|x86.ActiveCfg = Release|Any CPU - {82AFDA65-515E-4EC0-A415-77D8A6711508}.Release-JP|x86.Build.0 = Release|Any CPU {B4B78BB2-1B7C-4CF2-BC72-43789EEDCE00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B4B78BB2-1B7C-4CF2-BC72-43789EEDCE00}.Debug|Any CPU.Build.0 = Debug|Any CPU {B4B78BB2-1B7C-4CF2-BC72-43789EEDCE00}.Debug|ARM.ActiveCfg = Debug|Any CPU @@ -288,7 +262,6 @@ Global GlobalSection(NestedProjects) = preSolution {EB310923-197F-4E20-B123-3A3E7F1D5069} = {E048D213-B3B9-453F-9A41-29FDEB0D496B} {FFBBCF99-97F0-4F81-AAF6-8D851A8E1D2E} = {1F113DD0-E292-47A5-8EFF-3FB5D0869BF3} - {82AFDA65-515E-4EC0-A415-77D8A6711508} = {E048D213-B3B9-453F-9A41-29FDEB0D496B} {B4B78BB2-1B7C-4CF2-BC72-43789EEDCE00} = {E048D213-B3B9-453F-9A41-29FDEB0D496B} {8E7279F8-F801-4672-B42F-1ED2C68B16A4} = {E048D213-B3B9-453F-9A41-29FDEB0D496B} {4232CB4A-DFE3-46CA-9503-C5F1798BAED3} = {E048D213-B3B9-453F-9A41-29FDEB0D496B} diff --git a/nuget/OpenCvSharp4.nuspec b/nuget/OpenCvSharp4.nuspec index a6b043c5d..1d2964564 100644 --- a/nuget/OpenCvSharp4.nuspec +++ b/nuget/OpenCvSharp4.nuspec @@ -55,9 +55,6 @@ - - - @@ -65,9 +62,6 @@ - - - @@ -75,9 +69,6 @@ - - - @@ -85,9 +76,6 @@ - - - @@ -95,11 +83,8 @@ - - - - \ No newline at end of file + diff --git a/src/OpenCvSharp.Blob/BlobRenderer.cs b/src/OpenCvSharp.Blob/BlobRenderer.cs deleted file mode 100644 index 22949551a..000000000 --- a/src/OpenCvSharp.Blob/BlobRenderer.cs +++ /dev/null @@ -1,207 +0,0 @@ -using System; -using System.Collections.Generic; - -// Copyright (C) 2007 by Cristóbal Carnero Liñán -// grendel.ccl@gmail.com -// -// This file is part of cvBlob. -// -// cvBlob is free software: you can redistribute it and/or modify -// it under the terms of the Lesser GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// cvBlob is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// Lesser GNU General Public License for more details. -// -// You should have received a copy of the Lesser GNU General Public License -// along with cvBlob. If not, see . - -namespace OpenCvSharp.Blob -{ - /// - /// - /// - internal static class BlobRenderer - { - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - 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(); - var pDst = imgDst.GetGenericIndexer(); - - 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)); - } - } - } - - /// - /// - /// - /// - /// - /// - /// - /// - 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(); - 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); - } - } - - /// - /// - /// - /// - /// - /// - /// - /// - /// - private static void Hsv2Rgb(double h, double s, double v, out double r, out double g, out double b) - { - double hh = h / 60.0; - int hf = (int)Math.Floor(hh); - int hi = ((int)hh) % 6; - double f = hh - hf; - - double p = v * (1.0 - s); - double q = v * (1.0 - f * s); - double t = v * (1.0 - (1.0 - f) * s); - - switch (hi) - { - case 0: - r = 255.0 * v; - g = 255.0 * t; - b = 255.0 * p; - break; - case 1: - r = 255.0 * q; - g = 255.0 * v; - b = 255.0 * p; - break; - case 2: - r = 255.0 * p; - g = 255.0 * v; - b = 255.0 * t; - break; - case 3: - r = 255.0 * p; - g = 255.0 * q; - b = 255.0 * v; - break; - case 4: - r = 255.0 * t; - g = 255.0 * p; - b = 255.0 * v; - break; - case 5: - r = 255.0 * v; - g = 255.0 * p; - b = 255.0 * q; - break; - default: - throw new Exception(); - } - } - } -} diff --git a/src/OpenCvSharp.Blob/CvBlob.cs b/src/OpenCvSharp.Blob/CvBlob.cs deleted file mode 100644 index 24ebefbd4..000000000 --- a/src/OpenCvSharp.Blob/CvBlob.cs +++ /dev/null @@ -1,292 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace OpenCvSharp.Blob -{ - /// - /// Struct that contain information about one blob. - /// - [Serializable] - public class CvBlob - { - #region Init - - /// - /// Constructor - /// - public CvBlob() - { - Contour = new CvContourChainCode(); - InternalContours = new List(); - } - - /// - /// - /// - /// - /// - /// - internal CvBlob(int label, int x, int y) : this() - { - Label = label; - Area = 1; - MinX = x; - MaxX = x; - MinY = y; - MaxY = y; - M10 = x; - M01 = y; - M11 = x*y; - M20 = x*x; - M02 = y*y; - } - - #endregion - - #region Properties - - /// - /// Label assigned to the blob - /// - public int Label { get; set; } - - /// - /// Area (moment 00) - /// - public int Area { get; set; } - - /// - /// Area (moment 00) - /// - public int M00 - { - get { return Area; } - set { Area = value; } - } - - /// - /// X min - /// - public int MinX { get; set; } - - /// - /// X max - /// - public int MaxX { get; set; } - - /// - /// Y min - /// - public int MinY { get; set; } - - /// - /// Y max - /// - public int MaxY { get; set; } - - /// - /// CvRect(MinX, MinY, MaxX - MinX, MaxY - MinY) - /// - public Rect Rect - { - get { return new Rect(MinX, MinY, MaxX - MinX + 1, MaxY - MinY + 1); } - set - { - MinX = value.Left; - MinY = value.Top; - MaxX = value.Right; - MaxY = value.Bottom; - } - } - - /// - /// Centroid - /// - public Point2d Centroid { get; internal set; } - - /// - /// Moment 10 - /// - public double M10 { get; set; } - - /// - /// Moment 01 - /// - public double M01 { get; set; } - - /// - /// Moment 11 - /// - public double M11 { get; set; } - - /// - /// Moment 20 - /// - public double M20 { get; set; } - - /// - /// Moment 02 - /// - public double M02 { get; set; } - - /// - /// True if central moments are being calculated - /// - public bool CentralMoments { get; set; } - - /// - /// Central moment 11 - /// - public double U11 { get; internal set; } - - /// - /// Central moment 20 - /// - public double U20 { get; internal set; } - - /// - /// Central moment 02 - /// - public double U02 { get; internal set; } - - /// - /// Normalized central moment 11. - /// - public double N11 { get; internal set; } - - /// - /// Normalized central moment 20. - /// - public double N20 { get; internal set; } - - /// - /// Normalized central moment 02. - /// - public double N02 { get; internal set; } - - /// - /// Hu moment 1. - /// - public double P1 { get; set; } - - /// - /// Hu moment 2. - /// - public double P2 { get; set; } - - /// - /// Contour - /// - public CvContourChainCode Contour { get; internal set; } - - /// - /// Internal contours - /// - public List InternalContours { get; internal set; } - - #endregion - - #region Methods - - /// - /// Calculates angle orientation of a blob. - /// This function uses central moments so cvCentralMoments should have been called before for this blob. (cvAngle) - /// - /// Angle orientation in radians. - public double Angle() - { - return 0.5*Math.Atan2(2.0*U11, (U20 - U02)); - } - - - /// - /// Calculates centroid. - /// Centroid will be returned and stored in the blob structure. (cvCentroid) - /// - /// Centroid. - public Point2d CalcCentroid() - { - Centroid = new Point2d(M10 / Area, M01 / Area); - return Centroid; - } - - /// - /// Save the image of a blob to a file. - /// The function uses an image (that can be the original pre-processed image or a processed one, or even the result of cvRenderBlobs, for example) and a blob structure. - /// Then the function saves a copy of the part of the image where the blob is. - /// - /// Name of the file. - /// Image. - public void SaveImage(string fileName, Mat img) - { - if (string.IsNullOrEmpty(fileName)) - throw new ArgumentNullException(nameof(fileName)); - if (img == null) - throw new ArgumentNullException(nameof(img)); - - using (var subMat = img.SubMat(Rect)) - { - subMat.SaveImage(fileName); - } - } - - /// - /// Set central/hu moments and centroid value from moment values (M**) - /// - public void SetMoments() - { - // 重心 - Centroid = new Point2d(M10/Area, M01/Area); - // 各モーメント - U11 = M11 - (M10*M01)/M00; - U20 = M20 - (M10*M10)/M00; - U02 = M02 - (M01*M01)/M00; - - double m00Pow2 = M00*M00; - N11 = U11/m00Pow2; - N20 = U20/m00Pow2; - N02 = U02/m00Pow2; - - P1 = N20 + N02; - double nn = N20 - N02; - P2 = nn*nn + 4.0*(N11*N11); - } - - /// - /// - /// - /// - public CvBlob Clone() - { - return new CvBlob - { - Area = Area, - CentralMoments = CentralMoments, - Centroid = Centroid, - Contour = Contour.Clone(), - InternalContours = new List(InternalContours), - Label = Label, - M00 = M00, - M01 = M01, - M02 = M02, - M10 = M10, - M11 = M11, - M20 = M20, - MaxX = MaxX, - MaxY = MaxY, - MinX = MinX, - MinY = MinY, - N02 = N02, - N11 = N11, - N20 = N20, - P1 = P1, - P2 = P2, - U02 = U02, - U11 = U11, - U20 = U20, - }; - } - - #endregion - } -} diff --git a/src/OpenCvSharp.Blob/CvBlobConst.cs b/src/OpenCvSharp.Blob/CvBlobConst.cs deleted file mode 100644 index abeb84fc6..000000000 --- a/src/OpenCvSharp.Blob/CvBlobConst.cs +++ /dev/null @@ -1,136 +0,0 @@ -namespace OpenCvSharp.Blob -{ - /// - /// Constants which are defined by cvblob - /// - public static class CvBlobConst - { - // ReSharper disable InconsistentNaming - - #region RenderBlobsMode - -#pragma warning disable CA1707 - /// - /// Render each blog with a different color. - /// - public const ushort CV_BLOB_RENDER_COLOR = 0x0001; - - /// - /// Render centroid. - /// - public const ushort CV_BLOB_RENDER_CENTROID = 0x0002; - - /// - /// Render bounding box. - /// - public const ushort CV_BLOB_RENDER_BOUNDING_BOX = 0x0004; - - /// - /// Render angle. - /// - public const ushort CV_BLOB_RENDER_ANGLE = 0x0008; - - /// - /// Print blob data to log out. - /// - public const ushort CV_BLOB_RENDER_TO_LOG = 0x0010; - - /// - /// Print blob data to std out. - /// - public const ushort CV_BLOB_RENDER_TO_STD = 0x0020; -#pragma warning restore CA1707 - - #endregion - - #region CvChainCode - -#pragma warning disable CA1707 - /// - /// Up. - /// - public const byte CV_CHAINCODE_UP = 0; - - /// - /// Up and right. - /// - public const byte CV_CHAINCODE_UP_RIGHT = 1; - - /// - /// Right. - /// - public const byte CV_CHAINCODE_RIGHT = 2; - - /// - /// Down and right. - /// - public const byte CV_CHAINCODE_DOWN_RIGHT = 3; - - /// - /// Down. - /// - public const byte CV_CHAINCODE_DOWN = 4; - - /// - /// Down and left. - /// - public const byte CV_CHAINCODE_DOWN_LEFT = 5; - - /// - /// Left. - /// - public const byte CV_CHAINCODE_LEFT = 6; - - /// - /// Up and left. - /// - public const byte CV_CHAINCODE_UP_LEFT = 7; -#pragma warning restore CA1707 - - /// - /// Move vectors of chain codes. - /// - public static readonly sbyte[][] ChainCodeMoves = - { - new sbyte[] {0, -1}, - new sbyte[] {1, -1}, - new sbyte[] {1, 0}, - new sbyte[] {1, 1}, - new sbyte[] {0, 1}, - new sbyte[] {-1, 1}, - new sbyte[] {-1, 0}, - new sbyte[] {-1, -1} - }; - - #endregion - - #region RenderTracksMode - -#pragma warning disable CA1707 - // ReSharper disable InconsistentNaming - /// - /// Print the ID of each track in the image. - /// - public const ushort CV_TRACK_RENDER_ID = 0x0001; - - /// - /// Draw bounding box of each track in the image. \see cvRenderTracks - /// - public const ushort CV_TRACK_RENDER_BOUNDING_BOX = 0x0002; - - /// - /// Print track info to log out. - /// - public const ushort CV_TRACK_RENDER_TO_LOG = 0x0010; - - /// - /// Print track info to log out. - /// - public const ushort CV_TRACK_RENDER_TO_STD = 0x0020; - - // ReSharper restore InconsistentNaming -#pragma warning restore CA1707 - - #endregion - } -} diff --git a/src/OpenCvSharp.Blob/CvBlobLib.cs b/src/OpenCvSharp.Blob/CvBlobLib.cs deleted file mode 100644 index 6edd38e46..000000000 --- a/src/OpenCvSharp.Blob/CvBlobLib.cs +++ /dev/null @@ -1,515 +0,0 @@ -using System; - -//#pragma warning disable 1591 - -namespace OpenCvSharp.Blob -{ - /// - /// Functions of cvblob library - /// - public static class CvBlobLib - { - /// - /// Calculates angle orientation of a blob. - /// This function uses central moments so cvCentralMoments should have been called before for this blob. (cvAngle) - /// - /// Blob. - /// Angle orientation in radians. - public static double CalcAngle(CvBlob blob) - { - if (blob == null) - throw new ArgumentNullException(nameof(blob)); - return blob.Angle(); - } - - /// - /// Calculates centroid. - /// Centroid will be returned and stored in the blob structure. (cvCentroid) - /// - /// Blob whose centroid will be calculated. - /// Centroid. - public static Point2d CalcCentroid(CvBlob blob) - { - if (blob == null) - throw new ArgumentNullException(nameof(blob)); - return blob.CalcCentroid(); - } - - /// - /// Calculates area of a polygonal contour. - /// - /// Contour (polygon type). - /// Area of the contour. - public static double ContourPolygonArea(CvContourPolygon polygon) - { - if (polygon == null) - throw new ArgumentNullException(nameof(polygon)); - return polygon.Area(); - } - - /// - /// Calculates the circularity of a polygon (compactness measure). - /// - /// Contour (polygon type). - /// Circularity: a non-negative value, where 0 correspond with a circumference. - public static double ContourPolygonCircularity(CvContourPolygon polygon) - { - if (polygon == null) - throw new ArgumentNullException(nameof(polygon)); - return polygon.Circularity(); - } - - /// - /// Calculates perimeter of a chain code contour. - /// - /// Contour (polygon type). - /// Perimeter of the contour. - public static double ContourPolygonPerimeter(CvContourPolygon polygon) - { - if (polygon == null) - throw new ArgumentNullException(nameof(polygon)); - return polygon.Perimeter(); - } - - /// - /// Calculates perimeter of a chain code contour. - /// - /// Contour (chain code type). - /// Perimeter of the contour. - public static double ContourChainCodePerimeter(CvContourChainCode cc) - { - if (cc == null) - throw new ArgumentNullException(nameof(cc)); - return cc.Perimeter(); - } - - /// - /// Convert a chain code contour to a polygon. - /// - /// Chain code contour. - /// A polygon. - public static CvContourPolygon ConvertChainCodesToPolygon(CvContourChainCode cc) - { - if (cc == null) - throw new ArgumentNullException(nameof(cc)); - return cc.ConvertToPolygon(); - } - - /// - /// Filter blobs by area. - /// Those blobs whose areas are not in range will be erased from the input list of blobs. (cvFilterByArea) - /// - /// List of blobs. - /// Minimum area. - /// Maximum area. - public static void FilterByArea(CvBlobs blobs, int minArea, int maxArea) - { - if (blobs == null) - throw new ArgumentNullException(nameof(blobs)); - blobs.FilterByArea(minArea, maxArea); - } - - /// - /// Filter blobs by label. - /// Delete all blobs except those with label l. - /// - /// List of blobs. - /// Label to leave. - public static void FilterByLabel(CvBlobs blobs, int label) - { - if (blobs == null) - throw new ArgumentNullException(nameof(blobs)); - blobs.FilterByLabel(label); - } - - /// - /// Draw a binary image with the blobs that have been given. (cvFilterLabels) - /// - /// List of blobs to be drawn. - /// Output binary image (depth=IPL_DEPTH_8U and nchannels=1). - public static void FilterLabels(CvBlobs blobs, Mat imgOut) - { - if (blobs == null) - throw new ArgumentNullException(nameof(blobs)); - blobs.FilterLabels(imgOut); - } - - /// - /// Get the label value from a labeled image. - /// - /// Blob data. - /// X coordenate. - /// Y coordenate. - /// Label value. - public static int GetLabel(CvBlobs blobs, int x, int y) - { - if (blobs == null) - throw new ArgumentNullException(nameof(blobs)); - return blobs.GetLabel(x, y); - } - - /// - /// Find greater blob. (cvGreaterBlob) - /// - /// List of blobs. - /// The greater blob. - public static CvBlob? GreaterBlob(CvBlobs blobs) - { - if (blobs == null) - throw new ArgumentNullException(nameof(blobs)); - return blobs.GreaterBlob(); - } - - /// - /// Find the largest blob. (cvLargestBlob) - /// - /// List of blobs. - /// The largest blob. - public static CvBlob? LargestBlob(CvBlobs blobs) - { - if (blobs == null) - throw new ArgumentNullException(nameof(blobs)); - return blobs.LargestBlob(); - } - - /// - /// Label the connected parts of a binary image. (cvLabel) - /// - /// Input binary image (depth=IPL_DEPTH_8U and num. channels=1). - /// List of blobs. - /// Number of pixels that has been labeled. - public static int Label(Mat img, CvBlobs blobs) - { - if (img == null) - throw new ArgumentNullException(nameof(img)); - if (blobs == null) - throw new ArgumentNullException(nameof(blobs)); - - return blobs.Label(img); - } - - /// - /// Calculates mean color of a blob in an image. - /// - /// Blob list - /// The target blob - /// Original image. - /// Average color. - public static Scalar BlobMeanColor(CvBlobs blobs, CvBlob targetBlob, Mat originalImage) - { - if (blobs == null) - throw new ArgumentNullException(nameof(blobs)); - return blobs.BlobMeanColor(targetBlob, originalImage); - } - - /// - /// Calculates convex hull of a contour. - /// Uses the Melkman Algorithm. Code based on the version in http://w3.impa.br/~rdcastan/Cgeometry/. - /// - /// Contour (polygon type). - /// Convex hull. - public static CvContourPolygon PolygonContourConvexHull(CvContourPolygon polygon) - { - if (polygon == null) - throw new ArgumentNullException(nameof(polygon)); - return polygon.ContourConvexHull(); - } - - /// - /// Draws or prints information about a blob. - /// - /// Label data. - /// Blob. - /// Input image (depth=IPL_DEPTH_8U and num. channels=3). - /// Output image (depth=IPL_DEPTH_8U and num. channels=3). - public static void RenderBlob(LabelData labels, CvBlob blob, Mat imgSource, Mat imgDest) - { - RenderBlob(labels, blob, imgSource, imgDest, (RenderBlobsModes) 0x000f, Scalar.White, 1.0); - } - - /// - /// Draws or prints information about a blob. - /// - /// Label data. - /// Blob. - /// Input image (depth=IPL_DEPTH_8U and num. channels=3). - /// Output image (depth=IPL_DEPTH_8U and num. channels=3). - /// Render mode. By default is CV_BLOB_RENDER_COLOR|CV_BLOB_RENDER_CENTROID|CV_BLOB_RENDER_BOUNDING_BOX|CV_BLOB_RENDER_ANGLE. - public static void RenderBlob(LabelData labels, CvBlob blob, Mat imgSource, Mat imgDest, - RenderBlobsModes mode) - { - RenderBlob(labels, blob, imgSource, imgDest, mode, Scalar.White, 1.0); - } - - /// - /// Draws or prints information about a blob. - /// - /// Label data. - /// Blob. - /// Input image (depth=IPL_DEPTH_8U and num. channels=3). - /// Output image (depth=IPL_DEPTH_8U and num. channels=3). - /// Render mode. By default is CV_BLOB_RENDER_COLOR|CV_BLOB_RENDER_CENTROID|CV_BLOB_RENDER_BOUNDING_BOX|CV_BLOB_RENDER_ANGLE. - /// Color to render (if CV_BLOB_RENDER_COLOR is used). - /// If mode CV_BLOB_RENDER_COLOR is used. 1.0 indicates opaque and 0.0 translucent (1.0 by default). - 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); - } - - /// - /// Draws or prints information about blobs. (cvRenderBlobs) - /// - /// List of blobs. - /// Input image (depth=IPL_DEPTH_8U and num. channels=3). - /// Output image (depth=IPL_DEPTH_8U and num. channels=3). - public static void RenderBlobs(CvBlobs blobs, Mat imgSource, Mat imgDest) - { - RenderBlobs(blobs, imgSource, imgDest, (RenderBlobsModes) 0x000f, 1.0); - } - - /// - /// Draws or prints information about blobs. (cvRenderBlobs) - /// - /// List of blobs. - /// Input image (depth=IPL_DEPTH_8U and num. channels=3). - /// Output image (depth=IPL_DEPTH_8U and num. channels=3). - /// Render mode. By default is CV_BLOB_RENDER_COLOR|CV_BLOB_RENDER_CENTROID|CV_BLOB_RENDER_BOUNDING_BOX|CV_BLOB_RENDER_ANGLE. - /// If mode CV_BLOB_RENDER_COLOR is used. 1.0 indicates opaque and 0.0 translucent (1.0 by default). - 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); - } - - /// - /// Draw a contour. - /// - /// Chain code contour. - /// Image to draw on. - public static void RenderContourChainCode(CvContourChainCode contour, Mat img) - { - if (contour == null) - throw new ArgumentNullException(nameof(contour)); - contour.Render(img); - } - - /// - /// Draw a contour. - /// - /// Chain code contour. - /// Image to draw on. - /// Color to draw (default, white). - public static void RenderContourChainCode(CvContourChainCode contour, Mat img, Scalar color) - { - if (contour == null) - throw new ArgumentNullException(nameof(contour)); - contour.Render(img, color); - } - - /// - /// Draw a polygon. - /// - /// Polygon contour. - /// Image to draw on. - public static void RenderContourPolygon(CvContourPolygon contour, Mat img) - { - if (contour == null) - throw new ArgumentNullException(nameof(contour)); - contour.Render(img); - } - - /// - /// Draw a polygon. - /// - /// Polygon contour. - /// Image to draw on. - /// Color to draw (default, white). - public static void RenderContourPolygon(CvContourPolygon contour, Mat img, Scalar color) - { - if (contour == null) - throw new ArgumentNullException(nameof(contour)); - contour.Render(img, color); - } - - /// - /// Prints tracks information. - /// - /// List of tracks. - /// Input image (depth=IPL_DEPTH_8U and num. channels=3). - /// Output image (depth=IPL_DEPTH_8U and num. channels=3). - public static void RenderTracks(CvTracks tracks, Mat imgSource, Mat imgDest) - { - if (tracks == null) - throw new ArgumentNullException(nameof(tracks)); - tracks.Render(imgSource, imgDest); - } - - /// - /// Prints tracks information. - /// - /// List of tracks. - /// Input image (depth=IPL_DEPTH_8U and num. channels=3). - /// Output image (depth=IPL_DEPTH_8U and num. channels=3). - /// Render mode. By default is CV_TRACK_RENDER_ID. - public static void RenderTracks(CvTracks tracks, Mat imgSource, Mat imgDest, RenderTracksModes mode) - { - if (tracks == null) - throw new ArgumentNullException(nameof(tracks)); - tracks.Render(imgSource, imgDest, mode); - } - - /// - /// Prints tracks information. - /// - /// List of tracks. - /// Input image (depth=IPL_DEPTH_8U and num. channels=3). - /// Output image (depth=IPL_DEPTH_8U and num. channels=3). - /// Render mode. By default is CV_TRACK_RENDER_ID. - /// - /// - /// - /// - public static void RenderTracks(CvTracks tracks, Mat imgSource, Mat imgDest, RenderTracksModes mode, - Scalar textColor, HersheyFonts fontFace = HersheyFonts.HersheySimplex, double fontScale = 1d, int thickness = 1) - { - if (tracks == null) - throw new ArgumentNullException(nameof(tracks)); - tracks.Render(imgSource, imgDest, mode, textColor, fontFace, fontScale, thickness); - } - - /// - /// Save the image of a blob to a file. - /// The function uses an image (that can be the original pre-processed image or a processed one, or even the result of cvRenderBlobs, for example) and a blob structure. - /// Then the function saves a copy of the part of the image where the blob is. - /// - /// Name of the file. - /// Image. - /// Blob. - public static void SaveImageBlob(string fileName, Mat img, CvBlob blob) - { - if (string.IsNullOrEmpty(fileName)) - throw new ArgumentNullException(nameof(fileName)); - if (blob == null) - throw new ArgumentNullException(nameof(blob)); - blob.SaveImage(fileName, img); - } - - /// - /// Simplify a polygon reducing the number of vertex according the distance "delta". - /// Uses a version of the Ramer-Douglas-Peucker algorithm (http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm). - /// - /// Contour (polygon type). - /// A simplify version of the original polygon. - public static CvContourPolygon SimplifyPolygon(CvContourPolygon polygon) - { - if (polygon == null) - throw new ArgumentNullException(nameof(polygon)); - return polygon.Simplify(); - } - - /// - /// Simplify a polygon reducing the number of vertex according the distance "delta". - /// Uses a version of the Ramer-Douglas-Peucker algorithm (http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm). - /// - /// Contour (polygon type). - /// Minimum distance. - /// A simplify version of the original polygon. - public static CvContourPolygon SimplifyPolygon(CvContourPolygon polygon, double delta) - { - if (polygon == null) - throw new ArgumentNullException(nameof(polygon)); - return polygon.Simplify(delta); - } - - /// - /// Updates list of tracks based on current blobs. - /// - /// List of blobs. - /// List of tracks. - /// Max distance to determine when a track and a blob match. - /// Max number of frames a track can be inactive. - public static void UpdateTracks(CvBlobs blobs, CvTracks tracks, double thDistance, int thInactive) - { - if (blobs == null) - throw new ArgumentNullException(nameof(blobs)); - blobs.UpdateTracks(tracks, thDistance, thInactive); - } - - /// - /// Updates list of tracks based on current blobs. - /// - /// List of blobs. - /// List of tracks. - /// Max distance to determine when a track and a blob match. - /// Max number of frames a track can be inactive. - /// If a track becomes inactive but it has been active less than thActive frames, the track will be deleted. - /// - /// Tracking based on: - /// A. Senior, A. Hampapur, Y-L Tian, L. Brown, S. Pankanti, R. Bolle. Appearance Models for - /// Occlusion Handling. Second International workshop on Performance Evaluation of Tracking and - /// Surveillance Systems & CVPR'01. December, 2001. - /// (http://www.research.ibm.com/peoplevision/PETS2001.pdf) - /// - public static void UpdateTracks(CvBlobs blobs, CvTracks tracks, double thDistance, int thInactive, int thActive) - { - if (blobs == null) - throw new ArgumentNullException(nameof(blobs)); - blobs.UpdateTracks(tracks, thDistance, thInactive, thActive); - } - - /// - /// Write a contour to a CSV (Comma-separated values) file. - /// - /// Polygon contour. - /// File name. - public static void WriteContourPolygonCsv(CvContourPolygon polygon, string filename) - { - if (polygon == null) - throw new ArgumentNullException(nameof(polygon)); - polygon.WriteAsCsv(filename); - } - - /// - /// Write a contour to a SVG file. - /// - /// Polygon contour. - /// File name. - public static void WriteContourPolygonSvg(CvContourPolygon polygon, string fileName) - { - if (polygon == null) - throw new ArgumentNullException(nameof(polygon)); - polygon.WriteAsSvg(fileName); - } - - /// - /// Write a contour to a SVG file. - /// - /// Polygon contour. - /// File name. - /// Stroke color (black by default). - /// Fill color (white by default). - public static void WriteContourPolygonSvg(CvContourPolygon polygon, string fileName, - Scalar stroke, Scalar fill) - { - if (polygon == null) - throw new ArgumentNullException(nameof(polygon)); - polygon.WriteAsSvg(fileName, stroke, fill); - } - } -} diff --git a/src/OpenCvSharp.Blob/CvBlobs.cs b/src/OpenCvSharp.Blob/CvBlobs.cs deleted file mode 100644 index 71beefc9a..000000000 --- a/src/OpenCvSharp.Blob/CvBlobs.cs +++ /dev/null @@ -1,721 +0,0 @@ -using System; -using System.Collections.Generic; - -// Copyright (C) 2007 by Cristóbal Carnero Liñán -// grendel.ccl@gmail.com -// -// This file is part of cvBlob. -// -// cvBlob is free software: you can redistribute it and/or modify -// it under the terms of the Lesser GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// cvBlob is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// Lesser GNU General Public License for more details. -// -// You should have received a copy of the Lesser GNU General Public License -// along with cvBlob. If not, see . - -namespace OpenCvSharp.Blob -{ - /// - /// Blob set - /// - [Serializable] -#pragma warning disable CA1710 // suffix -#pragma warning disable CA2229 // Implement serialization constructors - public class CvBlobs : Dictionary -#pragma warning restore CA2229 -#pragma warning restore CA1710 - { - /// - /// Label values - /// - public LabelData? Labels { get; set; } - - /// - /// Constructor (init only) - /// - public CvBlobs() - { - Labels = null; - } - - /// - /// Constructor (copy) - /// - public CvBlobs(IEnumerable> blobData, int[,] labelData) - { - if (blobData == null) - throw new ArgumentNullException(nameof(blobData)); - if (labelData == null) - throw new ArgumentNullException(nameof(labelData)); - - foreach (KeyValuePair pair in blobData) - { - Add(pair.Key, pair.Value); - } - Labels = new LabelData(labelData); - } - - /// - /// Constructor (copy) - /// - public CvBlobs(IEnumerable> blobData, LabelData labelData) - : this(blobData, labelData?.Values?.GetBuffer() ?? throw new ArgumentNullException(nameof(labelData))) - { - } - - /// - /// Constructor (init and cvLabel) - /// - /// Input binary image (depth=IPL_DEPTH_8U and nchannels=1). - public CvBlobs(Mat img) - { - if (img == null) - throw new ArgumentNullException(nameof(img)); - if (img.IsDisposed) - throw new ArgumentException("img is disposed"); - if (img.Type() != MatType.CV_8UC1) - throw new ArgumentException("img.Depth == BitDepth.U8 && img.NChannels == 1"); - - Label(img); - } - - #region Methods - - #region BlobMeanColor - - /// - /// Calculates mean color of a blob in an image. (cvBlobMeanColor) - /// - /// The target blob - /// Original image. - public Scalar BlobMeanColor(CvBlob targetBlob, Mat originalImage) - { - if (targetBlob == null) - throw new ArgumentNullException(nameof(targetBlob)); - if (originalImage == null) - throw new ArgumentNullException(nameof(originalImage)); - if (originalImage.Type() != MatType.CV_8UC3) - throw new ArgumentException("imgOut.Depth != BitDepth.U8 || imgOut.NChannels != 3"); - if (Labels == null) - throw new ArgumentException("blobs.Labels == null"); - - int width = originalImage.Cols; - int height = originalImage.Rows; - int mb = 0; - int mg = 0; - int mr = 0; - - var indexer = originalImage.GetGenericIndexer(); - for (int r = 0; r < height; r++) - { - for (int c = 0; c < width; c++) - { - if (Labels[r, c] == targetBlob.Label) - { - var v = indexer[r, c]; - mb += v.Item0; - mg += v.Item1; - mr += v.Item2; - } - } - } - - GC.KeepAlive(originalImage); - - int pixels = targetBlob.Area; - return new Scalar((byte) (mb/pixels), (byte) (mg/pixels), (byte) (mr/pixels)); - } - - #endregion - - #region FilterByArea - - /// - /// Filter blobs by area. - /// Those blobs whose areas are not in range will be erased from the input list of blobs. (cvFilterByArea) - /// - /// Minimun area. - /// Maximun area. - public void FilterByArea(int minArea, int maxArea) - { - int[] keys = new int[Count]; - Keys.CopyTo(keys, 0); - - foreach (int key in keys) - { - int area = this[key].Area; - if (area < minArea || area > maxArea) - { - Remove(key); - } - } - } - - #endregion - - #region FilterByLabel - - /// - /// Filter blobs by label. - /// Delete all blobs except those with label l. - /// - /// Label to leave. - public void FilterByLabel(int label) - { - int[] keys = new int[Count]; - Keys.CopyTo(keys, 0); - - foreach (int key in keys) - { - if (this[key].Label != label) - { - Remove(key); - } - } - } - - #endregion - - #region FilterLabels - - /// - /// Draw a binary image with the blobs that have been given. (cvFilterLabels) - /// - /// Output binary image (depth=IPL_DEPTH_8U and nchannels=1). - public void FilterLabels(Mat imgOut) - { - if (imgOut == null) - throw new ArgumentNullException(nameof(imgOut)); - if (imgOut.Type() != MatType.CV_8UC1) - throw new ArgumentException("imgOut.Depth != BitDepth.U8 || imgOut.NChannels != 1"); - if (Labels == null) - throw new ArgumentException("blobs.Labels == null"); - - Size size = Labels.Size; - int w = size.Width; - int h = size.Height; - var indexer = imgOut.GetGenericIndexer(); - - for (int r = 0; r < h; r++) - { - for (int c = 0; c < w; c++) - { - int label = Labels[r, c]; - if (label != 0) - { - if (ContainsKey(label)) - indexer[r, c] = 0xff; - else - indexer[r, c] = 0x00; - } - else - { - indexer[r, c] = 0x00; - } - } - } - } - - #endregion - - #region GreaterBlob - - /// - /// Find greater blob. (cvGreaterBlob) - /// - /// The greater blob. - public CvBlob? GreaterBlob() - { - return LargestBlob(); - } - - /// - /// Find the largest blob. (cvGreaterBlob) - /// - /// The largest blob. - public CvBlob? LargestBlob() - { - if (Count == 0) - return null; - - var list = new List>(this); - // 降順ソート - list.Sort((kv1, kv2) => - { - CvBlob b1 = kv1.Value; - CvBlob b2 = kv2.Value; - if (b1 == null) - return -1; - if (b2 == null) - return 1; - return b2.Area - b1.Area; - }); - return list[0].Value; - } - - #endregion - - #region GetLabel - - /// - /// Label the connected parts of a binary image. (cvLabel) - /// - /// - /// - /// Number of pixels that has been labeled. - public int GetLabel(int x, int y) - { - if (Labels == null) - throw new NotSupportedException("Label() not called"); - return Labels[y, x]; - } - - #endregion - - #region Label - - /// - /// Label the connected parts of a binary image. (cvLabel) - /// - /// Input binary image (depth=IPL_DEPTH_8U and num. channels=1). - /// Number of pixels that has been labeled. - public int Label(Mat img) - { - if (img == null) - throw new ArgumentNullException(nameof(img)); - - Labels = new LabelData(img.Height, img.Width); - return Labeller.Perform(img, this); - } - - #endregion - - #region RenderBlobs - - /// - /// Draws or prints information about blobs. (cvRenderBlobs) - /// - /// Input image (depth=IPL_DEPTH_8U and num. channels=3). - /// Output image (depth=IPL_DEPTH_8U and num. channels=3). - public void RenderBlobs(Mat imgSource, Mat imgDest) - { - CvBlobLib.RenderBlobs(this, imgSource, imgDest); - } - - /// - /// Draws or prints information about blobs. (cvRenderBlobs) - /// - /// Input image (depth=IPL_DEPTH_8U and num. channels=3). - /// Output image (depth=IPL_DEPTH_8U and num. channels=3). - /// Render mode. By default is CV_BLOB_RENDER_COLOR|CV_BLOB_RENDER_CENTROID|CV_BLOB_RENDER_BOUNDING_BOX|CV_BLOB_RENDER_ANGLE. - public void RenderBlobs(Mat imgSource, Mat imgDest, RenderBlobsModes mode) - { - CvBlobLib.RenderBlobs(this, imgSource, imgDest, mode); - } - - /// - /// Draws or prints information about blobs. (cvRenderBlobs) - /// - /// Input image (depth=IPL_DEPTH_8U and num. channels=3). - /// Output image (depth=IPL_DEPTH_8U and num. channels=3). - /// Render mode. By default is CV_BLOB_RENDER_COLOR|CV_BLOB_RENDER_CENTROID|CV_BLOB_RENDER_BOUNDING_BOX|CV_BLOB_RENDER_ANGLE. - /// If mode CV_BLOB_RENDER_COLOR is used. 1.0 indicates opaque and 0.0 translucent (1.0 by default). - public void RenderBlobs(Mat imgSource, Mat imgDest, RenderBlobsModes mode, double alpha) - { - CvBlobLib.RenderBlobs(this, imgSource, imgDest, mode, alpha); - } - - #endregion - - #region UpdateTracks - - /// - /// Updates list of tracks based on current blobs. - /// - /// List of tracks. - /// Max distance to determine when a track and a blob match. - /// Max number of frames a track can be inactive. - /// - /// Tracking based on: - /// A. Senior, A. Hampapur, Y-L Tian, L. Brown, S. Pankanti, R. Bolle. Appearance Models for - /// Occlusion Handling. Second International workshop on Performance Evaluation of Tracking and - /// Surveillance Systems & CVPR'01. December, 2001. - /// (http://www.research.ibm.com/peoplevision/PETS2001.pdf) - /// - public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive) - { - UpdateTracks(tracks, thDistance, thInactive, 0); - } - - /// - /// Updates list of tracks based on current blobs. - /// - /// List of tracks. - /// Max distance to determine when a track and a blob match. - /// Max number of frames a track can be inactive. - /// If a track becomes inactive but it has been active less than thActive frames, the track will be deleted. - /// - /// Tracking based on: - /// A. Senior, A. Hampapur, Y-L Tian, L. Brown, S. Pankanti, R. Bolle. Appearance Models for - /// Occlusion Handling. Second International workshop on Performance Evaluation of Tracking and - /// Surveillance Systems & CVPR'01. December, 2001. - /// (http://www.research.ibm.com/peoplevision/PETS2001.pdf) - /// - public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int thActive) - { - if (tracks == null) - throw new ArgumentNullException(nameof(tracks)); - - int nBlobs = this.Count; - int nTracks = tracks.Count; - - if (nBlobs == 0) - return; - - // Proximity matrix: - // Last row/column is for ID/label. - // Last-1 "/" is for accumulation. - var close = new ProximityMatrix(nBlobs, nTracks); - - // Initialization: - int i = 0; - foreach (CvBlob blob in Values) - { - close.AB[i] = 0; - close.IB[i] = blob.Label; - i++; - } - - int maxTrackId = 0; - int j = 0; - foreach (CvTrack track in tracks.Values) - { - close.AT[j] = 0; - close.IT[j] = track.Id; - if (track.Id > maxTrackId) - maxTrackId = track.Id; - j++; - } - - // Proximity matrix calculation and "used blob" list initialization: - for (i = 0; i < nBlobs; i++) - { - for (j = 0; j < nTracks; j++) - { - int ib = close.IB[i]; - int it = close.IT[j]; - CvBlob b = this[ib]; - CvTrack t = tracks[it]; - close[i, j] = (DistantBlobTrack(b, t) < thDistance) ? 1 : 0; - if (close[i, j] < thDistance) - { - close.AB[i]++; - close.AT[j]++; - } - } - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // Detect inactive tracks - for (j = 0; j < nTracks; j++) - { - int c = close[nBlobs, j]; - if (c == 0) - { - //cout << "Inactive track: " << j << endl; - - // Inactive track. - CvTrack track = tracks[j]; - track.Inactive++; - track.Label = 0; - } - } - - // Detect new tracks - for (i = 0; i < nBlobs; i++) - { - int c = close.AB[i]; - if (c == 0) - { - //cout << "Blob (new track): " << maxTrackID+1 << endl; - //cout << *B(i) << endl; - - // New track. - maxTrackId++; - int ib = close.IB[i]; - CvBlob blob = this[ib]; - CvTrack track = new CvTrack - { - Id = maxTrackId, - Label = blob.Label, - MinX = blob.MinX, - MinY = blob.MinY, - MaxX = blob.MaxX, - MaxY = blob.MaxY, - Centroid = blob.Centroid, - LifeTime = 0, - Active = 0, - Inactive = 0, - }; - tracks[maxTrackId] = track; - //maxTrackId++; - } - } - - // Clustering - for (j = 0; j < nTracks; j++) - { - int c = close.AT[j]; - if (c != 0) - { - var tt = new List(); - var bb = new List(); - - int it = close.IT[j]; - tt.Add(tracks[it]); - - GetClusterForTrack(j, close, nBlobs, nTracks, this, tracks, bb, tt); - - // Select track - CvTrack? track = null; - int area = 0; - foreach (CvTrack t in tt) - { - int a = (t.MaxX - t.MinX)*(t.MaxY - t.MinY); - if (a > area) - { - area = a; - track = t; - } - } - - // Select blob - CvBlob? blob = null; - area = 0; - foreach (CvBlob b in Values) - { - if (b.Area > area) - { - area = b.Area; - blob = b; - } - } - - if (blob == null || track == null) - //throw new Exception(); - continue; - - // Update track - track.Label = blob.Label; - track.Centroid = blob.Centroid; - track.MinX = blob.MinX; - track.MinY = blob.MinY; - track.MaxX = blob.MaxX; - track.MaxY = blob.MaxY; - if (track.Inactive != 0) - track.Active = 0; - track.Inactive = 0; - - // Others to inactive - foreach (CvTrack t in tt) - { - if (t != track) - { - //cout << "Inactive: track=" << t->id << endl; - t.Inactive++; - t.Label = 0; - } - } - } - } - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - int[] trackKeys = new int[tracks.Count]; - tracks.Keys.CopyTo(trackKeys, 0); - foreach (int tkey in trackKeys) - { - CvTrack t = tracks[tkey]; - if ((t.Inactive >= thInactive) || - ((t.Inactive != 0) && (thActive != 0) && (t.Active < thActive))) - { - tracks.Remove(tkey); - } - else - { - t.LifeTime++; - if (t.Inactive == 0) - t.Active++; - } - } - } - - private static double DistantBlobTrack(CvBlob b, CvTrack t) - { - double d1; - if (b.Centroid.X < t.MinX) - { - if (b.Centroid.Y < t.MinY) - d1 = Math.Max(t.MinX - b.Centroid.X, t.MinY - b.Centroid.Y); - else if (b.Centroid.Y > t.MaxY) - d1 = Math.Max(t.MinX - b.Centroid.X, b.Centroid.Y - t.MaxY); - else // if (t.MinY < b.Centroid.Y)&&(b.Centroid.Y < t.MaxY) - d1 = t.MinX - b.Centroid.X; - } - else if (b.Centroid.X > t.MaxX) - { - if (b.Centroid.Y < t.MinY) - d1 = Math.Max(b.Centroid.X - t.MaxX, t.MinY - b.Centroid.Y); - else if (b.Centroid.Y > t.MaxY) - d1 = Math.Max(b.Centroid.X - t.MaxX, b.Centroid.Y - t.MaxY); - else - d1 = b.Centroid.X - t.MaxX; - } - else // if (t.MinX =< b.Centroid.X) && (b.Centroid.X =< t.MaxX) - { - if (b.Centroid.Y < t.MinY) - d1 = t.MinY - b.Centroid.Y; - else if (b.Centroid.Y > t.MaxY) - d1 = b.Centroid.Y - t.MaxY; - else - return 0.0; - } - - double d2; - if (t.Centroid.X < b.MinX) - { - if (t.Centroid.Y < b.MinY) - d2 = Math.Max(b.MinX - t.Centroid.X, b.MinY - t.Centroid.Y); - else if (t.Centroid.Y > b.MaxY) - d2 = Math.Max(b.MinX - t.Centroid.X, t.Centroid.Y - b.MaxY); - else // if (b.MinY < t.Centroid.Y)&&(t.Centroid.Y < b.MaxY) - d2 = b.MinX - t.Centroid.X; - } - else if (t.Centroid.X > b.MaxX) - { - if (t.Centroid.Y < b.MinY) - d2 = Math.Max(t.Centroid.X - b.MaxX, b.MinY - t.Centroid.Y); - else if (t.Centroid.Y > b.MaxY) - d2 = Math.Max(t.Centroid.X - b.MaxX, t.Centroid.Y - b.MaxY); - else - d2 = t.Centroid.X - b.MaxX; - } - else // if (b.MinX =< t.Centroid.X) && (t.Centroid.X =< b.MaxX) - { - if (t.Centroid.Y < b.MinY) - d2 = b.MinY - t.Centroid.Y; - else if (t.Centroid.Y > b.MaxY) - d2 = t.Centroid.Y - b.MaxY; - else - return 0.0; - } - - return Math.Min(d1, d2); - } - - private static void GetClusterForBlob(int blobPos, ProximityMatrix close, - int nBlobs, int nTracks, CvBlobs blobs, CvTracks tracks, - List bb, List tt) - { - retry: - var retryList = new List(); - - for (int j = 0; j < nTracks; j++) - { - if (close[blobPos, j] != 0) - { - int it = close.IT[j]; - tt.Add(tracks[it]); - - int c = close.AT[j]; - - close[blobPos, j] = 0; - close.AB[blobPos]--; - close.AT[j]--; - - if (c > 1) - { - retryList.Add(j); - //GetClusterForTrack(j, close, nBlobs, nTracks, blobs, tracks, bb, tt); - } - } - } - - if (retryList.Count > 0) - { - foreach (int j in retryList) - { - GetClusterForTrack(j, close, nBlobs, nTracks, blobs, tracks, bb, tt); - } - goto retry; - } - } - - private static void GetClusterForTrack(int trackPos, ProximityMatrix close, - int nBlobs, int nTracks, CvBlobs blobs, - CvTracks tracks, List bb, List tt) - { - retry: - var retryList = new List(); - - for (int i = 0; i < nBlobs; i++) - { - if (close[i, trackPos] != 0) - { - int ib = close.IB[i]; - bb.Add(blobs[ib]); - - int c = close.AB[i]; - - close[i, trackPos] = 0; - close.AB[i]--; - close.AT[trackPos]--; - - if (c > 1) - { - retryList.Add(i); - //GetClusterForBlob(i, close, nBlobs, nTracks, blobs, tracks, bb, tt); - } - } - } - - if (retryList.Count > 0) - { - foreach (int i in retryList) - { - GetClusterForBlob(i, close, nBlobs, nTracks, blobs, tracks, bb, tt); - } - goto retry; - } - } - - #endregion - - /// - /// - /// - /// - public CvBlobs Clone() - { - var newBlobs = new CvBlobs(); - - if (Labels == null) - return newBlobs; - - foreach (KeyValuePair pair in this) - { - newBlobs.Add(pair.Key, pair.Value); - } - newBlobs.Labels = Labels.Clone(); - - return newBlobs; - } - - #endregion - } -} diff --git a/src/OpenCvSharp.Blob/CvChainCode.cs b/src/OpenCvSharp.Blob/CvChainCode.cs deleted file mode 100644 index 1d11b1297..000000000 --- a/src/OpenCvSharp.Blob/CvChainCode.cs +++ /dev/null @@ -1,44 +0,0 @@ - -#pragma warning disable 1591 - -namespace OpenCvSharp.Blob -{ - /// - /// Chain code (direction) - /// - public enum CvChainCode - { - /// - /// Up. - /// - Up = CvBlobConst.CV_CHAINCODE_UP, - /// - /// Up and right. - /// - UpRight = CvBlobConst.CV_CHAINCODE_UP_RIGHT, - /// - /// Right. - /// - Right = CvBlobConst.CV_CHAINCODE_RIGHT, - /// - /// Down and right. - /// - DownRight = CvBlobConst.CV_CHAINCODE_DOWN_RIGHT, - /// - /// Down. - /// - Down = CvBlobConst.CV_CHAINCODE_DOWN, - /// - /// Down and left. - /// - DownLeft = CvBlobConst.CV_CHAINCODE_DOWN_LEFT, - /// - /// Left. - /// - Left = CvBlobConst.CV_CHAINCODE_LEFT, - /// - /// Up and left. - /// - UpLeft = CvBlobConst.CV_CHAINCODE_UP_LEFT, - } -} \ No newline at end of file diff --git a/src/OpenCvSharp.Blob/CvContourChainCode.cs b/src/OpenCvSharp.Blob/CvContourChainCode.cs deleted file mode 100644 index 392f7685d..000000000 --- a/src/OpenCvSharp.Blob/CvContourChainCode.cs +++ /dev/null @@ -1,144 +0,0 @@ -// Copyright (C) 2007 by Cristóbal Carnero Liñán -// grendel.ccl@gmail.com -// -// This file is part of cvBlob. -// -// cvBlob is free software: you can redistribute it and/or modify -// it under the terms of the Lesser GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// cvBlob is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// Lesser GNU General Public License for more details. -// -// You should have received a copy of the Lesser GNU General Public License -// along with cvBlob. If not, see . - -using System; -using System.Collections.Generic; - -namespace OpenCvSharp.Blob -{ - /// - /// - /// - [Serializable] - public class CvContourChainCode - { - /// - /// Point where contour begin. - /// - public Point StartingPoint { get; set; } - - /// - /// Polygon description based on chain codes. - /// - public List ChainCode { get; set; } - - /// - /// - /// - public CvContourChainCode() - { - StartingPoint = default; - ChainCode = new List(); - } - - /// - /// Convert a chain code contour to a polygon. - /// - /// A polygon. - public CvContourPolygon ConvertToPolygon() - { - CvContourPolygon contour = new CvContourPolygon(); - - int x = StartingPoint.X; - int y = StartingPoint.Y; - contour.Add(new Point(x, y)); - - if (ChainCode.Count > 0) - { - CvChainCode lastCode = ChainCode[0]; - x += CvBlobConst.ChainCodeMoves[(int) ChainCode[0]][0]; - y += CvBlobConst.ChainCodeMoves[(int) ChainCode[0]][1]; - for (int i = 1; i < ChainCode.Count; i++) - { - if (lastCode != ChainCode[i]) - { - contour.Add(new Point(x, y)); - lastCode = ChainCode[i]; - } - x += CvBlobConst.ChainCodeMoves[(int) ChainCode[i]][0]; - y += CvBlobConst.ChainCodeMoves[(int) ChainCode[i]][1]; - } - } - - return contour; - } - - /// - /// Calculates perimeter of a polygonal contour. - /// - /// Perimeter of the contour. - public double Perimeter() - { - double perimeter = 0.0; - foreach (CvChainCode cc in ChainCode) - { - int type = (int) cc; - if (type%2 != 0) - perimeter += Math.Sqrt(1.0 + 1.0); - else - perimeter += 1.0; - } - return perimeter; - } - - /// - /// Draw a contour. - /// - /// Image to draw on. - public void Render(Mat img) - { - Render(img, new Scalar(255, 255, 255)); - } - - /// - /// Draw a contour. - /// - /// Image to draw on. - /// Color to draw (default, white). - public void Render(Mat img, Scalar color) - { - if (img == null) - throw new ArgumentNullException(nameof(img)); - if (img.Type() != MatType.CV_8UC3) - throw new ArgumentException("Invalid img format (U8 3-channels)"); - - var indexer = img.GetGenericIndexer(); - int x = StartingPoint.X; - int y = StartingPoint.Y; - foreach (CvChainCode cc in ChainCode) - { - indexer[y, x] = new Vec3b((byte) color.Val0, (byte) color.Val1, (byte) color.Val2); - x += CvBlobConst.ChainCodeMoves[(int) cc][0]; - y += CvBlobConst.ChainCodeMoves[(int) cc][1]; - } - } - - /// - /// - /// - /// - public CvContourChainCode Clone() - { - return new CvContourChainCode - { - ChainCode = new List(ChainCode), - StartingPoint = StartingPoint, - }; - } - } -} diff --git a/src/OpenCvSharp.Blob/CvContourPolygon.cs b/src/OpenCvSharp.Blob/CvContourPolygon.cs deleted file mode 100644 index 0bdb6d720..000000000 --- a/src/OpenCvSharp.Blob/CvContourPolygon.cs +++ /dev/null @@ -1,443 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Text; - -// Copyright (C) 2007 by Cristóbal Carnero Liñán -// grendel.ccl@gmail.com -// -// This file is part of cvBlob. -// -// cvBlob is free software: you can redistribute it and/or modify -// it under the terms of the Lesser GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// cvBlob is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// Lesser GNU General Public License for more details. -// -// You should have received a copy of the Lesser GNU General Public License -// along with cvBlob. If not, see . - -namespace OpenCvSharp.Blob -{ - /// - /// Polygon based contour. - /// -#pragma warning disable CA1710 // suffix - public class CvContourPolygon : List -#pragma warning restore CA1710 - { - /// - /// - /// - public CvContourPolygon() - { - } - - /// - /// - /// - /// - public CvContourPolygon(IEnumerable list) - : base(list) - { - } - - /// - /// Converts this to CSV string - /// - /// - public override string ToString() - { - var sb = new StringBuilder(); - foreach (var p in this) - { - sb.AppendFormat(CultureInfo.InvariantCulture, "{0},{1}", p.X, p.Y).AppendLine(); - } - return sb.ToString(); - } - - #region Area - - /// - /// Calculates area of a polygonal contour. - /// - /// Area of the contour. - public double Area() - { - if (Count <= 2) - return 1.0; - - Point lastPoint = this[Count - 1]; - double area = 0.0; - foreach (Point point in this) - { - area += lastPoint.X*point.Y - lastPoint.Y*point.X; - lastPoint = point; - } - return area*0.5; - } - - #endregion - - #region Circularity - - /// - /// Calculates the circularity of a polygon (compactness measure). - /// - /// Circularity: a non-negative value, where 0 correspond with a circumference. - public double Circularity() - { - double l = Perimeter(); - double c = (l*l/Area()) - 4.0*Math.PI; - - if (c >= 0.0) - return c; - // This could happen if the blob it's only a pixel: the perimeter will be 0. Another solution would be to force "cvContourPolygonPerimeter" to be 1 or greater. - return 0.0; - } - - #endregion - - #region ContourConvexHull - - /// - /// Calculates convex hull of a contour. - /// Uses the Melkman Algorithm. Code based on the version in http://w3.impa.br/~rdcastan/Cgeometry/. - /// - /// Convex hull. - public CvContourPolygon ContourConvexHull() - { - if (Count <= 3) - return new CvContourPolygon(this); - - var dq = new List(); // instead of std::deq... - - if (CrossProductPoints(this[0], this[1], this[2]) > 0) - { - dq.Add(this[0]); - dq.Add(this[1]); - } - else - { - dq.Add(this[1]); - dq.Add(this[0]); - } - - dq.Add(this[2]); - dq.Insert(0, this[2]); - - for (int i = 3; i < Count; i++) - { - int s = dq.Count; - - if ((CrossProductPoints(this[i], dq[0], dq[1]) >= 0) && - (CrossProductPoints(dq[s - 2], dq[s - 1], this[i]) >= 0)) - continue; // TODO Optimize. - - while (CrossProductPoints(dq[s - 2], dq[s - 1], this[i]) < 0) - { - dq.RemoveAt(dq.Count - 1); - s = dq.Count; - } - - dq.Add(this[i]); - - while (CrossProductPoints(this[i], dq[0], dq[1]) < 0) - dq.RemoveAt(0); - - dq.Insert(0, this[i]); - } - - return new CvContourPolygon(dq); - } - - private static double CrossProductPoints(Point a, Point b, Point c) - { - double abx = b.X - a.X; - double aby = b.Y - a.Y; - double acx = c.X - a.X; - double acy = c.Y - a.Y; - return abx*acy - aby*acx; - } - - #endregion - - #region ContourPolygonPerimeter - - /// - /// Calculates perimeter of a chain code contour. - /// - /// Perimeter of the contour. - public double Perimeter() - { - double perimeter = DistancePointPoint(this[Count - 1], this[0]); - for (int i = 0; i < Count - 1; i++) - { - perimeter += DistancePointPoint(this[i], this[i + 1]); - } - return perimeter; - } - - private static double DistancePointPoint(Point a, Point b) - { - double abx = a.X - b.X; - double aby = a.Y - b.Y; - return Math.Sqrt(abx*abx + aby*aby); - } - - #endregion - - #region Render - - /// - /// Draw a polygon. - /// - /// Image to draw on. - public void Render(Mat img) - { - Render(img, new Scalar(255, 255, 255)); - } - - /// - /// Draw a polygon. - /// - /// Image to draw on. - /// Color to draw (default, white). - public void Render(Mat img, Scalar color) - { - if (img == null) - throw new ArgumentNullException(nameof(img)); - if (img.Type() != MatType.CV_8UC3) - throw new ArgumentException("Invalid img format (U8 3-channels)"); - - if (Count == 0) - return; - - int fx = this[Count - 1].X; - int fy = this[Count - 1].Y; - foreach (Point p in this) - { - Cv2.Line(img, fx, fy, p.X, p.Y, color); - fx = p.X; - fy = p.Y; - } - } - - #endregion - - #region SimplifyPolygon - - /// - /// Simplify a polygon reducing the number of vertex according the distance "delta". - /// Uses a version of the Ramer-Douglas-Peucker algorithm (http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm). - /// - /// A simplify version of the original polygon. - public CvContourPolygon Simplify() - { - return Simplify(1.0); - } - - /// - /// Simplify a polygon reducing the number of vertex according the distance "delta". - /// Uses a version of the Ramer-Douglas-Peucker algorithm (http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm). - /// - /// Minimun distance. - /// A simplify version of the original polygon. - public CvContourPolygon Simplify(double delta) - { - double furtherDistance = 0.0; - int furtherIndex = 0; - - if (Count == 0) - return new CvContourPolygon(); - - for (int i = 1; i < Count; i++) - { - double d = DistancePointPoint(this[i], this[0]); - if (d > furtherDistance) - { - furtherDistance = d; - furtherIndex = i; - } - } - - if (furtherDistance < delta) - { - CvContourPolygon result = new CvContourPolygon(); - result.Add(this[0]); - return result; - } - else - { - bool[] pnUseFlag = new bool[Count]; - for (int i = 1; i < Count; i++) - pnUseFlag[i] = false; - - pnUseFlag[0] = pnUseFlag[furtherIndex] = true; - - SimplifyPolygonRecursive(this, 0, furtherIndex, pnUseFlag, delta); - SimplifyPolygonRecursive(this, furtherIndex, -1, pnUseFlag, delta); - - CvContourPolygon result = new CvContourPolygon(); - - for (int i = 0; i < Count; i++) - { - if (pnUseFlag[i]) - result.Add(this[i]); - } - return result; - } - } - - private static void SimplifyPolygonRecursive(CvContourPolygon p, int i1, int i2, bool[] pnUseFlag, double delta) - { - int endIndex = (i2 < 0) ? p.Count : i2; - - if (Math.Abs(i1 - endIndex) <= 1) - return; - - Point firstPoint = p[i1]; - Point lastPoint = (i2 < 0) ? p[0] : p[i2]; - - double furtherDistance = 0.0; - int furtherIndex = 0; - - for (int i = i1 + 1; i < endIndex; i++) - { - double d = DistanceLinePoint(firstPoint, lastPoint, p[i]); - - if ((d >= delta) && (d > furtherDistance)) - { - furtherDistance = d; - furtherIndex = i; - } - } - - if (furtherIndex > 0) - { - pnUseFlag[furtherIndex] = true; - - SimplifyPolygonRecursive(p, i1, furtherIndex, pnUseFlag, delta); - SimplifyPolygonRecursive(p, furtherIndex, i2, pnUseFlag, delta); - } - } - - private static double DistanceLinePoint(Point a, Point b, Point c, bool isSegment = true) - { - if (isSegment) - { - double dot1 = DotProductPoints(a, b, c); - if (dot1 > 0) return DistancePointPoint(b, c); - - double dot2 = DotProductPoints(b, a, c); - if (dot2 > 0) return DistancePointPoint(a, c); - } - return Math.Abs(CrossProductPoints(a, b, c)/DistancePointPoint(a, b)); - } - - private static double DotProductPoints(Point a, Point b, Point c) - { - double abx = b.X - a.X; - double aby = b.Y - a.Y; - double bcx = c.X - b.X; - double bcy = c.Y - b.Y; - return abx*bcx + aby*bcy; - } - - #endregion - - #region WriteAsCsv - - /// - /// Write a contour to a CSV (Comma-separated values) file. - /// - /// File name. - public void WriteAsCsv(string fileName) - { - using (var stream = new FileStream(fileName, FileMode.Create)) - using (var writer = new StreamWriter(stream)) - { - writer.Write(ToString()); - } - } - -#endregion - -#region WriteAsSvg - - /// - /// Write a contour to a SVG file. - /// - /// File name - public void WriteAsSvg(string fileName) - { - WriteAsSvg(fileName, Scalar.Black, Scalar.White); - } - - /// - /// Write a contour to a SVG file. - /// - /// File name - /// Stroke color - /// Fill color - public void WriteAsSvg(string fileName, Scalar stroke, Scalar fill) - { - using (var stream = new FileStream(fileName, FileMode.Create)) - using (var writer = new StreamWriter(stream)) - { - writer.WriteLine(ToSvg(stroke, fill)); - } - } - - /// - /// - /// - /// - /// - /// - public string ToSvg(Scalar stroke, Scalar fill) - { - int minx = int.MaxValue; - int miny = int.MaxValue; - int maxx = int.MinValue; - int maxy = int.MinValue; - - var buffer = new StringBuilder(); - foreach (var p in this) - { - if (p.X > maxx) - maxx = p.X; - if (p.X < minx) - minx = p.X; - if (p.Y > maxy) - maxy = p.Y; - if (p.Y < miny) - miny = p.Y; - buffer.AppendFormat(CultureInfo.InvariantCulture, "{0},{1} ", p.X, p.Y); - } - - var builder = new StringBuilder() - .AppendLine("") - .AppendLine( - "") - .AppendFormat( - CultureInfo.InvariantCulture, - "", - maxx - minx, maxy - miny, minx, miny, maxx, maxy).AppendLine() - .AppendFormat( - CultureInfo.InvariantCulture, - "", - fill.Val0, fill.Val1, fill.Val2, stroke.Val0, stroke.Val1, stroke.Val2, buffer).AppendLine() - .AppendLine(""); - return builder.ToString(); - } - -#endregion - } -} diff --git a/src/OpenCvSharp.Blob/CvTrack.cs b/src/OpenCvSharp.Blob/CvTrack.cs deleted file mode 100644 index 3beb22fdf..000000000 --- a/src/OpenCvSharp.Blob/CvTrack.cs +++ /dev/null @@ -1,73 +0,0 @@ - - -// Copyright (C) 2007 by Cristóbal Carnero Liñán -// grendel.ccl@gmail.com -// -// This file is part of cvBlob. -// -// cvBlob is free software: you can redistribute it and/or modify -// it under the terms of the Lesser GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// cvBlob is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// Lesser GNU General Public License for more details. -// -// You should have received a copy of the Lesser GNU General Public License -// along with cvBlob. If not, see . - -namespace OpenCvSharp.Blob -{ - /// - /// Struct that contain information about one track. - /// - public class CvTrack - { - /// - /// Track identification number. - /// - public int Id { get; set; } - - /// - /// Label assigned to the blob related to this track. - /// - public int Label { get; set; } - - /// - /// X min. - /// - public int MinX { get; set; } - /// - /// X max. - /// - public int MaxX { get; set; } - /// - /// Y min. - /// - public int MinY { get; set; } - /// - /// Y max. - /// - public int MaxY { get; set; } - - /// - /// Centroid. - /// - public Point2d Centroid { get; set; } - - /// - /// Indicates how much frames the object has been in scene. - /// - public int LifeTime { get; set; } - /// - /// Indicates number of frames that has been active from last inactive period. - /// - public int Active { get; set; } - /// - /// Indicates number of frames that has been missing. - /// - public int Inactive { get; set; } - } -} diff --git a/src/OpenCvSharp.Blob/CvTracks.cs b/src/OpenCvSharp.Blob/CvTracks.cs deleted file mode 100644 index f897573eb..000000000 --- a/src/OpenCvSharp.Blob/CvTracks.cs +++ /dev/null @@ -1,150 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Text; - -// Copyright (C) 2007 by Cristóbal Carnero Liñán -// grendel.ccl@gmail.com -// -// This file is part of cvBlob. -// -// cvBlob is free software: you can redistribute it and/or modify -// it under the terms of the Lesser GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// cvBlob is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// Lesser GNU General Public License for more details. -// -// You should have received a copy of the Lesser GNU General Public License -// along with cvBlob. If not, see . - -namespace OpenCvSharp.Blob -{ - /// - /// - [Serializable] -#pragma warning disable CA1710 // suffix -#pragma warning disable CA2229 // Implement serialization constructors - public class CvTracks : Dictionary -#pragma warning restore CA2229 -#pragma warning restore CA1710 - { - /// - /// - /// - public CvTracks() - { - } - - #region Render - - /// - /// Prints tracks information. - /// - /// Input image (depth=IPL_DEPTH_8U and num. channels=3). - /// Output image (depth=IPL_DEPTH_8U and num. channels=3). - public void Render(Mat imgSource, Mat imgDest) - { - Render(imgSource, imgDest, RenderTracksModes.Id, Scalar.Green); - } - - /// - /// Prints tracks information. - /// - /// Input image (depth=IPL_DEPTH_8U and num. channels=3). - /// Output image (depth=IPL_DEPTH_8U and num. channels=3). - /// Render mode. By default is CV_TRACK_RENDER_ID. - public void Render(Mat imgSource, Mat imgDest, RenderTracksModes mode) - { - Render(imgSource, imgDest, mode, Scalar.Green); - } - - /// - /// Prints tracks information. - /// - /// Input image (depth=IPL_DEPTH_8U and num. channels=3). - /// Output image (depth=IPL_DEPTH_8U and num. channels=3). - /// Render mode. By default is CV_TRACK_RENDER_ID. - /// - /// - /// - /// - public void Render(Mat imgSource, Mat imgDest, RenderTracksModes mode, Scalar textColor, - HersheyFonts fontFace = HersheyFonts.HersheySimplex, double fontScale = 1d, int thickness = 1) - { - if (imgSource == null) - throw new ArgumentNullException(nameof(imgSource)); - if (imgDest == null) - throw new ArgumentNullException(nameof(imgDest)); - if (imgDest.Type() != MatType.CV_8UC3) - throw new ArgumentException("imgDest.Depth != U8 || imgDest.NChannels != 3"); - - if (mode != RenderTracksModes.None) - { - foreach (KeyValuePair kv in this) - { - int key = kv.Key; - CvTrack value = kv.Value; - - if ((mode & RenderTracksModes.Id) == RenderTracksModes.Id) - { - if (value.Inactive == 0) - { - Cv2.PutText(imgDest, key.ToString(CultureInfo.InvariantCulture), (Point)value.Centroid, - fontFace, fontScale, textColor, thickness); - } - } - if ((mode & RenderTracksModes.BoundingBox) == RenderTracksModes.BoundingBox) - { - if (value.Inactive > 0) - Cv2.Rectangle( - imgDest, - new Point(value.MinX, value.MinY), - new Point(value.MaxX - 1, value.MaxY - 1), - new Scalar(50, 0, 0)); - else - Cv2.Rectangle( - imgDest, - new Point(value.MinX, value.MinY), - new Point(value.MaxX - 1, value.MaxY - 1), - new Scalar(255, 0, 0)); - } - } - } - } - - #endregion - - #region ToString - - /// - /// - /// - /// - public override string ToString() - { - StringBuilder builder = new StringBuilder(); - foreach (KeyValuePair kv in this) - { - CvTrack value = kv.Value; - - builder.AppendLine($"Track {value}"); - if (value.Inactive > 0) - builder.AppendLine($" - Inactive for {value.Inactive} frames"); - else - builder.AppendLine($" - Associated with blobs {value.Label}"); - builder.AppendLine($" - Lifetime {value.LifeTime}"); - builder.AppendLine($" - Active {value.Active}"); - builder.AppendLine($" - Bounding box: ({value.MinX},{value.MinY}) - ({value.MaxX}, {value.MaxY})"); - builder.AppendLine($" - Centroid: ({value.Centroid.X}, {value.Centroid.Y})"); - builder.AppendLine(); - } - return builder.ToString(); - } - - #endregion - } -} \ No newline at end of file diff --git a/src/OpenCvSharp.Blob/Key.snk b/src/OpenCvSharp.Blob/Key.snk deleted file mode 100644 index a1e0330875a62647b4816405afd28c23b7c96bda..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 596 zcmV-a0;~N80ssI2Bme+XQ$aES1ONa50097hb`!zC2}+oW;oJ74*1Q;_u8X*R)f@Xe zRkMem+Z_{428PoA*!u|qx9GJm_8SoM-<5oDY)_n+EH~zLqx-#)7>dbkS1U6|hvjOc z8Fi=IZ@e-eC4OV+p9-Vzh|UayLJP+o42R(>0OUAi1{OY8#70hyBg5jP+*=`oDeLp6)fqWNcMc7kM zjP|n?eZB;&k%Kv8x;0!Z21V>rl}gybdz%UeGi#R*@y~Y^@ta~D#aHM%_#WBi>Y5R( zH0^)y0q4E@Wn_X%P~E2nBgZgo;W%&Ob&2G9l3-crFAka~6P=-|8^E&CmRYklr9xS7 zw8`CMwqDehI=wK{+H+plDau)KR6FVet|e zm40 - /// Label values for each pixel - /// - public class LabelData - { - private readonly Size size; - private readonly int[,] values; - - /// - /// Label value - /// - public ReadOnlyArray2D Values => new ReadOnlyArray2D(values); - - /// - /// Image sizw - /// - public Size Size => size; - - /// - /// Row length - /// - public int Rows => Values.GetLength(0); - - /// - /// Column Length - /// - public int Cols => Values.GetLength(1); - - /// - /// - /// - /// - /// - public LabelData(int rows, int cols) - { - values = new int[rows, cols]; - size = new Size(cols, rows); - } - - /// - /// - /// - /// - public LabelData(int[,] values) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - this.values = (int[,]) values.Clone(); - size.Height = values.GetLength(0); - size.Width = values.GetLength(1); - } - - /// - /// - /// - /// - /// - /// - public int RawGetLabel(int row, int col) - { - return values[row, col]; - } - - /// - /// - /// - /// - /// - /// - public void RawSetLabel(int row, int col, int value) - { - values[row, col] = value; - } - - /// - /// - /// - /// - /// - /// - public int this[int row, int col] - { - get => values[row, col]; - set => values[row, col] = value; - } - - /// - /// - /// - public void DebugShow() - { - using (Mat img = Mat.Zeros(Rows, Cols, MatType.CV_8UC1)) - { - var indexer = img.GetGenericIndexer(); - for (int r = 0; r < Rows; r++) - { - for (int c = 0; c < Cols; c++) - { - if (Values[r, c] != 0) - indexer[r, c] = 255; - } - } - Window.ShowImages(img); - } - } - - /// - /// Returns deep copied instance of this - /// - /// - public LabelData Clone() - { - return new LabelData((int[,]) values.Clone()); - } - } -} diff --git a/src/OpenCvSharp.Blob/Labeller.cs b/src/OpenCvSharp.Blob/Labeller.cs deleted file mode 100644 index d4ca0af14..000000000 --- a/src/OpenCvSharp.Blob/Labeller.cs +++ /dev/null @@ -1,356 +0,0 @@ -using System; -using System.Runtime.InteropServices; - -// Copyright (C) 2007 by Cristóbal Carnero Liñán -// grendel.ccl@gmail.com -// -// This file is part of cvBlob. -// -// cvBlob is free software: you can redistribute it and/or modify -// it under the terms of the Lesser GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// cvBlob is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// Lesser GNU General Public License for more details. -// -// You should have received a copy of the Lesser GNU General Public License -// along with cvBlob. If not, see . - -namespace OpenCvSharp.Blob -{ - /// - /// - /// - internal static class Labeller - { - private static readonly int[,,] MovesE = new int[4, 3, 4] - { - { - {-1, -1, 3, CvBlobConst.CV_CHAINCODE_UP_LEFT}, {0, -1, 0, CvBlobConst.CV_CHAINCODE_UP}, - {1, -1, 0, CvBlobConst.CV_CHAINCODE_UP_RIGHT} - }, - { - {1, -1, 0, CvBlobConst.CV_CHAINCODE_UP_RIGHT}, {1, 0, 1, CvBlobConst.CV_CHAINCODE_RIGHT}, - {1, 1, 1, CvBlobConst.CV_CHAINCODE_DOWN_RIGHT} - }, - { - {1, 1, 1, CvBlobConst.CV_CHAINCODE_DOWN_RIGHT}, {0, 1, 2, CvBlobConst.CV_CHAINCODE_DOWN}, - {-1, 1, 2, CvBlobConst.CV_CHAINCODE_DOWN_LEFT} - }, - { - {-1, 1, 2, CvBlobConst.CV_CHAINCODE_DOWN_LEFT}, {-1, 0, 3, CvBlobConst.CV_CHAINCODE_LEFT}, - {-1, -1, 3, CvBlobConst.CV_CHAINCODE_UP_LEFT} - } - }; - - private static readonly int[,,] MovesI = new int[4, 3, 4] - { - { - {1, -1, 3, CvBlobConst.CV_CHAINCODE_UP_RIGHT}, {0, -1, 0, CvBlobConst.CV_CHAINCODE_UP}, - {-1, -1, 0, CvBlobConst.CV_CHAINCODE_UP_LEFT} - }, - { - {-1, -1, 0, CvBlobConst.CV_CHAINCODE_UP_LEFT}, {-1, 0, 1, CvBlobConst.CV_CHAINCODE_LEFT}, - {-1, 1, 1, CvBlobConst.CV_CHAINCODE_DOWN_LEFT} - }, - { - {-1, 1, 1, CvBlobConst.CV_CHAINCODE_DOWN_LEFT}, {0, 1, 2, CvBlobConst.CV_CHAINCODE_DOWN}, - {1, 1, 2, CvBlobConst.CV_CHAINCODE_DOWN_RIGHT} - }, - { - {1, 1, 2, CvBlobConst.CV_CHAINCODE_DOWN_RIGHT}, {1, 0, 3, CvBlobConst.CV_CHAINCODE_RIGHT}, - {1, -1, 3, CvBlobConst.CV_CHAINCODE_UP_RIGHT} - } - }; - - /// - /// Value of invalid pixel. - /// -1 == uint.MaxValue - /// - private const int MarkerValue = -1; - - /// - /// - /// - /// - /// - /// - public static int Perform(Mat img, CvBlobs blobs) - { - if (img == null) - throw new ArgumentNullException(nameof(img)); - if (blobs == null) - throw new ArgumentNullException(nameof(blobs)); - if (img.Type() != MatType.CV_8UC1) - throw new ArgumentException("'img' must be a 1-channel U8 image."); - - if (blobs.Labels == null) - throw new ArgumentException("blobs.Labels == null", nameof(blobs)); - LabelData labels = blobs.Labels; - //if(labels.GetLength(0) != h || labels.GetLength(1) != w) - if (labels.Rows != img.Height || labels.Cols != img.Width) - throw new ArgumentException("img.Size != labels' size"); - - int numPixels = 0; - blobs.Clear(); - - int w = img.Cols; - int h = img.Rows; - int step = (int)img.Step(); - byte[] imgIn; - unsafe - { - byte* imgInPtr = (byte*)img.Data; - if ((long) h * step > int.MaxValue) - throw new ArgumentException("Too big image (image data > 2^31)"); - int length = h * step; - imgIn = new byte[length]; - Marshal.Copy(new IntPtr(imgInPtr), imgIn, 0, imgIn.Length); - } - int label = 0; - int lastLabel = 0; - CvBlob? lastBlob = null; - - for (int y = 0; y < h; y++) - { - for (int x = 0; x < w; x++) - { - if (imgIn[x + y * step] == 0) - continue; - - bool labeled = labels[y, x] != 0; - if (!labeled && ((y == 0) || (imgIn[x + (y - 1) * step] == 0))) - { - labeled = true; - - // Label contour. - label++; - if (label == MarkerValue) - throw new Exception(); - - labels[y, x] = label; - numPixels++; - - // XXX This is not necessary at all. I only do this for consistency. - if (y > 0) - labels[y - 1, x] = MarkerValue; - - var blob = new CvBlob(label, x, y); - blobs.Add(label, blob); - lastLabel = label; - lastBlob = blob; - - blob.Contour.StartingPoint = new Point(x, y); - int direction = 1; - int xx = x; - int yy = y; - bool contourEnd = false; - - do - { - for (int numAttempts = 0; numAttempts < 3; numAttempts++) - { - bool found = false; - for (int i = 0; i < 3; i++) - { - int nx = xx + MovesE[direction, i, 0]; - int ny = yy + MovesE[direction, i, 1]; - if ((nx < w) && (nx >= 0) && (ny < h) && (ny >= 0)) - { - if (imgIn[nx + ny * step] != 0) - { - found = true; - blob.Contour.ChainCode.Add((CvChainCode)MovesE[direction, i, 3]); - xx = nx; - yy = ny; - direction = MovesE[direction, i, 2]; - break; - } - labels[ny, nx] = MarkerValue; - } - } - - if (!found) - direction = (direction + 1) % 4; - else - { - if (labels[yy, xx] != label) - { - labels[yy, xx] = label; - numPixels++; - - if (xx < blob.MinX) - blob.MinX = xx; - else if (xx > blob.MaxX) - blob.MaxX = xx; - if (yy < blob.MinY) - blob.MinY = yy; - else if (yy > blob.MaxY) - blob.MaxY = yy; - - blob.Area++; - blob.M10 += xx; - blob.M01 += yy; - blob.M11 += xx * yy; - blob.M20 += xx * xx; - blob.M02 += yy * yy; - } - break; - } - - contourEnd = ((xx == x) && (yy == y) && (direction == 1)); - if (contourEnd) - break; - } - } while (!contourEnd); - - } - - if ((y + 1 < h) && (imgIn[x + (y + 1) * step] == 0) && (labels[y + 1, x] == 0)) - { - labeled = true; - - // Label internal contour - int l; - CvBlob? blob; - - if (labels[y, x] == 0) - { - l = labels[y, x - 1]; - labels[y, x] = l; - numPixels++; - - if (l == lastLabel) - blob = lastBlob; - else - { - blob = blobs[l]; - lastLabel = l; - lastBlob = blob; - } - if (blob == null) - throw new Exception(); - blob.Area++; - blob.M10 += x; - blob.M01 += y; - blob.M11 += x * y; - blob.M20 += x * x; - blob.M02 += y * y; - } - else - { - l = labels[y, x]; - if (l == lastLabel) - blob = lastBlob; - else - { - blob = blobs[l]; - lastLabel = l; - lastBlob = blob; - } - } - - if (blob == null) - throw new Exception(); - - // XXX This is not necessary (I believe). I only do this for consistency. - labels[y + 1, x] = MarkerValue; - var contour = new CvContourChainCode - { - StartingPoint = new Point(x, y) - }; - - int direction = 3; - int xx = x; - int yy = y; - - do - { - for (int numAttempts = 0; numAttempts < 3; numAttempts++) - { - bool found = false; - - for (int i = 0; i < 3; i++) - { - int nx = xx + MovesI[direction, i, 0]; - int ny = yy + MovesI[direction, i, 1]; - if (imgIn[nx + ny * step] != 0) - { - found = true; - contour.ChainCode.Add((CvChainCode)MovesI[direction, i, 3]); - xx = nx; - yy = ny; - direction = MovesI[direction, i, 2]; - break; - } - labels[ny, nx] = MarkerValue; - } - - if (!found) - direction = (direction + 1) % 4; - else - { - if (labels[yy, xx] == 0) - { - labels[yy, xx] = l; - numPixels++; - - blob.Area++; - blob.M10 += xx; - blob.M01 += yy; - blob.M11 += xx * yy; - blob.M20 += xx * xx; - blob.M02 += yy * yy; - } - break; - } - } - } while (!(xx == x && yy == y)); - - blob.InternalContours.Add(contour); - } - - //else if (!imageOut(x, y)) - if (!labeled) - { - // Internal pixel - int l = labels[y, x - 1]; - labels[y, x] = l; - numPixels++; - - CvBlob? blob; - if (l == lastLabel) - blob = lastBlob; - else - { - blob = blobs[l]; - lastLabel = l; - lastBlob = blob; - } - if (blob == null) - throw new Exception(); - blob.Area++; - blob.M10 += x; - blob.M01 += y; - blob.M11 += x * y; - blob.M20 += x * x; - blob.M02 += y * y; - } - } - } - - - foreach (var kv in blobs) - { - kv.Value.SetMoments(); - } - - GC.KeepAlive(img); - - return numPixels; - } - } -} diff --git a/src/OpenCvSharp.Blob/OpenCvSharp.Blob.csproj b/src/OpenCvSharp.Blob/OpenCvSharp.Blob.csproj deleted file mode 100644 index b82a373bc..000000000 --- a/src/OpenCvSharp.Blob/OpenCvSharp.Blob.csproj +++ /dev/null @@ -1,48 +0,0 @@ - - - - netstandard2.0;netstandard2.1;netcoreapp2.1;netcoreapp3.1;net48;net461 - true - true - OpenCvSharp.Blob - Key.snk - true - true - OpenCvSharp.Blob - false - false - false - 9 - enable - true - true - snupkg - AllEnabledByDefault - - - - - - - - - - - - - - - - - $(DefineConstants);DOTNET_FRAMEWORK; - - - - $(DefineConstants);DOTNETCORE - - - - CA1303;CA1814; - - - diff --git a/src/OpenCvSharp.Blob/Properties/AssemblyInfo.cs b/src/OpenCvSharp.Blob/Properties/AssemblyInfo.cs deleted file mode 100644 index 9f0a804b0..000000000 --- a/src/OpenCvSharp.Blob/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Reflection; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("OpenCvSharp.Blob")] -[assembly: AssemblyTrademark("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("82afda65-515e-4ec0-a415-77d8a6711508")] diff --git a/src/OpenCvSharp.Blob/ProximityMatrix.cs b/src/OpenCvSharp.Blob/ProximityMatrix.cs deleted file mode 100644 index e273fbede..000000000 --- a/src/OpenCvSharp.Blob/ProximityMatrix.cs +++ /dev/null @@ -1,97 +0,0 @@ -namespace OpenCvSharp.Blob -{ - internal class ProximityMatrix - { - private readonly int[] close; - private readonly int nBlobs; - private readonly int nTtacks; - - public ProximityMatrix(int nBlobs, int nTracks) - { - this.nBlobs = nBlobs; - this.nTtacks = nTracks; - this.close = new int[(nBlobs + 2) * (nTracks + 2)]; - AB = new ABIndexer(this); - AT = new ATIndexer(this); - IB = new IBIndexer(this); - IT = new ITIndexer(this); - } - - public int this[int blob, int track] - { - get - { - return close[((blob) + (track) * (nBlobs + 2))]; - } - set - { - close[((blob) + (track) * (nBlobs + 2))] = value; - } - } - - #region Indexer - public abstract class Indexer - { - protected ProximityMatrix matrix; - protected Indexer(ProximityMatrix matrix) - { - this.matrix = matrix; - } - public abstract int this[int index] { get; set; } - } - public class ABIndexer : Indexer - { - internal ABIndexer(ProximityMatrix matrix) - : base(matrix) - { - } - public override int this[int label] - { - get { return matrix[label, matrix.nTtacks]; } - set { matrix[label, matrix.nTtacks] = value; } - } - } - public class ATIndexer : Indexer - { - internal ATIndexer(ProximityMatrix matrix) - : base(matrix) - { - } - public override int this[int id] - { - get { return matrix[matrix.nBlobs, id]; } - set { matrix[matrix.nBlobs, id] = value; } - } - } - public class IBIndexer : Indexer - { - internal IBIndexer(ProximityMatrix matrix) - : base(matrix) - { - } - public override int this[int label] - { - get { return matrix[label, matrix.nTtacks + 1]; } - set { matrix[label, matrix.nTtacks + 1] = value; } - } - } - public class ITIndexer : Indexer - { - internal ITIndexer(ProximityMatrix matrix) - : base(matrix) - { - } - public override int this[int id] - { - get { return matrix[matrix.nBlobs + 1, id]; } - set { matrix[matrix.nBlobs + 1, id] = value; } - } - } - - public ABIndexer AB { get; private set; } - public ATIndexer AT { get; private set; } - public IBIndexer IB { get; private set; } - public ITIndexer IT { get; private set; } - #endregion - } -} diff --git a/src/OpenCvSharp.Blob/ReadMe.txt b/src/OpenCvSharp.Blob/ReadMe.txt deleted file mode 100644 index 0c8268a9d..000000000 --- a/src/OpenCvSharp.Blob/ReadMe.txt +++ /dev/null @@ -1,3 +0,0 @@ -This project "OpenCvSharp.Blob" is a wrapper of cvblob (http://code.google.com/p/cvblob/). - -cvblob and OpenCvSharp are licensed using the LGPL (http://www.gnu.org/licenses/lgpl.html). diff --git a/src/OpenCvSharp.Blob/RenderBlobsMode.cs b/src/OpenCvSharp.Blob/RenderBlobsMode.cs deleted file mode 100644 index e1c88eaae..000000000 --- a/src/OpenCvSharp.Blob/RenderBlobsMode.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; - -namespace OpenCvSharp.Blob -{ - /// - /// Render mode of cvRenderBlobs - /// - [Flags] - public enum RenderBlobsModes - { - /// - /// No flags (=0) - /// - None = 0, - - /// - /// Render each blog with a different color. - /// [CV_BLOB_RENDER_COLOR] - /// - Color = CvBlobConst.CV_BLOB_RENDER_COLOR, - - /// - /// Render centroid. - /// CV_BLOB_RENDER_CENTROID] - /// - Centroid = CvBlobConst.CV_BLOB_RENDER_CENTROID, - - /// - /// Render bounding box. - /// [CV_BLOB_RENDER_BOUNDING_BOX] - /// - BoundingBox = CvBlobConst.CV_BLOB_RENDER_BOUNDING_BOX, - - /// - /// Render angle. - /// [CV_BLOB_RENDER_ANGLE] - /// - Angle = CvBlobConst.CV_BLOB_RENDER_ANGLE, - - /// - /// Print blob data to log out. - /// [CV_BLOB_RENDER_TO_LOG] - /// - ToLog = CvBlobConst.CV_BLOB_RENDER_TO_LOG, - - /// - /// Print blob data to std out. - /// [CV_BLOB_RENDER_TO_STD] - /// - ToStd = CvBlobConst.CV_BLOB_RENDER_TO_STD, - } -} diff --git a/src/OpenCvSharp.Blob/RenderTracksMode.cs b/src/OpenCvSharp.Blob/RenderTracksMode.cs deleted file mode 100644 index 99da5a030..000000000 --- a/src/OpenCvSharp.Blob/RenderTracksMode.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; - -namespace OpenCvSharp.Blob -{ - /// - /// Render mode of cvRenderTracks - /// - [Flags] - public enum RenderTracksModes - { - /// - /// No flags - /// [0] - /// - None = 0, - - /// - /// Print the ID of each track in the image. - /// [CV_TRACK_RENDER_ID] - /// - Id = CvBlobConst.CV_TRACK_RENDER_ID, - - /// - /// Draw bounding box of each track in the image. \see cvRenderTracks - /// [CV_TRACK_RENDER_BOUNDING_BOX] - /// - BoundingBox = CvBlobConst.CV_TRACK_RENDER_BOUNDING_BOX, - } -}