Skip to content

Commit

Permalink
Merge pull request #1479 from kjagosz/master
Browse files Browse the repository at this point in the history
Restore LineSegmentDetector
  • Loading branch information
shimat authored Sep 25, 2022
2 parents a82a981 + f9e6c49 commit f6b8d7a
Show file tree
Hide file tree
Showing 13 changed files with 336 additions and 6 deletions.
2 changes: 1 addition & 1 deletion nuget/OpenCvSharp4.Extensions.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>OpenCvSharp4.Extensions</id>
<version>4.3.0.20190901</version>
<version>4.6.0.20220607</version>
<title>OpenCvSharp GDI+ extension library.</title>
<authors>shimat</authors>
<license type="expression">Apache-2.0</license>
Expand Down
2 changes: 1 addition & 1 deletion nuget/OpenCvSharp4.Windows.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>OpenCvSharp4.Windows</id>
<version>4.3.0.20190901</version>
<version>4.6.0.20220607</version>
<title>OpenCvSharp NuGet package for x64/x86 Windows (same as OpenCvSharp3-AnyCPU)</title>
<authors>shimat</authors>
<license type="expression">Apache-2.0</license>
Expand Down
2 changes: 1 addition & 1 deletion nuget/OpenCvSharp4.WpfExtensions.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>OpenCvSharp4.WpfExtensions</id>
<version>4.3.0.20190901</version>
<version>4.6.0.20220607</version>
<title>OpenCvSharp WPF extension library.</title>
<authors>shimat</authors>
<license type="expression">Apache-2.0</license>
Expand Down
2 changes: 1 addition & 1 deletion nuget/OpenCvSharp4.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>OpenCvSharp4</id>
<version>4.3.0.20190901</version>
<version>4.6.0.20220607</version>
<title>OpenCvSharp core library. A package of separate native bindings for your OS is required.</title>
<authors>shimat</authors>
<license type="expression">Apache-2.0</license>
Expand Down
2 changes: 1 addition & 1 deletion nuget/OpenCvSharp4.runtime.uwp.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>OpenCvSharp4.runtime.uwp</id>
<version>4.3.0.20190901</version>
<version>4.6.0.20220607</version>
<title>OpenCvSharp4 native bindings for UWP x64/x86/ARM</title>
<authors>shimat</authors>
<license type="expression">Apache-2.0</license>
Expand Down
2 changes: 1 addition & 1 deletion nuget/OpenCvSharp4.runtime.win.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>OpenCvSharp4.runtime.win</id>
<version>4.3.0.20190901</version>
<version>4.6.0.20220607</version>
<title>OpenCvSharp4 native bindings for Windows x64/x86 (except UWP)</title>
<authors>shimat</authors>
<license type="expression">Apache-2.0</license>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;

#pragma warning disable 1591
#pragma warning disable CA1401 // P/Invokes should not be visible
#pragma warning disable CA1720 // Identifiers should not contain type names
#pragma warning disable IDE1006 // Naming style

namespace OpenCvSharp.Internal;

static partial class NativeMethods
{
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void imgproc_LineSegmentDetector_detect_OutputArray(IntPtr obj, IntPtr image, IntPtr lines,
IntPtr width, IntPtr prec, IntPtr nfa);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void imgproc_LineSegmentDetector_detect_vector(IntPtr obj, IntPtr image, IntPtr lines,
IntPtr width, IntPtr prec, IntPtr nfa);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void imgproc_LineSegmentDetector_drawSegments(IntPtr obj, IntPtr image, IntPtr lines);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int imgproc_LineSegmentDetector_compareSegments(IntPtr obj, Size size,
IntPtr lines1, IntPtr lines2, IntPtr image);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern IntPtr imgproc_createLineSegmentDetector(
int refine, double scale, double sigma_scale, double quant, double ang_th,
double log_eps, double density_th, int n_bins);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void imgproc_Ptr_LineSegmentDetector_delete(IntPtr obj);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern IntPtr imgproc_Ptr_LineSegmentDetector_get(IntPtr obj);
}
29 changes: 29 additions & 0 deletions src/OpenCvSharp/Modules/imgproc/Enum/LineSegmentDetectorModes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

#pragma warning disable CA1008 // Enums should have zero value

namespace OpenCvSharp;

/// <summary>
/// Variants of Line Segment %Detector
/// </summary>
[Flags]
public enum LineSegmentDetectorModes : int
{
/// <summary>
/// No refinement applied
/// </summary>
RefineNone = 0,

/// <summary>
/// Standard refinement is applied. E.g. breaking arches into smaller straighter line approximations.
/// </summary>
RefineStd = 1,

/// <summary>
/// Advanced refinement. Number of false alarms is calculated, lines are
/// refined through increase of precision, decrement in size, etc.
/// </summary>
RefineAdv = 2,
}

