Skip to content

Commit 7e45d53

Browse files
committed
Add Mat.EyeMat
1 parent 67e274d commit 7e45d53

2 files changed

Lines changed: 87 additions & 9 deletions

File tree

src/OpenCvSharp/Modules/core/Mat/Mat.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,84 @@ public static MatExpr Eye(int rows, int cols, MatType type)
695695
return retVal;
696696
}
697697

698+
/// <summary>
699+
/// Returns a zero array of the specified size and type as <see cref="Mat"/>.
700+
/// Unlike <see cref="Zeros(int,int,MatType)"/>, this method returns a <see cref="Mat"/> directly,
701+
/// so a single <c>using</c> statement is sufficient to manage its lifetime.
702+
/// </summary>
703+
/// <param name="rows">Number of rows.</param>
704+
/// <param name="cols">Number of columns.</param>
705+
/// <param name="type">Created matrix type.</param>
706+
/// <returns></returns>
707+
public static Mat ZerosMat(int rows, int cols, MatType type)
708+
{
709+
using var expr = Zeros(rows, cols, type);
710+
return (Mat)expr;
711+
}
712+
713+
/// <summary>
714+
/// Returns a zero array of the specified size and type as <see cref="Mat"/>.
715+
/// Unlike <see cref="Zeros(Size,MatType)"/>, this method returns a <see cref="Mat"/> directly,
716+
/// so a single <c>using</c> statement is sufficient to manage its lifetime.
717+
/// </summary>
718+
/// <param name="size">Alternative to the matrix size specification Size(cols, rows).</param>
719+
/// <param name="type">Created matrix type.</param>
720+
/// <returns></returns>
721+
public static Mat ZerosMat(Size size, MatType type)
722+
=> ZerosMat(size.Height, size.Width, type);
723+
724+
/// <summary>
725+
/// Returns an array of all 1's of the specified size and type as <see cref="Mat"/>.
726+
/// Unlike <see cref="Ones(int,int,MatType)"/>, this method returns a <see cref="Mat"/> directly,
727+
/// so a single <c>using</c> statement is sufficient to manage its lifetime.
728+
/// </summary>
729+
/// <param name="rows">Number of rows.</param>
730+
/// <param name="cols">Number of columns.</param>
731+
/// <param name="type">Created matrix type.</param>
732+
/// <returns></returns>
733+
public static Mat OnesMat(int rows, int cols, MatType type)
734+
{
735+
using var expr = Ones(rows, cols, type);
736+
return (Mat)expr;
737+
}
738+
739+
/// <summary>
740+
/// Returns an array of all 1's of the specified size and type as <see cref="Mat"/>.
741+
/// Unlike <see cref="Ones(Size,MatType)"/>, this method returns a <see cref="Mat"/> directly,
742+
/// so a single <c>using</c> statement is sufficient to manage its lifetime.
743+
/// </summary>
744+
/// <param name="size">Alternative to the matrix size specification Size(cols, rows).</param>
745+
/// <param name="type">Created matrix type.</param>
746+
/// <returns></returns>
747+
public static Mat OnesMat(Size size, MatType type)
748+
=> OnesMat(size.Height, size.Width, type);
749+
750+
/// <summary>
751+
/// Returns an identity matrix of the specified size and type as <see cref="Mat"/>.
752+
/// Unlike <see cref="Eye(int,int,MatType)"/>, this method returns a <see cref="Mat"/> directly,
753+
/// so a single <c>using</c> statement is sufficient to manage its lifetime.
754+
/// </summary>
755+
/// <param name="rows">Number of rows.</param>
756+
/// <param name="cols">Number of columns.</param>
757+
/// <param name="type">Created matrix type.</param>
758+
/// <returns></returns>
759+
public static Mat EyeMat(int rows, int cols, MatType type)
760+
{
761+
using var expr = Eye(rows, cols, type);
762+
return (Mat)expr;
763+
}
764+
765+
/// <summary>
766+
/// Returns an identity matrix of the specified size and type as <see cref="Mat"/>.
767+
/// Unlike <see cref="Eye(Size,MatType)"/>, this method returns a <see cref="Mat"/> directly,
768+
/// so a single <c>using</c> statement is sufficient to manage its lifetime.
769+
/// </summary>
770+
/// <param name="size">Alternative to the matrix size specification Size(cols, rows).</param>
771+
/// <param name="type">Created matrix type.</param>
772+
/// <returns></returns>
773+
public static Mat EyeMat(Size size, MatType type)
774+
=> EyeMat(size.Height, size.Width, type);
775+
698776
#region FromArray
699777

700778
/// <summary>

test/OpenCvSharp.Tests/calib3d/Calib3dTest.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void CalibrateCameraByMat()
140140

