Skip to content

Commit 1fc449a

Browse files
authored
Merge pull request #1818 from shimat/16f
Add CV_16F
2 parents 43ffda3 + 55d38fe commit 1fc449a

10 files changed

Lines changed: 145 additions & 58 deletions

File tree

src/OpenCvSharp/Cv2/Cv2_core.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,6 @@ public static void BitwiseAnd(InputArray src1, InputArray src2, OutputArray dst,
12881288
GC.KeepAlive(src1);
12891289
GC.KeepAlive(src2);
12901290
GC.KeepAlive(dst);
1291-
dst.Fix();
12921291
GC.KeepAlive(mask);
12931292
}
12941293

@@ -1491,7 +1490,7 @@ public static void InRange(InputArray src, Scalar lowerb, Scalar upperb, OutputA
14911490
/// <param name="dst">output array of type ref CV_8U that has the same size and the same number of channels as the input arrays.</param>
14921491
/// <param name="cmpop">a flag, that specifies correspondence between the arrays (cv::CmpTypes)</param>
14931492
// ReSharper disable once IdentifierTypo
1494-
public static void Compare(InputArray src1, InputArray src2, OutputArray dst, CmpType cmpop)
1493+
public static void Compare(InputArray src1, InputArray src2, OutputArray dst, CmpTypes cmpop)
14951494
{
14961495
if (src1 is null)
14971496
throw new ArgumentNullException(nameof(src1));
@@ -1748,7 +1747,6 @@ public static void Log(InputArray src, OutputArray dst)
17481747

17491748
GC.KeepAlive(src);
17501749
GC.KeepAlive(dst);
1751-
dst.Fix();
17521750
}
17531751

17541752

@@ -2511,7 +2509,7 @@ public static void EigenNonSymmetric(InputArray src, OutputArray eigenvalues, Ou
25112509
/// <param name="samples">samples stored as separate matrices</param>
25122510
/// <param name="covar">output covariance matrix of the type ctype and square size.</param>
25132511
/// <param name="mean">input or output (depending on the flags) array as the average value of the input vectors.</param>
2514-
/// <param name="flags">operation flags as a combination of CovarFlags</param>
2512+
/// <param name="flags">operation flags - see CovarFlags.</param>
25152513
/// <param name="ctype">type of the matrixl; it equals 'CV_64F' by default.</param>
25162514
public static void CalcCovarMatrix(
25172515
Mat[] samples, Mat covar, Mat mean,
@@ -2542,7 +2540,7 @@ public static void CalcCovarMatrix(
25422540
/// <param name="samples">samples stored as rows/columns of a single matrix.</param>
25432541
/// <param name="covar">output covariance matrix of the type ctype and square size.</param>
25442542
/// <param name="mean">input or output (depending on the flags) array as the average value of the input vectors.</param>
2545-
/// <param name="flags">operation flags as a combination of CovarFlags</param>
2543+
/// <param name="flags">operation flags - see CovarFlags.</param>
25462544
/// <param name="ctype">type of the matrixl; it equals 'CV_64F' by default.</param>
25472545
public static void CalcCovarMatrix(
25482546
InputArray samples, OutputArray covar,
@@ -2575,7 +2573,7 @@ public static void CalcCovarMatrix(
25752573
/// <param name="data">input samples stored as the matrix rows or as the matrix columns.</param>
25762574
/// <param name="mean">optional mean value; if the matrix is empty (noArray()), the mean is computed from the data.</param>
25772575
/// <param name="eigenvectors">eigenvectors of the covariation matrix</param>
2578-
/// <param name="maxComponents">maximum number of components that PCA should
2576+
/// <param name="maxComponents">Number of components that PCA should
25792577
/// retain; by default, all the components are retained.</param>
25802578
public static void PCACompute(
25812579
InputArray data, InputOutputArray mean,
@@ -2606,7 +2604,7 @@ public static void PCACompute(
26062604
/// <param name="mean">optional mean value; if the matrix is empty (noArray()), the mean is computed from the data.</param>
26072605
/// <param name="eigenvectors">eigenvectors of the covariation matrix</param>
26082606
/// <param name="eigenvalues">eigenvalues of the covariation matrix</param>
2609-
/// <param name="maxComponents">maximum number of components that PCA should
2607+
/// <param name="maxComponents">Number of components that PCA should
26102608
/// retain; by default, all the components are retained.</param>
26112609
public static void PCACompute(
26122610
InputArray data, InputOutputArray mean,

src/OpenCvSharp/Modules/core/Enum/CmpType.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
namespace OpenCvSharp;
2+
3+
/// <summary>
4+
/// The flag specifying the relation between the elements to be checked
5+
/// </summary>
6+
public enum CmpTypes
7+
{
8+
/// <summary>
9+
/// src1(I) "equal to" src2(I)
10+
/// </summary>
11+
EQ = 0,
12+
13+
/// <summary>
14+
/// src1(I) "greater than" src2(I)
15+
/// </summary>
16+
GT = 1,
17+
18+
/// <summary>
19+
/// src1(I) "greater or equal" src2(I)
20+
/// </summary>
21+
GE = 2,
22+
23+
/// <summary>
24+
/// src1(I) "less than" src2(I)
25+
/// </summary>
26+
LT = 3,
27+
28+
/// <summary>
29+
/// src1(I) "less or equal" src2(I)
30+
/// </summary>
31+
LE = 4,
32+
33+
/// <summary>
34+
/// src1(I) "not equal to" src2(I)
35+
/// </summary>
36+
NE = 5,
37+
}
38+
39+
/// <summary>
40+
/// Obsolete: Use CmpTypes instead. This enum is kept for backward compatibility.
41+
/// </summary>
42+
[Obsolete("Use CmpTypes instead", false)]
43+
public enum CmpType
44+
{
45+
/// <summary>
46+
/// src1(I) "equal to" src2(I)
47+
/// </summary>
48+
EQ = 0,
49+
50+
/// <summary>
51+
/// src1(I) "greater than" src2(I)
52+
/// </summary>
53+
GT = 1,
54+
55+
/// <summary>
56+
/// src1(I) "greater or equal" src2(I)
57+
/// </summary>
58+
GE = 2,
59+
60+
/// <summary>
61+
/// src1(I) "less than" src2(I)
62+
/// </summary>
63+
LT = 3,
64+
65+
/// <summary>
66+
/// src1(I) "less or equal" src2(I)
67+
/// </summary>
68+
LE = 4,
69+
70+
/// <summary>
71+
/// src1(I) "not equal to" src2(I)
72+
/// </summary>
73+
NE = 5,
74+
}

src/OpenCvSharp/Modules/core/Struct/MatType.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public override string ToString()
106106
case CV_64F:
107107
s = "CV_64F";
108108
break;
109-
case CV_USRTYPE1:
110-
s = "CV_USRTYPE1";
109+
case CV_16F:
110+
s = "CV_16F";
111111
break;
112112
default:
113113
return $"Unsupported type value ({Value})";
@@ -135,7 +135,8 @@ public const int
135135
CV_32S = 4,
136136
CV_32F = 5,
137137
CV_64F = 6,
138-
CV_USRTYPE1 = 7;
138+
CV_16F = 7,
139+
CV_USRTYPE1 = CV_16F;
139140

140141
/// <summary>
141142
/// predefined type constants
@@ -168,7 +169,11 @@ public static readonly MatType
168169
CV_64FC1 = CV_64FC(1),
169170
CV_64FC2 = CV_64FC(2),
170171
CV_64FC3 = CV_64FC(3),
171-
CV_64FC4 = CV_64FC(4);
172+
CV_64FC4 = CV_64FC(4),
173+
CV_16FC1 = CV_16FC(1),
174+
CV_16FC2 = CV_16FC(2),
175+
CV_16FC3 = CV_16FC(3),
176+
CV_16FC4 = CV_16FC(4);
172177
/*
173178
public const int
174179
CV_8UC1 = 0,
@@ -250,6 +255,11 @@ public static MatType CV_64FC(int ch)
250255
return MakeType(CV_64F, ch);
251256
}
252257

258+
public static MatType CV_16FC(int ch)
259+
{
260+
return MakeType(CV_16F, ch);
261+
}
262+
253263
public static MatType MakeType(int depth, int channels)
254264
{
255265
if (channels <= 0 || channels >= CV_CN_MAX)

src/OpenCvSharp/Modules/imgproc/ConnectedComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private Mat GetLabelMask(int label)
180180
using var labels = Mat.FromPixelData(rows, cols, MatType.CV_32SC1, Labels.GetBuffer());
181181
using var cmp = new Mat(rows, cols, MatType.CV_32SC1, Scalar.All(label));
182182
var result = new Mat();
183-
Cv2.Compare(labels, cmp, result, CmpType.EQ);
183+
Cv2.Compare(labels, cmp, result, CmpTypes.EQ);
184184
return result;
185185
}
186186

test/OpenCvSharp.Tests/TestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected static void ImageEquals(Mat img1, Mat img2)
3838
#pragma warning restore CA1062
3939

4040
using var comparison = new Mat();
41-
Cv2.Compare(img1, img2, comparison, CmpType.NE);
41+
Cv2.Compare(img1, img2, comparison, CmpTypes.NE);
4242

4343
if (img1.Channels() == 1)
4444
{

test/OpenCvSharp.Tests/core/CoreTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public void Compare()
288288
using var src = Mat.FromPixelData(bytes.Length, 1, MatType.CV_8UC1, bytes);
289289
using var dst = new Mat();
290290

291-
Cv2.Compare(src, 3, dst, CmpType.LE);
291+
Cv2.Compare(src, 3, dst, CmpTypes.LE);
292292
Assert.Equal(255, dst.Get<byte>(0));
293293
Assert.Equal(255, dst.Get<byte>(1));
294294
Assert.Equal(255, dst.Get<byte>(2));

test/OpenCvSharp.Tests/core/MatTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,37 @@ public void At()
156156
Assert.Equal(55.5555f, mat32FC1.At<float>(2, 0));
157157
}
158158

159+
#if NET8_0_OR_GREATER
160+
[Fact]
161+
public void ConvertTo16FMatchesHalfValues()
162+
{
163+
var srcValues = new float[]
164+
{
165+
-2.0f, -0.5f, 0.0f,
166+
0.5f, 1.0f, 10.25f,
167+
};
168+
169+
using var src32f = new Mat(2, 3, MatType.CV_32FC1);
170+
Assert.True(src32f.SetArray(srcValues));
171+
172+
using var halfMat = new Mat();
173+
src32f.ConvertTo(halfMat, MatType.CV_16FC1);
174+
175+
Assert.Equal(MatType.CV_16FC1, halfMat.Type());
176+
Assert.Equal(MatType.CV_16F, halfMat.Depth());
177+
Assert.Equal(2, halfMat.ElemSize1());
178+
179+
var halfValues = halfMat.AsSpan<Half>();
180+
Assert.Equal(srcValues.Length, halfValues.Length);
181+
182+
for (var i = 0; i < srcValues.Length; i++)
183+
{
184+
var expected = (Half)srcValues[i];
185+
Assert.Equal(expected, halfValues[i]);
186+
}
187+
}
188+
#endif
189+
159190
[Fact]
160191
public void Diag()
161192
{

test/OpenCvSharp.Tests/core/MatTypeTest.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public void CreateByChannels()
3737
Assert.Equal(MatType.CV_64FC2, MatType.CV_64FC(2));
3838
Assert.Equal(MatType.CV_64FC3, MatType.CV_64FC(3));
3939
Assert.Equal(MatType.CV_64FC4, MatType.CV_64FC(4));
40+
Assert.Equal(MatType.CV_16FC1, MatType.CV_16FC(1));
41+
Assert.Equal(MatType.CV_16FC2, MatType.CV_16FC(2));
42+
Assert.Equal(MatType.CV_16FC3, MatType.CV_16FC(3));
43+
Assert.Equal(MatType.CV_16FC4, MatType.CV_16FC(4));
4044
}
4145

4246
[Fact]
@@ -84,15 +88,23 @@ public void MakeType()
8488
Assert.Equal(MatType.CV_64FC(4), MatType.MakeType(MatType.CV_64F, 4));
8589
Assert.Equal(MatType.CV_64FC(5), MatType.MakeType(MatType.CV_64F, 5));
8690
Assert.Equal(MatType.CV_64FC(6), MatType.MakeType(MatType.CV_64F, 6));
91+
Assert.Equal(MatType.CV_16FC(1), MatType.MakeType(MatType.CV_16F, 1));
92+
Assert.Equal(MatType.CV_16FC(2), MatType.MakeType(MatType.CV_16F, 2));
93+
Assert.Equal(MatType.CV_16FC(3), MatType.MakeType(MatType.CV_16F, 3));
94+
Assert.Equal(MatType.CV_16FC(4), MatType.MakeType(MatType.CV_16F, 4));
95+
Assert.Equal(MatType.CV_16FC(5), MatType.MakeType(MatType.CV_16F, 5));
96+
Assert.Equal(MatType.CV_16FC(6), MatType.MakeType(MatType.CV_16F, 6));
8797
}
8898

89-
// TODO
90-
/*[Fact]
99+
[Fact]
91100
public void DoNotCrash16F()
92101
{
93-
var matType = MatType.MakeType(7, 3);
102+
var matType = MatType.CV_16FC3;
94103
using var src = new Mat(4, 3, matType);
95104
using var dst = new Mat();
96-
Cv2.Compare(src, src, dst, CmpTypes.EQ);
97-
}*/
105+
// Transpose supports all types including CV_16F
106+
Cv2.Transpose(src, dst);
107+
Assert.Equal(matType, src.Type());
108+
Assert.Equal("CV_16FC3", matType.ToString());
109+
}
98110
}

0 commit comments

Comments
 (0)