Skip to content

Commit dac6456

Browse files
authored
Fix more enums carrying stale native values from the OpenCV4->5 migration (#2082)
Auditing for the same class of bug as #2080 (SolvePnPMethod) turned up two more live cases where a managed enum's numeric values no longer match their native OpenCV5 counterpart: - FishEyeCalibrationFlags: cv::fisheye lost its own flag enum in OpenCV5 and now aliases the unified CALIB_* flags, whose bit positions are unrelated to the old fisheye-only values. Every flag except UseIntrinsicGuess/FixIntrinsic (which happened to keep the same bit) was silently setting the wrong native bits. - WMFWeightType: declared as a plain 0..5 sequence, but the native enum is bit flags (1,2,4,8,16,32). Every value except EXP called the wrong weighting formula in WeightedMedianFilter. Also removes VideoCaptureAPIs.OPENNI/OPENNI_ASUS/GIGANETIX, whose native backend IDs (CAP_OPENNI/CAP_OPENNI_ASUS/CAP_GIGANETIX) no longer exist in OpenCV5 at all (OpenNI was superseded by OpenNI2, Giganetix support was dropped) - VideoCapture.Open() with these would silently fail to find a backend.
1 parent 52fa2c5 commit dac6456

4 files changed

Lines changed: 56 additions & 41 deletions

File tree

src/OpenCvSharp/Modules/calib/Enum/FishEyeCalibrationFlags.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,47 @@ public enum FishEyeCalibrationFlags
1919
UseIntrinsicGuess = 1,
2020

2121
/// <summary>
22-
/// For fisheye model only. Recompute board position on each calibration iteration.
22+
/// The principal point (cx, cy) stays the same as in the input camera matrix. Image center is used as principal point, if UseIntrinsicGuess is not set.
2323
/// </summary>
24-
RecomputeExtrinsic = 1 << 1,
24+
FixPrincipalPoint = 0x00004,
2525

2626
/// <summary>
27-
/// For fisheye model only. Check SVD decomposition quality for each frame during extrinsics estimation.
27+
/// The distortion coefficient k1 is not changed during the optimization. 0 value is used, if UseIntrinsicGuess is not set.
2828
/// </summary>
29-
CheckCond = 1 << 2,
29+
FixK1 = 0x00020,
3030

3131
/// <summary>
32-
/// For fisheye model only. Skew coefficient (alpha) is set to zero and stay zero.
32+
/// The distortion coefficient k2 is not changed during the optimization. 0 value is used, if UseIntrinsicGuess is not set.
3333
/// </summary>
34-
FixSkew = 1 << 3,
34+
FixK2 = 0x00040,
3535

3636
/// <summary>
37-
/// The distortion coefficient k1 is not changed during the optimization. 0 value is used, if UseIntrinsicGuess is not set.
37+
/// The distortion coefficient k3 is not changed during the optimization. 0 value is used, if UseIntrinsicGuess is not set.
3838
/// </summary>
39-
FixK1 = 1 << 4,
39+
FixK3 = 0x00080,
4040

4141
/// <summary>
42-
/// The distortion coefficient k2 is not changed during the optimization. 0 value is used, if UseIntrinsicGuess is not set.
42+
/// The distortion coefficient k4 is not changed during the optimization. 0 value is used, if UseIntrinsicGuess is not set.
4343
/// </summary>
44-
FixK2 = 1 << 5,
44+
FixK4 = 0x00800,
4545

4646
/// <summary>
47-
/// The distortion coefficient k3 is not changed during the optimization. 0 value is used, if UseIntrinsicGuess is not set.
47+
/// For stereo and multi-camera calibration only. Do not optimize cameras intrinsics.
4848
/// </summary>
49-
FixK3 = 1 << 6,
49+
FixIntrinsic = 0x00100,
5050

5151
/// <summary>
52-
/// The distortion coefficient k4 is not changed during the optimization. 0 value is used, if UseIntrinsicGuess is not set.
52+
/// For fisheye model only. Recompute board position on each calibration iteration.
5353
/// </summary>
54-
FixK4 = 1 << 7,
54+
RecomputeExtrinsic = 1 << 23,
5555

5656
/// <summary>
57-
/// For stereo and multi-camera calibration only. Do not optimize cameras intrinsics.
57+
/// For fisheye model only. Check SVD decomposition quality for each frame during extrinsics estimation.
5858
/// </summary>
59-
FixIntrinsic = 1 << 8,
59+
CheckCond = 1 << 24,
6060

6161
/// <summary>
62-
/// The principal point (cx, cy) stays the same as in the input camera matrix. Image center is used as principal point, if UseIntrinsicGuess is not set.
62+
/// For fisheye model only. Skew coefficient (alpha) is set to zero and stay zero.
6363
/// </summary>
64-
FixPrincipalPoint = 1 << 9
64+
FixSkew = 1 << 25
6565
}