141141
using var objectPoints = Mat<Point3f>.FromArray(objectPointsArray);
142142
using var imagePoints = Mat<Point2f>.FromArray(imagePointsArray);
143-
using var cameraMatrix = new Mat<double>(Mat.Eye(3, 3, MatType.CV_64FC1));
143+
using var cameraMatrix = Mat.EyeMat(3, 3, MatType.CV_64FC1);
144144
using var distCoeffs = new Mat<double>();
145145
var rms = Cv2.CalibrateCamera([objectPoints], [imagePoints], image.Size(), cameraMatrix,
146146
distCoeffs, out var rotationVectors, out var translationVectors,
@@ -165,7 +165,7 @@ public void FishEyeCalibrate()
165165

166166
using var objectPoints = Mat<Point3f>.FromArray(objectPointsArray);
167167
using var imagePoints = Mat<Point2f>.FromArray(imagePointsArray);
168-
using var cameraMatrix = new Mat<double>(Mat.Eye(3, 3, MatType.CV_64FC1));
168+
using var cameraMatrix = Mat.EyeMat(3, 3, MatType.CV_64FC1);
169169
using var distCoeffs = new Mat<double>();
170170
var rms = Cv2.FishEye.Calibrate([objectPoints], [imagePoints], image.Size(), cameraMatrix,
171171
distCoeffs, out var rotationVectors, out var translationVectors);
@@ -327,8 +327,8 @@ public void SolvePnPTestByMat()
327327

328328
using var objPtsMat = Mat.FromPixelData(objPts.Length, 1, MatType.CV_32FC3, objPts);
329329
using var imgPtsMat = Mat.FromPixelData(imgPts.Length, 1, MatType.CV_32FC2, imgPts);
330-
using var cameraMatrixMat = Mat.Eye(3, 3, MatType.CV_64FC1);
331-
using var distMat = Mat.Zeros(5, 0, MatType.CV_64FC1);
330+
using var cameraMatrixMat = Mat.EyeMat(3, 3, MatType.CV_64FC1);
331+
using var distMat = Mat.ZerosMat(5, 0, MatType.CV_64FC1);
332332
using var rvecMat = new Mat();
333333
using var tvecMat = new Mat();
334334
Cv2.SolvePnP(objPtsMat, imgPtsMat, cameraMatrixMat, distMat, rvecMat, tvecMat);
@@ -496,7 +496,7 @@ public void RecoverPoseWithTriangulatedPoints()
496496

497497
using var points1m = Mat<double>.FromArray(points1);
498498
using var points2m = Mat<double>.FromArray(points2);
499-
using var K = Mat.Eye(3, 3, MatType.CV_64F);
499+
using var K = Mat.EyeMat(3, 3, MatType.CV_64F);
500500
using var inliers = new Mat();
501501
using var E = Cv2.FindEssentialMat(points1m, points2m, K, EssentialMatMethod.LMedS, 0.99, 1.0, inliers);
502502

@@ -589,9 +589,9 @@ public void StereoCalibrateByInputArray()
589589
using var ip1IA = InputArray.Create(cornerPoints);
590590
using var ip2IA = InputArray.Create(cornerPoints);
591591

592-
using var cameraMatrix1 = new Mat<double>(Mat.Eye(3, 3, MatType.CV_64FC1));
592+
using var cameraMatrix1 = Mat.EyeMat(3, 3, MatType.CV_64FC1);
593593
using var distCoeffs1 = new Mat<double>();
594-
using var cameraMatrix2 = new Mat<double>(Mat.Eye(3, 3, MatType.CV_64FC1));
594+
using var cameraMatrix2 = Mat.EyeMat(3, 3, MatType.CV_64FC1);
595595
using var distCoeffs2 = new Mat<double>();
596596
using var R = new Mat();
597597
using var T = new Mat();
@@ -621,9 +621,9 @@ public void StereoCalibrateByMat()
621621

622622
using var objectPoints = Mat<Point3f>.FromArray(objectPointsArray);
623623
using var imagePoints = Mat<Point2f>.FromArray(cornerPoints);
624-
using var cameraMatrix1 = new Mat(Mat.Eye(3, 3, MatType.CV_64FC1));
624+
using var cameraMatrix1 = Mat.EyeMat(3, 3, MatType.CV_64FC1);
625625
using var distCoeffs1 = new Mat();
626-
using var cameraMatrix2 = new Mat(Mat.Eye(3, 3, MatType.CV_64FC1));
626+
using var cameraMatrix2 = Mat.EyeMat(3, 3, MatType.CV_64FC1);
627627
using var distCoeffs2 = new Mat();
628628
using var R = new Mat();
629629
using var T = new Mat();

0 commit comments

Comments
 (0)