Skip to content

Commit bbab2a7

Browse files
shimatclaude
andcommitted
Fix all build warnings (CA/CS analyzers) across src and tests
Eliminates every warning surfaced by a clean Release build (174 -> 0): - CA2000: split chained `LoadImage(...)[rect]`/`new IndexParams()` expressions into separate `using` declarations so every disposable is tracked - CA1861: hoist constant array literals passed to repeatedly-invoked test helpers into static readonly fields - CA5394: mark test files that use seeded `Random` for synthetic data as intentionally non-cryptographic (matches existing CoreTest.cs convention) - CS8602/CS8604: add the missing null checks that StereoCalibrate's InputArray overload and FindHomography/FindFundamentalMat's mask parameter should have had, and widen InputArray's Mat conversion/factory to accept null (mirroring the existing nullable-Mat constructor) so passing a null Mat for an optional InputArray? parameter no longer looks unsafe - CS1573: document the AlgorithmHint `hint` parameter added to CvtColor/CvtColorTwoPlane/GaussianBlur/Remap/WarpAffine/WarpPerspective - CS1591: suppress doc-comment requirements on the internal blittable FacemarkAAM/LBF ParamsData marshalling structs (same pattern already used for CvEdgeDrawingParams) - CA1859/CA1805/CA1069/CA1000: narrow InputArray's owned-disposable field to Mat, drop a redundant default-value initializer, and suppress the two known-intentional false positives (PutTextFlags' dual-axis bit flags, SparseMat<T>'s generic static factory) Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent a5b0372 commit bbab2a7

23 files changed

Lines changed: 114 additions & 47 deletions

src/OpenCvSharp/Cv2/Cv2_calib.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,14 @@ public static double StereoCalibrate(
570570
throw new ArgumentNullException(nameof(cameraMatrix2));
571571
if (distCoeffs2 is null)
572572
throw new ArgumentNullException(nameof(distCoeffs2));
573+
if (R is null)
574+
throw new ArgumentNullException(nameof(R));
575+
if (T is null)
576+
throw new ArgumentNullException(nameof(T));
577+
if (E is null)
578+
throw new ArgumentNullException(nameof(E));
579+
if (F is null)
580+
throw new ArgumentNullException(nameof(F));
573581
cameraMatrix1.ThrowIfDisposed();
574582
distCoeffs1.ThrowIfDisposed();
575583
cameraMatrix2.ThrowIfDisposed();
@@ -592,7 +600,7 @@ public static double StereoCalibrate(
592600
ip1Ptrs, ip1Ptrs.Length, ip2Ptrs, ip2Ptrs.Length,
593601
cameraMatrix1.ToInputOutputProxy(), distCoeffs1.ToInputOutputProxy(),
594602
cameraMatrix2.ToInputOutputProxy(), distCoeffs2.ToInputOutputProxy(),
595-
imageSize, R?.ToOutputProxy() ?? default, T?.ToOutputProxy() ?? default, E?.ToOutputProxy() ?? default, F?.ToOutputProxy() ?? default,
603+
imageSize, R.ToOutputProxy(), T.ToOutputProxy(), E.ToOutputProxy(), F.ToOutputProxy(),
596604
(int) flags, criteria0, out var ret));
597605

598606
GC.KeepAlive(cameraMatrix1);

src/OpenCvSharp/Cv2/Cv2_geometry.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public static Mat FindHomography(
195195
var p = (@params ?? new UsacParams()).ToNativeStruct();
196196
NativeMethods.HandleException(
197197
NativeMethods.geometry_findHomography_UsacParams(
198-
srcPoints.ToInputProxy(), dstPoints.ToInputProxy(), mask?.ToOutputProxy() ?? default, ref p,
198+
srcPoints.ToInputProxy(), dstPoints.ToInputProxy(), mask.ToOutputProxy(), ref p,
199199
out var ret));
200200

201201
GC.KeepAlive(srcPoints);
@@ -1432,7 +1432,7 @@ public static Mat FindFundamentalMat(InputArray points1, InputArray points2, Out
14321432
var p = (@params ?? new UsacParams()).ToNativeStruct();
14331433
NativeMethods.HandleException(
14341434
NativeMethods.geometry_findFundamentalMat_UsacParams(
1435-
points1.ToInputProxy(), points2.ToInputProxy(), mask?.ToOutputProxy() ?? default, ref p, out var ret));
1435+
points1.ToInputProxy(), points2.ToInputProxy(), mask.ToOutputProxy(), ref p, out var ret));
14361436