src/OpenCvSharp/Modules/videoio/Enum/VideoCaptureAPIs.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,10 @@ public enum VideoCaptureAPIs
6666
/// </summary>
6767
PVAPI = 800,
6868

69-
/// <summary>
70-
/// OpenNI (for Kinect)
71-
/// </summary>
72-
OPENNI = 900,
73-
74-
/// <summary>
75-
/// OpenNI (for Asus Xtion)
76-
/// </summary>
77-
OPENNI_ASUS = 910,
78-
7969
/// <summary>
8070
/// Android - not used
8171
/// </summary>
82-
ANDROID = 1000,
72+
ANDROID = 1000,
8373

8474
/// <summary>
8575
/// XIMEA Camera API
@@ -91,15 +81,10 @@ public enum VideoCaptureAPIs
9181
/// </summary>
9282
AVFOUNDATION = 1200,
9383

94-
/// <summary>
95-
/// Smartek Giganetix GigEVisionSDK
96-
/// </summary>
97-
GIGANETIX = 1300,
98-
9984
/// <summary>
10085
/// Microsoft Media Foundation (via videoInput)
10186
/// </summary>
102-
MSMF = 1400,
87+
MSMF = 1400,
10388

10489
/// <summary>
10590
/// Microsoft Windows Runtime using Media Foundation

src/OpenCvSharp/Modules/ximgproc/Enum/WMFWeightType.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,36 @@ namespace OpenCvSharp.XImgProc;
44
/// <summary>
55
/// Specifies weight types of weighted median filter.
66
/// </summary>
7+
[Flags]
78
public enum WMFWeightType
89
{
910
/// <summary>
1011
/// \f$exp(-|I1-I2|^2/(2*sigma^2))\f$
1112
/// </summary>
12-
EXP,
13+
EXP = 1,
1314

1415
/// <summary>
1516
/// \f$(|I1-I2|+sigma)^-1\f$
1617
/// </summary>
17-
IV1,
18+
IV1 = 1 << 1,
1819

1920
/// <summary>
2021
/// \f$(|I1-I2|^2+sigma^2)^-1\f$
2122
/// </summary>
22-
IV2,
23+
IV2 = 1 << 2,
2324

2425
/// <summary>
2526
/// \f$dot(I1,I2)/(|I1|*|I2|)\f$
2627
/// </summary>
27-
COS,
28+
COS = 1 << 3,
2829

2930
/// <summary>
3031
/// \f$(min(r1,r2)+min(g1,g2)+min(b1,b2))/(max(r1,r2)+max(g1,g2)+max(b1,b2))\f$
3132
/// </summary>
32-
JAC,
33+
JAC = 1 << 4,
3334

3435
/// <summary>
3536
/// unweighted
3637
/// </summary>
37-
OFF
38+
OFF = 1 << 5
3839
}

test/OpenCvSharp.Tests/calib3d/Calib3dTest.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,35 @@ public void FishEyeCalibrate()
314314
Assert.NotEmpty(translationVectors);
315315
}
316316

317+
// Regression test for issue #2080-adjacent enum drift: FishEyeCalibrationFlags used to carry
318+
// stale OpenCV4 fisheye-only bit values instead of the OpenCV5 aliased CALIB_* values, so
319+
// FixK1..FixK4 silently set the wrong native bits. If the flags don't reach native correctly,
320+
// the corresponding distortion coefficients are optimized freely instead of being held at 0.
321+
[Fact]
322+
public void FishEyeCalibrateWithFixedDistortion()
323+
{
324+
var patternSize = new Size(10, 7);
325+
326+
using var image = LoadImage("calibration/00.jpg");
327+
using var corners = new Mat<Point2f>();
328+
Cv2.FindChessboardCorners(image, patternSize, corners);
329+
330+
var objectPointsArray = Create3DChessboardCorners(patternSize, 1.0f).ToArray();
331+
var imagePointsArray = corners.ToArray();
332+
333+
using var objectPoints = Mat<Point3f>.FromArray(objectPointsArray);
334+
using var imagePoints = Mat<Point2f>.FromArray(imagePointsArray);
335+
using var cameraMatrix = Mat.EyeMat(3, 3, MatType.CV_64FC1);
336+
using var distCoeffs = new Mat<double>();
337+
Cv2.FishEye.Calibrate([objectPoints], [imagePoints], image.Size(), cameraMatrix,
338+
distCoeffs, out _, out _,
339+
FishEyeCalibrationFlags.FixK1 | FishEyeCalibrationFlags.FixK2 |
340+
FishEyeCalibrationFlags.FixK3 | FishEyeCalibrationFlags.FixK4);
341+
342+
var distCoeffValues = distCoeffs.ToArray();
343+
Assert.All(distCoeffValues, d => Assert.Equal(0.0, d, 10));
344+
}
345+
317346
[Fact]
318347
public void FishEyeCalibrateWithNonContinuousPoints()
319348
{

0 commit comments

Comments
 (0)