Skip to content

Commit 6da2b43

Browse files
committed
Added CLAHE
1 parent 5b36402 commit 6da2b43

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+504
-27
lines changed

src/OpenCvSharp.CPlusPlus/Cv2/Cv2_imgproc.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,18 @@ public static void EqualizeHist(InputArray src, OutputArray dst)
17621762
dst.Fix();
17631763
}
17641764
#endregion
1765+
#region CreateCLAHE
1766+
/// <summary>
1767+
/// Creates a predefined CLAHE object
1768+
/// </summary>
1769+
/// <param name="clipLimit"></param>
1770+
/// <param name="tileGridSize"></param>
1771+
/// <returns></returns>
1772+
public static CLAHE CreateCLAHE(double clipLimit = 40.0, Size? tileGridSize = null)
1773+
{
1774+
return CLAHE.Create(clipLimit, tileGridSize);
1775+
}
1776+
#endregion
17651777
#region EMD
17661778
/// <summary>
17671779
///

src/OpenCvSharp.CPlusPlus/OpenCvSharp.CPlusPlus.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
<Compile Include="modules\gpu\GpuMatRowColIndexer.cs" />
115115
<Compile Include="modules\gpu\HOGDescriptor.cs" />
116116
<Compile Include="modules\gpu\StereoBM_GPU.cs" />
117+
<Compile Include="modules\imgproc\CLAHE.cs" />
117118
<Compile Include="modules\ml\Class\CvDTreeNode.cs" />
118119
<Compile Include="modules\ml\Class\CvDTreeSplit.cs" />
119120
<Compile Include="modules\ml\Class\CvDTreeTrainData.cs" />
@@ -192,6 +193,7 @@
192193
<Compile Include="PInvoke\ml\NativeMethods_ml_CvDTree.cs" />
193194
<Compile Include="PInvoke\ml\NativeMethods_ml_CvBoost.cs" />
194195
<Compile Include="PInvoke\ml\NativeMethods_ml_CvANN_MLP.cs" />
196+
<Compile Include="PInvoke\NativeMethods_imgproc_CLAHE.cs" />
195197
<Compile Include="PInvoke\NativeMethods_photo.cs" />
196198
<Compile Include="modules\calib3d\StereoBM.cs" />
197199
<Compile Include="modules\core\Mat\MatIndexer.cs" />
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
#pragma warning disable 1591
5+
6+
namespace OpenCvSharp.CPlusPlus
7+
{
8+
static partial class NativeMethods
9+
{
10+
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
11+
public static extern IntPtr imgproc_createCLAHE(double clipLimit, CvSize tileGridSize);
12+
13+
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
14+
public static extern void imgproc_Ptr_CLAHE_delete(IntPtr obj);
15+
16+
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
17+
public static extern IntPtr imgproc_Ptr_CLAHE_obj(IntPtr obj);
18+
19+
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
20+
public static extern IntPtr imgproc_CLAHE_info(IntPtr obj);
21+
22+
23+
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
24+
public static extern void imgproc_CLAHE_apply(IntPtr obj, IntPtr src, IntPtr dst);
25+
26+
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
27+
public static extern void imgproc_CLAHE_setClipLimit(IntPtr obj, double clipLimit);
28+
29+
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
30+
public static extern double imgproc_CLAHE_getClipLimit(IntPtr obj);
31+
32+
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
33+
public static extern void imgproc_CLAHE_setTilesGridSize(IntPtr obj, CvSize tileGridSize);
34+
35+
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
36+
public static extern CvSize imgproc_CLAHE_getTilesGridSize(IntPtr obj);
37+
38+
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
39+
public static extern void imgproc_CLAHE_collectGarbage(IntPtr obj);
40+
}
41+
}

src/OpenCvSharp.CPlusPlus/modules/core/Ptr.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ static Ptr()
5555
{ typeof(BackgroundSubtractorMOG), NativeMethods.video_Ptr_BackgroundSubtractorMOG_delete },
5656
{ typeof(BackgroundSubtractorMOG2), NativeMethods.video_Ptr_BackgroundSubtractorMOG2_delete },
5757
{ typeof(BackgroundSubtractorGMG), NativeMethods.video_Ptr_BackgroundSubtractorGMG_delete },
58+
{ typeof(CLAHE), NativeMethods.imgproc_Ptr_CLAHE_delete },
5859
};
5960
definedObjFunctions = new Dictionary<Type, ObjFunc>
6061
{
@@ -87,6 +88,7 @@ static Ptr()
8788
{ typeof(BackgroundSubtractorMOG), NativeMethods.video_Ptr_BackgroundSubtractorMOG_obj },
8889
{ typeof(BackgroundSubtractorMOG2), NativeMethods.video_Ptr_BackgroundSubtractorMOG2_obj },
8990
{ typeof(BackgroundSubtractorGMG), NativeMethods.video_Ptr_BackgroundSubtractorGMG_obj },
91+
{ typeof(CLAHE), NativeMethods.imgproc_Ptr_CLAHE_obj },
9092
};
9193
}
9294