14371437
GC.KeepAlive(points1);
14381438
GC.KeepAlive(points2);

src/OpenCvSharp/Cv2/Cv2_imgproc.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public static void MedianBlur(InputArray src, OutputArray dst, int ksize)
156156
/// respectively (see getGaussianKernel() for details); to fully control the result
157157
/// regardless of possible future modifications of all this semantics, it is recommended to specify all of ksize, sigmaX, and sigmaY.</param>
158158
/// <param name="borderType">pixel extrapolation method</param>
159+
/// <param name="hint">Hint that selects between alternative algorithm implementations (OpenCV 5).</param>
159160
public static void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX,
160161
double sigmaY = 0, BorderTypes borderType = BorderTypes.Default, AlgorithmHint hint = AlgorithmHint.Default)
161162
{
@@ -1051,6 +1052,7 @@ public static void Resize(InputArray src, OutputArray dst, Size dsize,
10511052
/// it means that the pixels in the destination image corresponding to the "outliers"
10521053
/// in the source image are not modified by the function.</param>
10531054
/// <param name="borderValue">value used in case of a constant border; by default, it is 0.</param>
1055+
/// <param name="hint">Hint that selects between alternative algorithm implementations (OpenCV 5).</param>
10541056
public static void WarpAffine(
10551057
InputArray src, OutputArray dst, InputArray m, Size dsize,
10561058
InterpolationFlags flags = InterpolationFlags.Linear,
@@ -1088,6 +1090,7 @@ public static void WarpAffine(
10881090
/// and the optional flag WARP_INVERSE_MAP, that sets M as the inverse transformation (dst -> src).</param>
10891091
/// <param name="borderMode">pixel extrapolation method (BORDER_CONSTANT or BORDER_REPLICATE).</param>
10901092
/// <param name="borderValue">value used in case of a constant border; by default, it equals 0.</param>
1093+
/// <param name="hint">Hint that selects between alternative algorithm implementations (OpenCV 5).</param>
10911094
public static void WarpPerspective(
10921095
InputArray src, OutputArray dst, InputArray m, Size dsize,
10931096
InterpolationFlags flags = InterpolationFlags.Linear,
@@ -1127,6 +1130,7 @@ public static void WarpPerspective(
11271130
/// and the optional flag WARP_INVERSE_MAP, that sets M as the inverse transformation (dst -> src).</param>
11281131
/// <param name="borderMode">pixel extrapolation method (BORDER_CONSTANT or BORDER_REPLICATE).</param>
11291132
/// <param name="borderValue">value used in case of a constant border; by default, it equals 0.</param>
1133+
/// <param name="hint">Hint that selects between alternative algorithm implementations (OpenCV 5).</param>
11301134
public static void WarpPerspective(
11311135
InputArray src, OutputArray dst, float[,] m, Size dsize,
11321136
InterpolationFlags flags = InterpolationFlags.Linear,
@@ -1168,6 +1172,7 @@ public static void WarpPerspective(
11681172
/// it means that the pixels in the destination image that corresponds to the "outliers" in
11691173
/// the source image are not modified by the function.</param>
11701174
/// <param name="borderValue">Value used in case of a constant border. By default, it is 0.</param>
1175+
/// <param name="hint">Hint that selects between alternative algorithm implementations (OpenCV 5).</param>
11711176
public static void Remap(
11721177
InputArray src, OutputArray dst, InputArray map1, InputArray map2,
11731178
InterpolationFlags interpolation = InterpolationFlags.Linear,
@@ -2430,6 +2435,7 @@ public static void BlendLinear(InputArray src1, InputArray src2, InputArray weig
24302435
/// <param name="dst">The destination image; will have the same size and the same depth as src</param>
24312436
/// <param name="code">The color space conversion code</param>
24322437
/// <param name="dstCn">The number of channels in the destination image; if the parameter is 0, the number of the channels will be derived automatically from src and the code</param>
2438+
/// <param name="hint">Hint that selects between alternative algorithm implementations (OpenCV 5).</param>
24332439
public static void CvtColor(InputArray src, OutputArray dst, ColorConversionCodes code, int dstCn = 0,
24342440
AlgorithmHint hint = AlgorithmHint.Default)
24352441
{
@@ -2464,6 +2470,7 @@ public static void CvtColor(InputArray src, OutputArray dst, ColorConversionCode
24642470
/// - #COLOR_YUV2RGB_NV21
24652471
/// - #COLOR_YUV2BGRA_NV21
24662472
/// - #COLOR_YUV2RGBA_NV21</param>
2473+
/// <param name="hint">Hint that selects between alternative algorithm implementations (OpenCV 5).</param>
24672474
public static void CvtColorTwoPlane(InputArray src1, InputArray src2, OutputArray dst, ColorConversionCodes code,
24682475
AlgorithmHint hint = AlgorithmHint.Default)
24692476
{

src/OpenCvSharp/Internal/PInvoke/NativeMethods/face/FacemarkParamsData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Runtime.InteropServices;
22

3+
#pragma warning disable 1591
4+
35
namespace OpenCvSharp.Internal;
46

57
/// <summary>

src/OpenCvSharp/Modules/core/InputArray.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class InputArray : CvObject
1616

1717
// A resource materialized by this InputArray itself (e.g. the Mat produced from a
1818
// MatExpr). Unlike 'obj', which is merely kept alive, this is disposed with the InputArray.
19-
private IDisposable? ownedDisposable;
19+
private Mat? ownedDisposable;
2020

2121
#pragma warning disable 1591
2222
// ReSharper disable InconsistentNaming
@@ -306,7 +306,7 @@ protected override void DisposeManaged()
306306
/// </summary>
307307
/// <param name="mat"></param>
308308
/// <returns></returns>
309-
public static InputArray Create(Mat mat) => new(mat);
309+
public static InputArray Create(Mat? mat) => new(mat);
310310

311311
/// <summary>
312312
/// Creates a proxy class of the specified Mat
@@ -609,7 +609,7 @@ private static MatType EstimateType(Type t)
609609
#pragma warning disable 1591
610610
#pragma warning disable CA2225
611611

612-
public static implicit operator InputArray(Mat mat) => Create(mat);
612+
public static implicit operator InputArray(Mat? mat) => Create(mat);
613613

614614
public static implicit operator InputArray(UMat mat) => Create(mat);
615615

src/OpenCvSharp/Modules/core/SparseMat.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,9 @@ internal static SparseMat<T> WrapShared(SparseMat source)
469469
/// Creates a typed sparse matrix from a dense <see cref="Mat"/>. The Mat type must match
470470
/// <typeparamref name="T"/>.
471471
/// </summary>
472+
#pragma warning disable CA1000 // Do not declare static members on generic types
472473
public static new SparseMat<T> FromMat(Mat mat)
474+
#pragma warning restore CA1000
473475
{
474476
if (mat is null)
475477
throw new ArgumentNullException(nameof(mat));

src/OpenCvSharp/Modules/imgproc/Enum/PutTextFlags.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public enum PutTextFlags
3030
/// <summary>
3131
/// Treat the target image as having a top-left origin (default).
3232
/// </summary>
33+
#pragma warning disable CA1069 // Intentional: shares 0 with AlignLeft, an independent bitfield (alignment vs. origin), mirroring cv::PutTextFlags.
3334
OriginTL = 0,
35+
#pragma warning restore CA1069
3436

3537
/// <summary>
3638
/// Treat the target image as having a bottom-left origin.

src/OpenCvSharp/Modules/imgproc/Filter2DParams.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Filter2DParams
2424
/// <summary>
2525
/// Border value used in case of a constant border.
2626
/// </summary>
27-
public Scalar BorderValue { get; set; } = new Scalar();
27+
public Scalar BorderValue { get; set; }
2828

2929
/// <summary>
3030
/// Desired depth of the destination image. -1 means the same depth as the source.

test/OpenCvSharp.Tests/calib3d/FishEyeTest.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ namespace OpenCvSharp.Tests.Calib3D;
66
// otherwise exercised (issue #1976 follow-up: every migrated method needs >=1 test).
77
public class FishEyeTest : TestBase
88
{
9-
private static Mat CameraMatrix() => Mat.FromPixelData(3, 3, MatType.CV_64FC1, new double[]
9+
private static readonly double[] CameraMatrixValues =
1010
{
1111
300, 0, 160,
1212
0, 300, 120,
1313
0, 0, 1
14-
});
14+
};
1515

16-
private static Mat DistCoeffs() => Mat.FromPixelData(4, 1, MatType.CV_64FC1, new[] { 0.1, 0.01, 0.0, 0.0 });
16+
private static readonly double[] DistCoeffsValues = { 0.1, 0.01, 0.0, 0.0 };
17+
18+
private static Mat CameraMatrix() => Mat.FromPixelData(3, 3, MatType.CV_64FC1, CameraMatrixValues);
19+
20+
private static Mat DistCoeffs() => Mat.FromPixelData(4, 1, MatType.CV_64FC1, DistCoeffsValues);
1721

1822
[Fact]
1923
public void UndistortPoints()

test/OpenCvSharp.Tests/calib3d/GeometryFunctionsTest.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Xunit;
22

3+
#pragma warning disable CA5394 // Do not use insecure randomness
4+
35
namespace OpenCvSharp.Tests.Calib3D;
46

57
// Tests for the OpenCV 5 geometry/calib functions added to OpenCvSharp:
@@ -20,6 +22,11 @@ public class GeometryFunctionsTest : TestBase
2022
new Point3f(0, 0, 1), new Point3f(1, 0, 1), new Point3f(0, 1, 1), new Point3f(1, 1, 1)
2123
};
2224

25+
private static readonly double[] UnitTranslationX = { 1.0, 0.0, 0.0 };
26+
private static readonly double[] UnitTranslationY = { 0.0, 1.0, 0.0 };
27+
private static readonly double[] UnitPoint3D = { 1.0, 1.0, 1.0 };
28+
private static readonly double[] SmallDistCoeffs = { 0.1, 0.01, 0.0, 0.0 };
29+
2330
[Theory]
2431
[InlineData(false)]
2532
[InlineData(true)]
@@ -165,9 +172,9 @@ public void ComposeRT()
165172
{
166173
// Two pure translations (no rotation) compose by simply adding the translations.
167174
using var rvec1 = Mat.ZerosMat(3, 1, MatType.CV_64FC1);
168-
using var tvec1 = Mat.FromPixelData(3, 1, MatType.CV_64FC1, new[] { 1.0, 0.0, 0.0 });
175+
using var tvec1 = Mat.FromPixelData(3, 1, MatType.CV_64FC1, UnitTranslationX);
169176
using var rvec2 = Mat.ZerosMat(3, 1, MatType.CV_64FC1);
170-
using var tvec2 = Mat.FromPixelData(3, 1, MatType.CV_64FC1, new[] { 0.0, 1.0, 0.0 });
177+
using var tvec2 = Mat.FromPixelData(3, 1, MatType.CV_64FC1, UnitTranslationY);
171178
using var rvec3 = new Mat();
172179
using var tvec3 = new Mat();
173180

@@ -482,8 +489,8 @@ public void RQDecomp3x3()
482489
[Fact]
483490
public void SampsonDistance()
484491
{
485-
using var pt1 = Mat.FromPixelData(1, 1, MatType.CV_64FC3, new[] { 1.0, 1.0, 1.0 });
486-
using var pt2 = Mat.FromPixelData(1, 1, MatType.CV_64FC3, new[] { 1.0, 1.0, 1.0 });
492+
using var pt1 = Mat.FromPixelData(1, 1, MatType.CV_64FC3, UnitPoint3D);
493+
using var pt2 = Mat.FromPixelData(1, 1, MatType.CV_64FC3, UnitPoint3D);
487494
using var f = Mat.FromPixelData(3, 3, MatType.CV_64FC1, new double[]
488495
{
489496
0, 0, 0,
@@ -597,7 +604,7 @@ public void FishEyeDistortPointsWithUndistortedMatrix()
597604
0, 0, 1
598605
});
599606
using var kUndistorted = k.Clone();
600-
using var d = Mat.FromPixelData(4, 1, MatType.CV_64FC1, new[] { 0.1, 0.01, 0.0, 0.0 });
607+
using var d = Mat.FromPixelData(4, 1, MatType.CV_64FC1, SmallDistCoeffs);
601608

602609
Cv2.FishEye.DistortPoints(undistorted, distorted, kUndistorted, k, d);
603610

0 commit comments

Comments
 (0)