/
LearningBasedWB.cs
176 lines (159 loc) · 5.29 KB
/
LearningBasedWB.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using OpenCvSharp.Internal;
namespace OpenCvSharp.XPhoto;
/// <summary>
/// More sophisticated learning-based automatic white balance algorithm.
/// </summary>
// ReSharper disable once InconsistentNaming
public class LearningBasedWB : WhiteBalancer
{
private Ptr? ptrObj;
/// <summary>
/// Constructor
/// </summary>
internal LearningBasedWB(IntPtr p)
{
ptrObj = new Ptr(p);
ptr = ptrObj.Get();
}
/// <summary>
/// Creates an instance of LearningBasedWB
/// </summary>
/// <param name="model">Path to a .yml file with the model. If not specified, the default model is used</param>
/// <returns></returns>
public static LearningBasedWB Create(string? model)
{
NativeMethods.HandleException(
NativeMethods.xphoto_createLearningBasedWB(model ?? "", out var ptr));
return new LearningBasedWB(ptr);
}
/// <inheritdoc />
protected override void DisposeManaged()
{
ptrObj?.Dispose();
ptrObj = null;
base.DisposeManaged();
}
/// <summary>
/// Defines the size of one dimension of a three-dimensional RGB histogram that is used internally by the algorithm. It often makes sense to increase the number of bins for images with higher bit depth (e.g. 256 bins for a 12 bit image).
/// </summary>
public int HistBinNum
{
get
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.xphoto_LearningBasedWB_HistBinNum_get(ptr, out var ret));
GC.KeepAlive(this);
return ret;
}
set
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.xphoto_LearningBasedWB_HistBinNum_set(ptr, value));
GC.KeepAlive(this);
}
}
/// <summary>
/// Maximum possible value of the input image (e.g. 255 for 8 bit images, 4095 for 12 bit images)
/// </summary>
public int RangeMaxVal
{
get
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.xphoto_LearningBasedWB_RangeMaxVal_get(ptr, out var ret));
GC.KeepAlive(this);
return ret;
}
set
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.xphoto_LearningBasedWB_RangeMaxVal_set(ptr, value));
GC.KeepAlive(this);
}
}
/// <summary>
/// Threshold that is used to determine saturated pixels, i.e. pixels where at least one of the channels exceeds
/// </summary>
public float SaturationThreshold
{
get
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.xphoto_LearningBasedWB_SaturationThreshold_get(ptr, out var ret));
GC.KeepAlive(this);
return ret;
}
set
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.xphoto_LearningBasedWB_SaturationThreshold_set(ptr, value));
GC.KeepAlive(this);
}
}
/// <summary>
/// Applies white balancing to the input image.
/// </summary>
/// <param name="src">Input image</param>
/// <param name="dst">White balancing result</param>
public override void BalanceWhite(InputArray src, OutputArray dst)
{
if (src is null)
throw new ArgumentNullException(nameof(src));
if (dst is null)
throw new ArgumentNullException(nameof(dst));
src.ThrowIfDisposed();
dst.ThrowIfNotReady();
NativeMethods.HandleException(
NativeMethods.xphoto_LearningBasedWB_balanceWhite(ptr, src.CvPtr, dst.CvPtr));
GC.KeepAlive(this);
GC.KeepAlive(src);
GC.KeepAlive(dst);
dst.Fix();
}
/// <summary>
/// Implements the feature extraction part of the algorithm.
/// </summary>
/// <param name="src">Input three-channel image (BGR color space is assumed).</param>
/// <param name="dst">An array of four (r,g) chromaticity tuples corresponding to the features listed above.</param>
public void ExtractSimpleFeatures(InputArray src, OutputArray dst)
{
if (src is null)
throw new ArgumentNullException(nameof(src));
if (dst is null)
throw new ArgumentNullException(nameof(dst));
src.ThrowIfDisposed();
dst.ThrowIfNotReady();
NativeMethods.HandleException(
NativeMethods.xphoto_LearningBasedWB_extractSimpleFeatures(ptr, src.CvPtr, dst.CvPtr));
GC.KeepAlive(this);
GC.KeepAlive(src);
GC.KeepAlive(dst);
dst.Fix();
}
internal class Ptr : OpenCvSharp.Ptr
{
public Ptr(IntPtr ptr)
: base(ptr)
{
}
public override IntPtr Get()
{
NativeMethods.HandleException(
NativeMethods.xphoto_Ptr_LearningBasedWB_get(ptr, out var ret));
GC.KeepAlive(this);
return ret;
}
protected override void DisposeUnmanaged()
{
NativeMethods.HandleException(
NativeMethods.xphoto_Ptr_LearningBasedWB_delete(ptr));
base.DisposeUnmanaged();
}
}
}