src/OpenCvSharp.CPlusPlus/modules/features2d/FeatureDetector.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace OpenCvSharp.CPlusPlus
1717
public class FeatureDetector : Algorithm
1818
{
1919
private bool disposed;
20+
2021
/// <summary>
2122
/// cv::Ptr&lt;FeatureDetector&gt;
2223
/// </summary>
@@ -30,6 +31,7 @@ internal FeatureDetector()
3031
detectorPtr = null;
3132
ptr = IntPtr.Zero;
3233
}
34+
3335
/// <summary>
3436
/// Creates instance from cv::Ptr&lt;T&gt; .
3537
/// ptr is disposed when the wrapper disposes.
@@ -42,12 +44,13 @@ internal static FeatureDetector FromPtr(IntPtr ptr)
4244

4345
var ptrObj = new Ptr<FeatureDetector>(ptr);
4446
var detector = new FeatureDetector
45-
{
46-
detectorPtr = ptrObj,
47-
ptr = ptrObj.Obj
48-
};
47+
{
48+
detectorPtr = ptrObj,
49+
ptr = ptrObj.Obj
50+
};
4951
return detector;
5052
}
53+
5154
/// <summary>
5255
/// Creates instance from raw T*
5356
/// </summary>
@@ -57,10 +60,10 @@ internal static FeatureDetector FromRawPtr(IntPtr ptr)
5760
if (ptr == IntPtr.Zero)
5861
throw new OpenCvSharpException("Invalid FeatureDetector pointer");
5962
var detector = new FeatureDetector
60-
{
61-
detectorPtr = null,
62-
ptr = ptr
63-
};
63+
{
64+
detectorPtr = null,
65+
ptr = ptr
66+
};
6467
return detector;
6568
}
6669

