From 30e44bdcee5aecb8796524006a59012ed9c24b95 Mon Sep 17 00:00:00 2001 From: n0099 Date: Mon, 29 May 2023 15:28:44 +0800 Subject: [PATCH 1/4] Update RotatedRect.cs --- .../Modules/core/Struct/RotatedRect.cs | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs b/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs index d586c96cb..c6e142d30 100644 --- a/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs +++ b/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; @@ -39,6 +39,47 @@ public RotatedRect(Point2f center, Size2f size, float angle) Angle = angle; } + /// + /// Any 3 end points of the RotatedRect. They must be given in order (either clockwise or anticlockwise). + /// + public RotatedRect(Point2f point1, Point2f point2, Point2f point3) + { // https://github.com/opencv/opencv/blob/6ad77b23193bdf7e40db83e6077789284ac08781/modules/core/src/types.cpp#LL147C20-L147C20 + var center = (point1 + point3) * 0.5; + var vecs = new Vec2f[2]; + vecs[0] = (point1 - point2).ToVec2f(); + vecs[1] = (point2 - point3).ToVec2f(); + var x = Math.Max(Cv2.Norm(point1.ToVec2f()), Math.Max(Cv2.Norm(point2.ToVec2f()), Cv2.Norm(point3.ToVec2f()))); + var a = Math.Min(Cv2.Norm(vecs[0]), Cv2.Norm(vecs[1])); + + const float fltEpsilon = 1.19209290e-7f; + static double Ddot(Vec2f a, Vec2f b) + { + var s = 0d; + for (var i = 0; i < 2; i++) + { + s += (double)a[i] * b[i]; + } + return s; + } + // check that given sides are perpendicular + Debug.Assert(Math.Abs(Ddot(vecs[0], vecs[1])) * a <= fltEpsilon * 9 * x * (Cv2.Norm(vecs[0]) * Cv2.Norm(vecs[1]))); + + // wd_i stores which vector (0,1) or (1,2) will make the width + // One of them will definitely have slope within -1 to 1 + var wdI = 0; + if (Math.Abs(vecs[1][1]) < Math.Abs(vecs[1][0])) + { + wdI = 1; + } + var htI = (wdI + 1) % 2; + + var angle = Math.Atan(vecs[wdI][1] / vecs[wdI][0]) * 180.0f / (float)Math.PI; + var width = (float)Cv2.Norm(vecs[wdI]); + var height = (float)Cv2.Norm(vecs[htI]); + + return new(center, new(width, height), (float)angle); + } + /// /// returns 4 vertices of the rectangle /// From 73727e2809d2947d4533de6fefac87e08f7b573e Mon Sep 17 00:00:00 2001 From: n0099 Date: Mon, 29 May 2023 15:31:50 +0800 Subject: [PATCH 2/4] Update RotatedRect.cs --- src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs b/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs index c6e142d30..cf4f180a3 100644 --- a/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs +++ b/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs @@ -51,9 +51,9 @@ public RotatedRect(Point2f point1, Point2f point2, Point2f point3) var x = Math.Max(Cv2.Norm(point1.ToVec2f()), Math.Max(Cv2.Norm(point2.ToVec2f()), Cv2.Norm(point3.ToVec2f()))); var a = Math.Min(Cv2.Norm(vecs[0]), Cv2.Norm(vecs[1])); - const float fltEpsilon = 1.19209290e-7f; + const float fltEpsilon = 1.19209290e-7f; // https://github.com/opencv/opencv/blob/6ad77b23193bdf7e40db83e6077789284ac08781/modules/dnn/src/math_utils.hpp#L39 static double Ddot(Vec2f a, Vec2f b) - { + { // https://github.com/opencv/opencv/blob/0052d46b8e33c7bfe0e1450e4bff28b88f455570/modules/core/include/opencv2/core/matx.hpp#L741 var s = 0d; for (var i = 0; i < 2; i++) { From 92dc38aa53a3e22658d0100eb45e457cf0c13fae Mon Sep 17 00:00:00 2001 From: n0099 Date: Tue, 30 May 2023 15:45:04 +0800 Subject: [PATCH 3/4] Update OpenCvSharp.Tests.csproj --- test/OpenCvSharp.Tests/OpenCvSharp.Tests.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/OpenCvSharp.Tests/OpenCvSharp.Tests.csproj b/test/OpenCvSharp.Tests/OpenCvSharp.Tests.csproj index 0f2c9f2dc..f2f291055 100644 --- a/test/OpenCvSharp.Tests/OpenCvSharp.Tests.csproj +++ b/test/OpenCvSharp.Tests/OpenCvSharp.Tests.csproj @@ -1,4 +1,4 @@ - + net6.0;net48 @@ -10,7 +10,7 @@ false false false - 10 + 11 enable From f725f6a52aa120407dbdd97b86441196f3b8093b Mon Sep 17 00:00:00 2001 From: n0099 Date: Tue, 30 May 2023 15:47:40 +0800 Subject: [PATCH 4/4] Update RotatedRect.cs --- src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs b/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs index cf4f180a3..bdf49c95b 100644 --- a/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs +++ b/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices;