Skip to content

Commit 003fd08

Browse files
committed
Add CV_16F
1 parent 43ffda3 commit 003fd08

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

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)

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: 15 additions & 4 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,22 @@ 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();
96105
Cv2.Compare(src, src, dst, CmpTypes.EQ);
97-
}*/
106+
Assert.Equal(matType, src.Type());
107+
Assert.Equal("CV_16FC3", matType.ToString());
108+
}
98109
}

0 commit comments

Comments
 (0)