@@ -127,7 +130,7 @@ public override IntPtr InfoPtr
127130
/// <returns>The detected keypoints.</returns>
128131
public KeyPoint[] Detect(Mat image, Mat mask = null)
129132
{
130-
if(image == null)
133+
if (image == null)
131134
throw new ArgumentNullException("image");
132135
using (var keypoints = new VectorOfKeyPoint())
133136
{
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
using System;
2+
3+
namespace OpenCvSharp.CPlusPlus
4+
{
5+
/// <summary>
6+
/// Contrast Limited Adaptive Histogram Equalization
7+
/// </summary>
8+
public sealed class CLAHE : Algorithm
9+
{
10+
private bool disposed;
11+
/// <summary>
12+
/// cv::Ptr&lt;CLAHE&gt;
13+
/// </summary>
14+
private Ptr<CLAHE> ptrObj;
15+
16+
/// <summary>
17+
///
18+
/// </summary>
19+
internal CLAHE()
20+
{
21+
ptr = IntPtr.Zero;
22+
ptrObj = null;
23+
}
24+
25+
/// <summary>
26+
/// Creates instance from cv::Ptr&lt;T&gt; .
27+
/// ptr is disposed when the wrapper disposes.
28+
/// </summary>
29+
/// <param name="ptr"></param>
30+
internal static CLAHE FromPtr(IntPtr ptr)
31+
{
32+
if (ptr == IntPtr.Zero)
33+
throw new OpenCvSharpException("Invalid CLAHE pointer");
34+
35+
var ptrObj = new Ptr<CLAHE>(ptr);
36+
var ret = new CLAHE
37+
{
38+
ptr = ptrObj.Obj,
39+
ptrObj = ptrObj,
40+
};
41+
return ret;
42+
}
43+
44+
/// <summary>
45+
/// Creates a predefined CLAHE object
46+
/// </summary>
47+
/// <param name="clipLimit"></param>
48+
/// <param name="tileGridSize"></param>
49+
/// <returns></returns>
50+
public static CLAHE Create(double clipLimit = 40.0, Size? tileGridSize = null)
51+
{
52+
IntPtr ptr = NativeMethods.imgproc_createCLAHE(
53+
clipLimit, tileGridSize.GetValueOrDefault(new Size(8, 8)));
54+
return FromPtr(ptr);
55+
}
56+
57+
58+
#if LANG_JP
59+
/// <summary>
60+
/// リソースの解放
61+
/// </summary>
62+
/// <param name="disposing">
63+
/// trueの場合は、このメソッドがユーザコードから直接が呼ばれたことを示す。マネージ・アンマネージ双方のリソースが解放される。
64+
/// falseの場合は、このメソッドはランタイムからファイナライザによって呼ばれ、もうほかのオブジェクトから参照されていないことを示す。アンマネージリソースのみ解放される。
65+
///</param>
66+
#else
67+
/// <summary>
68+
/// Releases the resources
69+
/// </summary>
70+
/// <param name="disposing">
71+
/// If disposing equals true, the method has been called directly or indirectly by a user's code. Managed and unmanaged resources can be disposed.
72+
/// If false, the method has been called by the runtime from inside the finalizer and you should not reference other objects. Only unmanaged resources can be disposed.
73+
/// </param>
74+
#endif
75+
protected override void Dispose(bool disposing)
76+
{
77+
if (!disposed)
78+
{
79+
try
80+
{
81+
// releases managed resources
82+
if (disposing)
83+
{
84+
}
85+
// releases unmanaged resources
86+
if (IsEnabledDispose)
87+
{
88+
if (ptrObj != null)
89+
ptrObj.Dispose();
90+
ptrObj = null;
91+
ptr = IntPtr.Zero;
92+
}
93+
disposed = true;
94+
}
95+
finally
96+
{
97+
base.Dispose(disposing);
98+
}
99+
}
100+
}
101+
102+
/// <summary>
103+
/// Pointer to algorithm information (cv::AlgorithmInfo*)
104+
/// </summary>
105+
/// <returns></returns>
106+
public override IntPtr InfoPtr
107+
{
108+
get { return NativeMethods.imgproc_CLAHE_info(ptr); }
109+
}
110+
111+
/// <summary>
112+
///
113+
/// </summary>
114+
/// <param name="src"></param>
115+
/// <param name="dst"></param>
116+
public void Apply(InputArray src, OutputArray dst)
117+
{
118+
if (disposed)
119+
throw new ObjectDisposedException(GetType().Name);
120+
if (src == null)
121+
throw new ArgumentNullException("src");
122+
if (dst == null)
123+
throw new ArgumentNullException("dst");
124+
src.ThrowIfDisposed();
125+
dst.ThrowIfNotReady();
126+
127+
NativeMethods.imgproc_CLAHE_apply(ptr, src.CvPtr, dst.CvPtr);
128+
129+
dst.Fix();
130+
GC.KeepAlive(src);
131+
}
132+
133+
/// <summary>
134+
///
135+
/// </summary>
136+
/// <param name="clipLimit"></param>
137+
public void SetClipLimit(double clipLimit)
138+
{
139+
if (disposed)
140+
throw new ObjectDisposedException(GetType().Name);
141+
142+
NativeMethods.imgproc_CLAHE_setClipLimit(ptr, clipLimit);
143+
}
144+
145+
/// <summary>
146+
///
147+
/// </summary>
148+
/// <returns></returns>
149+
public double GetClipLimit()
150+
{
151+
if (disposed)
152+
throw new ObjectDisposedException(GetType().Name);
153+
154+
return NativeMethods.imgproc_CLAHE_getClipLimit(ptr);
155+
}
156+
157+
/// <summary>
158+
///
159+
/// </summary>
160+
public double ClipLimit
161+
{
162+
get { return GetClipLimit(); }
163+
set { SetClipLimit(value); }
164+
}
165+
166+
/// <summary>
167+
///
168+
/// </summary>
169+
/// <param name="tileGridSize"></param>
170+
public void SetTilesGridSize(Size tileGridSize)
171+
{
172+
if (disposed)
173+
throw new ObjectDisposedException(GetType().Name);
174+
175+
NativeMethods.imgproc_CLAHE_setTilesGridSize(ptr, tileGridSize);
176+
}
177+
178+
/// <summary>
179+
///
180+
/// </summary>
181+
/// <returns></returns>
182+
public Size GetTilesGridSize()
183+
{
184+
if (disposed)
185+
throw new ObjectDisposedException(GetType().Name);
186+
187+
return NativeMethods.imgproc_CLAHE_getTilesGridSize(ptr);
188+
}
189+
190+
/// <summary>
191+
///
192+
/// </summary>
193+
public Size TilesGridSize
194+
{
195+
get { return GetTilesGridSize(); }
196+
set { SetTilesGridSize(value); }
197+
}
198+
199+
200+
/// <summary>
201+
///
202+
/// </summary>
203+
public void CollectGarbage()
204+
{
205+
if (disposed)
206+
throw new ObjectDisposedException(GetType().Name);
207+
208+
NativeMethods.imgproc_CLAHE_collectGarbage(ptr);
209+
}
210+
}
211+
}

src/OpenCvSharp.Sandbox/OpenCvSharp.Sandbox.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@
153153
<Content Include="data\shapes.png">
154154
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
155155
</Content>
156+
<Content Include="data\tsukuba_left.png">
157+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
158+
</Content>
156159
<Content Include="dll\x64\OpenCvSharpExtern.dll">
157160
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
158161
</Content>
@@ -214,7 +217,7 @@
214217
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
215218
</Content>
216219
<Content Include="dll\x86\OpenCvSharpExtern.dll">
217-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
220+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
218221
</Content>
219222
<Content Include="dll\x86\opencv_calib3d249.dll">
220223
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

0 commit comments

Comments
 (0)