Skip to content

Commit 70d5816

Browse files
authored
Merge pull request #1404 from shimat/fastnlmeansdenoising_fix
Fix FastNlMeansDenosing problem
2 parents 62aad01 + 67c1c0d commit 70d5816

File tree

4 files changed

+118
-119
lines changed

4 files changed

+118
-119
lines changed

src/OpenCvSharp/Cv2/Cv2_photo.cs

+37-105
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ static partial class Cv2
1818
public static void Inpaint(InputArray src, InputArray inpaintMask,
1919
OutputArray dst, double inpaintRadius, InpaintMethod flags)
2020
{
21-
if (src == null)
21+
if (src is null)
2222
throw new ArgumentNullException(nameof(src));
23-
if (inpaintMask == null)
23+
if (inpaintMask is null)
2424
throw new ArgumentNullException(nameof(inpaintMask));
25-
if (dst == null)
25+
if (dst is null)
2626
throw new ArgumentNullException(nameof(dst));
2727
src.ThrowIfDisposed();
2828
inpaintMask.ThrowIfDisposed();
@@ -53,9 +53,9 @@ public static void Inpaint(InputArray src, InputArray inpaintMask,
5353
public static void FastNlMeansDenoising(InputArray src, OutputArray dst, float h = 3,
5454
int templateWindowSize = 7, int searchWindowSize = 21)
5555
{
56-
if (src == null)
56+
if (src is null)
5757
throw new ArgumentNullException(nameof(src));
58-
if (dst == null)
58+
if (dst is null)
5959
throw new ArgumentNullException(nameof(dst));
6060
src.ThrowIfDisposed();
6161
dst.ThrowIfNotReady();
@@ -85,9 +85,9 @@ public static void FastNlMeansDenoisingColored(InputArray src, OutputArray dst,
8585
float h = 3, float hColor = 3,
8686
int templateWindowSize = 7, int searchWindowSize = 21)
8787
{
88-
if (src == null)
88+
if (src is null)
8989
throw new ArgumentNullException(nameof(src));
90-
if (dst == null)
90+
if (dst is null)
9191
throw new ArgumentNullException(nameof(dst));
9292
src.ThrowIfDisposed();
9393
dst.ThrowIfNotReady();
@@ -115,14 +115,15 @@ public static void FastNlMeansDenoisingColored(InputArray src, OutputArray dst,
115115
/// <param name="searchWindowSize">Size in pixels of the window that is used to compute weighted average for given pixel.
116116
/// Should be odd. Affect performance linearly: greater searchWindowsSize - greater denoising time. Recommended value 21 pixels</param>
117117
public static void FastNlMeansDenoisingMulti(
118-
IEnumerable<InputArray> srcImgs, OutputArray dst,
118+
IEnumerable<Mat> srcImgs, OutputArray dst,
119119
int imgToDenoiseIndex, int temporalWindowSize,
120120
float h = 3, int templateWindowSize = 7, int searchWindowSize = 21)
121121
{
122-
if (srcImgs == null)
122+
if (srcImgs is null)
123123
throw new ArgumentNullException(nameof(srcImgs));
124-
if (dst == null)
124+
if (dst is null)
125125
throw new ArgumentNullException(nameof(dst));
126+
126127
dst.ThrowIfNotReady();
127128
var srcImgPtrs = srcImgs.Select(x => x.CvPtr).ToArray();
128129

@@ -136,41 +137,6 @@ public static void FastNlMeansDenoisingMulti(
136137
GC.KeepAlive(srcImgs);
137138
}
138139

139-
/// <summary>
140-
/// Modification of fastNlMeansDenoising function for images sequence where consequtive images have been captured
141-
/// in small period of time. For example video. This version of the function is for grayscale images or for manual manipulation with colorspaces.
142-
/// </summary>
143-
/// <param name="srcImgs">Input 8-bit 1-channel, 2-channel or 3-channel images sequence. All images should have the same type and size.</param>
144-
/// <param name="dst"> Output image with the same size and type as srcImgs images.</param>
145-
/// <param name="imgToDenoiseIndex">Target image to denoise index in srcImgs sequence</param>
146-
/// <param name="temporalWindowSize">Number of surrounding images to use for target image denoising.
147-
/// Should be odd. Images from imgToDenoiseIndex - temporalWindowSize / 2 to imgToDenoiseIndex - temporalWindowSize / 2
148-
/// from srcImgs will be used to denoise srcImgs[imgToDenoiseIndex] image.</param>
149-
/// <param name="h">Parameter regulating filter strength for luminance component. Bigger h value perfectly removes noise but also removes image details,
150-
/// smaller h value preserves details but also preserves some noise</param>
151-
/// <param name="templateWindowSize">Size in pixels of the template patch that is used to compute weights. Should be odd. Recommended value 7 pixels</param>
152-
/// <param name="searchWindowSize">Size in pixels of the window that is used to compute weighted average for given pixel.
153-
/// Should be odd. Affect performance linearly: greater searchWindowsSize - greater denoising time. Recommended value 21 pixels</param>
154-
public static void FastNlMeansDenoisingMulti(
155-
IEnumerable<Mat> srcImgs, OutputArray dst,
156-
int imgToDenoiseIndex, int temporalWindowSize,
157-
float h = 3, int templateWindowSize = 7, int searchWindowSize = 21)
158-
{
159-
var srcImgsAsArrays = srcImgs.Select(m => new InputArray(m)).ToArray();
160-
try
161-
{
162-
FastNlMeansDenoisingMulti(srcImgsAsArrays, dst, imgToDenoiseIndex, temporalWindowSize,
163-
h, templateWindowSize, searchWindowSize);
164-
}
165-
finally
166-
{
167-
foreach (var img in srcImgsAsArrays)
168-
{
169-
img.Dispose();
170-
}
171-
}
172-
}
173-
174140
/// <summary>
175141
/// Modification of fastNlMeansDenoisingMulti function for colored images sequences
176142
/// </summary>
@@ -187,13 +153,13 @@ public static void FastNlMeansDenoisingMulti(
187153
/// <param name="searchWindowSize">Size in pixels of the window that is used to compute weighted average for given pixel.
188154
/// Should be odd. Affect performance linearly: greater searchWindowsSize - greater denoising time. Recommended value 21 pixels</param>
189155
public static void FastNlMeansDenoisingColoredMulti(
190-
IEnumerable<InputArray> srcImgs, OutputArray dst,
156+
IEnumerable<Mat> srcImgs, OutputArray dst,
191157
int imgToDenoiseIndex, int temporalWindowSize, float h = 3, float hColor = 3,
192158
int templateWindowSize = 7, int searchWindowSize = 21)
193159
{
194-
if (srcImgs == null)
160+
if (srcImgs is null)
195161
throw new ArgumentNullException(nameof(srcImgs));
196-
if (dst == null)
162+
if (dst is null)
197163
throw new ArgumentNullException(nameof(dst));
198164
dst.ThrowIfNotReady();
199165
var srcImgPtrs = srcImgs.Select(x => x.CvPtr).ToArray();
@@ -207,40 +173,6 @@ public static void FastNlMeansDenoisingColoredMulti(
207173
GC.KeepAlive(srcImgs);
208174
}
209175

210-
/// <summary>
211-
/// Modification of fastNlMeansDenoisingMulti function for colored images sequences
212-
/// </summary>
213-
/// <param name="srcImgs">Input 8-bit 3-channel images sequence. All images should have the same type and size.</param>
214-
/// <param name="dst">Output image with the same size and type as srcImgs images.</param>
215-
/// <param name="imgToDenoiseIndex">Target image to denoise index in srcImgs sequence</param>
216-
/// <param name="temporalWindowSize">Number of surrounding images to use for target image denoising. Should be odd.
217-
/// Images from imgToDenoiseIndex - temporalWindowSize / 2 to imgToDenoiseIndex - temporalWindowSize / 2 from srcImgs
218-
/// will be used to denoise srcImgs[imgToDenoiseIndex] image.</param>
219-
/// <param name="h">Parameter regulating filter strength for luminance component. Bigger h value perfectly removes noise
220-
/// but also removes image details, smaller h value preserves details but also preserves some noise.</param>
221-
/// <param name="hColor"> The same as h but for color components.</param>
222-
/// <param name="templateWindowSize">Size in pixels of the template patch that is used to compute weights. Should be odd. Recommended value 7 pixels</param>
223-
/// <param name="searchWindowSize">Size in pixels of the window that is used to compute weighted average for given pixel.
224-
/// Should be odd. Affect performance linearly: greater searchWindowsSize - greater denoising time. Recommended value 21 pixels</param>
225-
public static void FastNlMeansDenoisingColoredMulti(IEnumerable<Mat> srcImgs, OutputArray dst,
226-
int imgToDenoiseIndex, int temporalWindowSize, float h = 3, float hColor = 3,
227-
int templateWindowSize = 7, int searchWindowSize = 21)
228-
{
229-
var srcImgsAsArrays = srcImgs.Select(m => new InputArray(m)).ToArray();
230-
try
231-
{
232-
FastNlMeansDenoisingColoredMulti(
233-
srcImgsAsArrays, dst, imgToDenoiseIndex, temporalWindowSize, h, hColor, templateWindowSize, searchWindowSize);
234-
}
235-
finally
236-
{
237-
foreach (var img in srcImgsAsArrays)
238-
{
239-
img.Dispose();
240-
}
241-
}
242-
}
243-
244176
/// <summary>
245177
/// Primal-dual algorithm is an algorithm for solving special types of variational problems
246178
/// (that is, finding a function to minimize some functional). As the image denoising,
@@ -262,9 +194,9 @@ public static void FastNlMeansDenoisingColoredMulti(IEnumerable<Mat> srcImgs, Ou
262194
public static void DenoiseTVL1(
263195
IEnumerable<Mat> observations, Mat result, double lambda = 1.0, int niters = 30)
264196
{
265-
if (observations == null)
197+
if (observations is null)
266198
throw new ArgumentNullException(nameof(observations));
267-
if (result == null)
199+
if (result is null)
268200
throw new ArgumentNullException(nameof(result));
269201

270202
var observationsPtrs = observations.Select(x => x.CvPtr).ToArray();
@@ -285,11 +217,11 @@ public static void DenoiseTVL1(
285217
public static void Decolor(
286218
InputArray src, OutputArray grayscale, OutputArray colorBoost)
287219
{
288-
if (src == null)
220+
if (src is null)
289221
throw new ArgumentNullException(nameof(src));
290-
if (grayscale == null)
222+
if (grayscale is null)
291223
throw new ArgumentNullException(nameof(grayscale));
292-
if (colorBoost == null)
224+
if (colorBoost is null)
293225
throw new ArgumentNullException(nameof(colorBoost));
294226
src.ThrowIfDisposed();
295227
grayscale.ThrowIfNotReady();
@@ -321,11 +253,11 @@ public static void SeamlessClone(
321253
InputArray src, InputArray dst, InputArray? mask, Point p,
322254
OutputArray blend, SeamlessCloneMethods flags)
323255
{
324-
if (src == null)
256+
if (src is null)
325257
throw new ArgumentNullException(nameof(src));
326-
if (dst == null)
258+
if (dst is null)
327259
throw new ArgumentNullException(nameof(dst));
328-
if (blend == null)
260+
if (blend is null)
329261
throw new ArgumentNullException(nameof(blend));
330262
src.ThrowIfDisposed();
331263
dst.ThrowIfDisposed();
@@ -356,9 +288,9 @@ public static void ColorChange(
356288
InputArray src, InputArray? mask, OutputArray dst,
357289
float redMul = 1.0f, float greenMul = 1.0f, float blueMul = 1.0f)
358290
{
359-
if (src == null)
291+
if (src is null)
360292
throw new ArgumentNullException(nameof(src));
361-
if (dst == null)
293+
if (dst is null)
362294
throw new ArgumentNullException(nameof(dst));
363295
src.ThrowIfDisposed();
364296
dst.ThrowIfNotReady();
@@ -390,9 +322,9 @@ public static void IlluminationChange(
390322
InputArray src, InputArray? mask, OutputArray dst,
391323
float alpha = 0.2f, float beta = 0.4f)
392324
{
393-
if (src == null)
325+
if (src is null)
394326
throw new ArgumentNullException(nameof(src));
395-
if (dst == null)
327+
if (dst is null)
396328
throw new ArgumentNullException(nameof(dst));
397329

398330
src.ThrowIfDisposed();
@@ -424,9 +356,9 @@ public static void TextureFlattening(
424356
float lowThreshold = 30, float highThreshold = 45,
425357
int kernelSize = 3)
426358
{
427-
if (src == null)
359+
if (src is null)
428360
throw new ArgumentNullException(nameof(src));
429-
if (dst == null)
361+
if (dst is null)
430362
throw new ArgumentNullException(nameof(dst));
431363

432364
src.ThrowIfDisposed();
@@ -456,9 +388,9 @@ public static void EdgePreservingFilter(
456388
EdgePreservingMethods flags = EdgePreservingMethods.RecursFilter,
457389
float sigmaS = 60, float sigmaR = 0.4f)
458390
{
459-
if (src == null)
391+
if (src is null)
460392
throw new ArgumentNullException(nameof(src));
461-
if (dst == null)
393+
if (dst is null)
462394
throw new ArgumentNullException(nameof(dst));
463395

464396
src.ThrowIfDisposed();
@@ -483,9 +415,9 @@ public static void DetailEnhance(
483415
InputArray src, OutputArray dst,
484416
float sigmaS = 10, float sigmaR = 0.15f)
485417
{
486-
if (src == null)
418+
if (src is null)
487419
throw new ArgumentNullException(nameof(src));
488-
if (dst == null)
420+
if (dst is null)
489421
throw new ArgumentNullException(nameof(dst));
490422

491423
src.ThrowIfDisposed();
@@ -512,11 +444,11 @@ public static void PencilSketch(
512444
InputArray src, OutputArray dst1, OutputArray dst2,
513445
float sigmaS = 60, float sigmaR = 0.07f, float shadeFactor = 0.02f)
514446
{
515-
if (src == null)
447+
if (src is null)
516448
throw new ArgumentNullException(nameof(src));
517-
if (dst1 == null)
449+
if (dst1 is null)
518450
throw new ArgumentNullException(nameof(dst1));
519-
if (dst2 == null)
451+
if (dst2 is null)
520452
throw new ArgumentNullException(nameof(dst2));
521453

522454
src.ThrowIfDisposed();
@@ -546,9 +478,9 @@ public static void Stylization(
546478
InputArray src, OutputArray dst,
547479
float sigmaS = 60, float sigmaR = 0.45f)
548480
{
549-
if (src == null)
481+
if (src is null)
550482
throw new ArgumentNullException(nameof(src));
551-
if (dst == null)
483+
if (dst is null)
552484
throw new ArgumentNullException(nameof(dst));
553485

554486
src.ThrowIfDisposed();

src/OpenCvSharp/Internal/PInvoke/NativeMethods/photo/NativeMethods_photo.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,25 @@ namespace OpenCvSharp.Internal
1212
static partial class NativeMethods
1313
{
1414
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
15-
public static extern ExceptionStatus photo_inpaint(IntPtr src, IntPtr inpaintMask,
15+
public static extern ExceptionStatus photo_inpaint(
16+
IntPtr src, IntPtr inpaintMask,
1617
IntPtr dst, double inpaintRadius, int flags);
1718

1819
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
19-
public static extern ExceptionStatus photo_fastNlMeansDenoising(IntPtr src, IntPtr dst, float h,
20+
public static extern ExceptionStatus photo_fastNlMeansDenoising(
21+
IntPtr src, IntPtr dst, float h,
2022
int templateWindowSize, int searchWindowSize);
2123

2224
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
23-
public static extern ExceptionStatus photo_fastNlMeansDenoisingColored(IntPtr src, IntPtr dst,
25+
public static extern ExceptionStatus photo_fastNlMeansDenoisingColored(
26+
IntPtr src, IntPtr dst,
2427
float h, float hColor, int templateWindowSize, int searchWindowSize);
2528

2629
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
27-
public static extern ExceptionStatus photo_fastNlMeansDenoisingMulti(IntPtr[] srcImgs, int srcImgsLength,
28-
IntPtr dst, int imgToDenoiseIndex, int temporalWindowSize,
29-
float h, int templateWindowSize, int searchWindowSize);
30+
public static extern ExceptionStatus photo_fastNlMeansDenoisingMulti(
31+
IntPtr[] srcImgs, int srcImgsLength,
32+
IntPtr dst, int imgToDenoiseIndex, int temporalWindowSize,
33+
float h, int templateWindowSize, int searchWindowSize);
3034

3135
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
3236
public static extern ExceptionStatus photo_fastNlMeansDenoisingColoredMulti(IntPtr[] srcImgs, int srcImgsLength,

src/OpenCvSharpExtern/photo.h

+16-8
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,35 @@ CVAPI(ExceptionStatus) photo_fastNlMeansDenoisingColored(cv::_InputArray *src, c
3131
END_WRAP
3232
}
3333

34-
CVAPI(ExceptionStatus) photo_fastNlMeansDenoisingMulti(cv::_InputArray ** srcImgs, int srcImgsLength,
35-
cv::_OutputArray *dst, int imgToDenoiseIndex, int temporalWindowSize,
34+
CVAPI(ExceptionStatus) photo_fastNlMeansDenoisingMulti(cv::Mat **srcImgs, int srcImgsLength,
35+
cv::_OutputArray* dst, int imgToDenoiseIndex, int temporalWindowSize,
3636
float h, int templateWindowSize, int searchWindowSize)
3737
{
3838
BEGIN_WRAP
39-
std::vector<cv::_InputArray> srcImgsVec(srcImgsLength);
40-
for (int i = 0; i < srcImgsLength; i++)
39+
40+
std::vector<cv::Mat> srcImgsVec;
41+
for (int i = 0; i < srcImgsLength; i++)
4142
srcImgsVec[i] = *srcImgs[i];
42-
cv::fastNlMeansDenoisingMulti(srcImgsVec, *dst, imgToDenoiseIndex, temporalWindowSize, h, templateWindowSize, searchWindowSize);
43+
44+
cv::fastNlMeansDenoisingMulti(
45+
srcImgsVec, *dst, imgToDenoiseIndex, temporalWindowSize, h, templateWindowSize, searchWindowSize);
46+
4347
END_WRAP
4448
}
4549

46-
CVAPI(ExceptionStatus) photo_fastNlMeansDenoisingColoredMulti(cv::_InputArray **srcImgs, int srcImgsLength,
50+
CVAPI(ExceptionStatus) photo_fastNlMeansDenoisingColoredMulti(cv::Mat**srcImgs, int srcImgsLength,
4751
cv::_OutputArray *dst, int imgToDenoiseIndex, int temporalWindowSize,
4852
float h, float hColor, int templateWindowSize, int searchWindowSize)
4953
{
5054
BEGIN_WRAP
51-
std::vector<cv::_InputArray> srcImgsVec(srcImgsLength);
55+
56+
std::vector<cv::Mat> srcImgsVec(srcImgsLength);
5257
for (int i = 0; i < srcImgsLength; i++)
5358
srcImgsVec[i] = *srcImgs[i];
54-
cv::fastNlMeansDenoisingColoredMulti(srcImgsVec, *dst, imgToDenoiseIndex, temporalWindowSize, h, hColor, templateWindowSize, searchWindowSize);
59+
60+
cv::fastNlMeansDenoisingColoredMulti(
61+
srcImgsVec, *dst, imgToDenoiseIndex, temporalWindowSize, h, hColor, templateWindowSize, searchWindowSize);
62+
5563
END_WRAP
5664
}
5765

0 commit comments

Comments
 (0)