208 changes: 208 additions & 0 deletions src/OpenCvSharp/Modules/imgproc/LineSegmentDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenCvSharp.Internal;
using OpenCvSharp.Internal.Util;
using OpenCvSharp.Internal.Vectors;

namespace OpenCvSharp
{
/// <summary>
/// Line segment detector class
/// </summary>
public class LineSegmentDetector : Algorithm
{
/// <summary>
/// cv::Ptr&lt;LineSegmentDetector&gt;
/// </summary>
private Ptr ptrObj;

/// <summary>
///
/// </summary>
protected LineSegmentDetector(IntPtr p)
{
ptrObj = new Ptr(p);
ptr = ptrObj.Get();
}

/// <summary>
/// Creates a smart pointer to a LineSegmentDetector object and initializes it.
/// </summary>
/// <param name="refine">The way found lines will be refined, see cv::LineSegmentDetectorModes</param>
/// <param name="scale">The scale of the image that will be used to find the lines. Range (0..1].</param>
/// <param name="sigmaScale">Sigma for Gaussian filter. It is computed as sigma = _sigma_scale/_scale.</param>
/// <param name="quant">Bound to the quantization error on the gradient norm.</param>
/// <param name="angTh">Gradient angle tolerance in degrees.</param>
/// <param name="logEps">Detection threshold: -log10(NFA) \> log_eps.
/// Used only when advancent refinement is chosen.</param>
/// <param name="densityTh">Minimal density of aligned region points in the enclosing rectangle.</param>
/// <param name="nBins">Number of bins in pseudo-ordering of gradient modulus.</param>
/// <returns></returns>
public static LineSegmentDetector Create(
LineSegmentDetectorModes refine = LineSegmentDetectorModes.RefineNone,
double scale = 0.8, double sigmaScale = 0.6, double quant = 2.0, double angTh = 22.5,
double logEps = 0, double densityTh = 0.7, int nBins = 1024)
{
IntPtr ptr = NativeMethods.imgproc_createLineSegmentDetector(
(int)refine, scale, sigmaScale, quant, angTh, logEps, densityTh, nBins);
return new LineSegmentDetector(ptr);
}

/// <summary>
/// Releases managed resources
/// </summary>
protected override void DisposeManaged()
{
ptrObj?.Dispose();
ptrObj = null;
base.DisposeManaged();
}

/// <summary>
/// Finds lines in the input image.
/// This is the output of the default parameters of the algorithm on the above shown image.
/// </summary>
/// <param name="image">A grayscale (CV_8UC1) input image. </param>
/// <param name="lines">A vector of Vec4i or Vec4f elements specifying the beginning and ending point of a line.
/// Where Vec4i/Vec4f is (x1, y1, x2, y2), point 1 is the start, point 2 - end. Returned lines are strictly oriented depending on the gradient.</param>
/// <param name="width">Vector of widths of the regions, where the lines are found. E.g. Width of line.</param>
/// <param name="prec">Vector of precisions with which the lines are found.</param>
/// <param name="nfa">Vector containing number of false alarms in the line region,
/// with precision of 10%. The bigger the value, logarithmically better the detection.</param>
public virtual void Detect(InputArray image, OutputArray lines,
OutputArray width = null, OutputArray prec = null, OutputArray nfa = null)
{
if (image == null)
throw new ArgumentNullException(nameof(image));
if (lines == null)
throw new ArgumentNullException(nameof(lines));
image.ThrowIfDisposed();
lines.ThrowIfNotReady();
width?.ThrowIfNotReady();
prec?.ThrowIfNotReady();
nfa?.ThrowIfNotReady();

NativeMethods.imgproc_LineSegmentDetector_detect_OutputArray(ptr, image.CvPtr, lines.CvPtr,
Cv2.ToPtr(width), Cv2.ToPtr(prec), Cv2.ToPtr(nfa));
GC.KeepAlive(this);
GC.KeepAlive(image);
GC.KeepAlive(lines);
GC.KeepAlive(width);
GC.KeepAlive(prec);
GC.KeepAlive(nfa);
lines.Fix();
width?.Fix();
prec?.Fix();
nfa?.Fix();
}

/// <summary>
/// Finds lines in the input image.
/// This is the output of the default parameters of the algorithm on the above shown image.
/// </summary>
/// <param name="image">A grayscale (CV_8UC1) input image. </param>
/// <param name="lines">A vector of Vec4i or Vec4f elements specifying the beginning and ending point of a line.
/// Where Vec4i/Vec4f is (x1, y1, x2, y2), point 1 is the start, point 2 - end. Returned lines are strictly oriented depending on the gradient.</param>
/// <param name="width">Vector of widths of the regions, where the lines are found. E.g. Width of line.</param>
/// <param name="prec">Vector of precisions with which the lines are found.</param>
/// <param name="nfa">Vector containing number of false alarms in the line region,
/// with precision of 10%. The bigger the value, logarithmically better the detection.</param>
public virtual void Detect(InputArray image, out Vec4f[] lines,
out double[] width, out double[] prec, out double[] nfa)
{
if (image == null)
throw new ArgumentNullException(nameof(image));
image.ThrowIfDisposed();

using (var linesVec = new VectorOfVec4f())
using (var widthVec = new VectorOfDouble())
using (var precVec = new VectorOfDouble())
using (var nfaVec = new VectorOfDouble())
{
NativeMethods.imgproc_LineSegmentDetector_detect_vector(ptr, image.CvPtr,
linesVec.CvPtr, widthVec.CvPtr, precVec.CvPtr, nfaVec.CvPtr);

lines = linesVec.ToArray();
width = widthVec.ToArray();
prec = precVec.ToArray();
nfa = nfaVec.ToArray();
}
GC.KeepAlive(this);
GC.KeepAlive(image);
}

/// <summary>
/// Draws the line segments on a given image.
/// </summary>
/// <param name="image">The image, where the liens will be drawn.
/// Should be bigger or equal to the image, where the lines were found.</param>
/// <param name="lines">A vector of the lines that needed to be drawn.</param>
public virtual void DrawSegments(InputOutputArray image, InputArray lines)
{
if (image == null)
throw new ArgumentNullException(nameof(image));
if (lines == null)
throw new ArgumentNullException(nameof(lines));
image.ThrowIfNotReady();
lines.ThrowIfDisposed();

NativeMethods.imgproc_LineSegmentDetector_drawSegments(ptr, image.CvPtr, lines.CvPtr);
GC.KeepAlive(this);
GC.KeepAlive(image);
image.Fix();
GC.KeepAlive(lines);
}

/// <summary>
/// Draws two groups of lines in blue and red, counting the non overlapping (mismatching) pixels.
/// </summary>
/// <param name="size">The size of the image, where lines1 and lines2 were found.</param>
/// <param name="lines1">The first group of lines that needs to be drawn. It is visualized in blue color.</param>
/// <param name="lines2">The second group of lines. They visualized in red color.</param>
/// <param name="image">Optional image, where the lines will be drawn.
/// The image should be color(3-channel) in order for lines1 and lines2 to be drawn
/// in the above mentioned colors.</param>
/// <returns></returns>
public virtual int CompareSegments(
Size size, InputArray lines1, InputArray lines2, InputOutputArray image = null)
{
if (lines1 == null)
throw new ArgumentNullException(nameof(lines1));
if (lines2 == null)
throw new ArgumentNullException(nameof(lines2));
lines1.ThrowIfDisposed();
lines2.ThrowIfDisposed();
image?.ThrowIfNotReady();

var ret = NativeMethods.imgproc_LineSegmentDetector_compareSegments(
ptr, size, lines1.CvPtr, lines2.CvPtr, Cv2.ToPtr(image));
GC.KeepAlive(this);
GC.KeepAlive(lines1);
GC.KeepAlive(lines2);
GC.KeepAlive(image);
image?.Fix();

return ret;
}
internal class Ptr : OpenCvSharp.Ptr
{
public Ptr(IntPtr ptr) : base(ptr)
{
}

public override IntPtr Get()
{
var res = NativeMethods.imgproc_Ptr_LineSegmentDetector_get(ptr);
GC.KeepAlive(this);
return res;
}

protected override void DisposeUnmanaged()
{
NativeMethods.imgproc_Ptr_LineSegmentDetector_delete(ptr);
base.DisposeUnmanaged();
}
}
}
}
1 change: 1 addition & 0 deletions src/OpenCvSharpExtern/OpenCvSharpExtern.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ copy "$(SolutionDir)opencv_files\opencv460_win_x64\x64\vc17\bin\opencv_videoio_f
<ClInclude Include="highgui.h" />
<ClInclude Include="imgproc.h" />
<ClInclude Include="imgproc_GeneralizedHough.h" />
<ClInclude Include="imgproc_LineSegmentDetector.h" />
<ClInclude Include="imgproc_LineIterator.h" />
<ClInclude Include="imgproc_Segmentation.h" />
<ClInclude Include="imgproc_Subdiv2D.h" />
Expand Down
3 changes: 3 additions & 0 deletions src/OpenCvSharpExtern/OpenCvSharpExtern.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@
<ClInclude Include="wechat_qrcode.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="imgproc_LineSegmentDetector.h">
<Filter>Header Files\imgproc</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Source Files">
Expand Down
1 change: 1 addition & 0 deletions src/OpenCvSharpExtern/imgproc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
#include "imgproc_LineIterator.h"
#include "imgproc_GeneralizedHough.h"
#include "imgproc_Segmentation.h"
#include "imgproc_LineSegmentDetector.h"
Loading

0 comments on commit f6b8d7a

Please sign in to comment.