public static Matrix <T> MatrixCast <T>(MatrixBase matrix) where T : struct { if (matrix == null) { throw new ArgumentNullException(nameof(matrix)); } matrix.ThrowIfDisposed(); var type = matrix.MatrixElementType.ToNativeMatrixElementType(); Matrix <T> .TryParse <T>(out var destElementType); var ret = NativeMethods.matrix_cast(type, matrix.NativePtr, matrix.TemplateRows, matrix.TemplateColumns, destElementType.ToNativeMatrixElementType(), out var mat); switch (ret) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{type} is not supported."); } return(new Matrix <T>(mat, matrix.TemplateRows, matrix.TemplateColumns)); }
/// <summary> /// This function saves matrix to disk as DNG (Digital Negative) file. /// </summary> /// <typeparam name="T">The type of element in the matrix.</typeparam> /// <param name="matrix">The matrix.</param> /// <param name="path">A string that contains the name of the file to which to save matrix.</param> /// <exception cref="ArgumentException">The specified type of matrix is not supported.</exception> /// <exception cref="ArgumentException">The <see cref="MatrixBase.TemplateRows"/> or <see cref="MatrixBase.TemplateColumns"/> is not supported.</exception> /// <exception cref="ArgumentException"><see cref="TwoDimensionObjectBase.Rows"/> or <see cref="TwoDimensionObjectBase.Columns"/> are less than or equal to zero.</exception> /// <exception cref="ArgumentNullException"><paramref name="matrix"/> or <paramref name="path"/> is null.</exception> /// <exception cref="ObjectDisposedException"><paramref name="matrix"/> is disposed.</exception> public static void SaveDng(MatrixBase matrix, string path) { if (matrix == null) { throw new ArgumentNullException(nameof(matrix)); } matrix.ThrowIfDisposed(); if (path == null) { throw new ArgumentNullException(nameof(path)); } // NOTE: save_dng does not throw exception but it does NOT output any file. // So it should throw exception in this timing!! if (matrix.Rows <= 0 || matrix.Columns <= 0) { throw new ArgumentException($"{nameof(matrix.Columns)} and {nameof(matrix.Rows)} is less than or equal to zero.", nameof(matrix)); } var str = Encoding.GetBytes(path); var matrixElementType = matrix.MatrixElementType.ToNativeMatrixElementType(); var ret = NativeMethods.save_dng_matrix(matrixElementType, matrix.NativePtr, matrix.TemplateRows, matrix.TemplateColumns, str); switch (ret) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{matrix.MatrixElementType} is not supported."); case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport: throw new ArgumentException($"{nameof(matrix.TemplateColumns)} or {nameof(matrix.TemplateRows)} is not supported."); } }
public static MatrixOp Trans(MatrixBase matrix) { if (matrix == null) { throw new ArgumentNullException(nameof(matrix)); } matrix.ThrowIfDisposed(); var templateRows = matrix.TemplateRows; var templateColumns = matrix.TemplateColumns; var type = matrix.MatrixElementType.ToNativeMatrixElementType(); var ret = NativeMethods.matrix_trans(type, matrix.NativePtr, templateRows, templateColumns, out var matrixOp); switch (ret) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{type} is not supported."); } var imageType = matrix.MatrixElementType; return(new MatrixOp(NativeMethods.ElementType.OpTrans, imageType, matrixOp, templateRows, templateColumns)); }
public FullObjectDetection Detect(MatrixBase image, Rectangle rect) { this.ThrowIfDisposed(); if (image == null) { throw new ArgumentNullException(nameof(image)); } image.ThrowIfDisposed(); var inType = image.MatrixElementType.ToNativeMatrixElementType(); using (var native = rect.ToNative()) { var ret = Native.shape_predictor_matrix_operator(this.NativePtr, inType, image.NativePtr, native.NativePtr, out var fullObjDetect); switch (ret) { case Dlib.Native.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"Input {inType} is not supported."); } return(new FullObjectDetection(fullObjDetect)); } }
public Matrix <double> Operator(MatrixBase data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } data.ThrowIfDisposed(); var kernelType = this._MultilayerPerceptronKernelType.ToNativeMlpKernelType(); var type = data.MatrixElementType.ToNativeMatrixElementType(); var ret = Dlib.Native.mlp_kernel_operator(kernelType, this.NativePtr, type, data.NativePtr, out var retMat); switch (ret) { case Dlib.Native.ErrorType.InputElementTypeNotSupport: throw new ArgumentException($"{type} is not supported."); case Dlib.Native.ErrorType.MlpKernelNotSupport: throw new ArgumentException($"{kernelType} is not supported."); } return(new Matrix <double>(retMat)); }
public static void DisturbColors(MatrixBase image, Rand rand, double gammaMagnitude = 0.5, double colorMagnitude = 0.2) { if (image == null) { throw new ArgumentNullException(nameof(image)); } if (rand == null) { throw new ArgumentNullException(nameof(rand)); } image.ThrowIfDisposed(); rand.ThrowIfDisposed(); var matrixElementType = image.MatrixElementType.ToNativeMatrixElementType(); var ret = NativeMethods.disturb_colors_matrix(matrixElementType, image.NativePtr, image.TemplateRows, image.TemplateColumns, rand.NativePtr, gammaMagnitude, colorMagnitude); if (ret == NativeMethods.ErrorType.MatrixElementTypeNotSupport) { throw new ArgumentException($"{image.MatrixElementType} is not supported."); } }
public static void ApplyRandomColorOffset(MatrixBase image, Rand rand) { if (image == null) { throw new ArgumentNullException(nameof(image)); } if (rand == null) { throw new ArgumentNullException(nameof(rand)); } image.ThrowIfDisposed(); rand.ThrowIfDisposed(); var matrixElementType = image.MatrixElementType.ToNativeMatrixElementType(); var ret = NativeMethods.apply_random_color_offset_matrix(matrixElementType, image.NativePtr, image.TemplateRows, image.TemplateColumns, rand.NativePtr); if (ret == NativeMethods.ErrorType.MatrixElementTypeNotSupport) { throw new ArgumentException($"{image.MatrixElementType} is not supported."); } }
public static void PyramidUp <T>(MatrixBase image, uint pyramidRate) where T : Pyramid { if (image == null) { throw new ArgumentNullException(nameof(image)); } image.ThrowIfDisposed(nameof(image)); var type = image.MatrixElementType.ToNativeMatrixElementType(); Pyramid.TryGetSupportPyramidType <T>(out var pyramidType); var ret = NativeMethods.pyramid_up_pyramid_matrix(pyramidType, pyramidRate, type, image.NativePtr); switch (ret) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{image.MatrixElementType} is not supported."); case NativeMethods.ErrorType.PyramidNotSupportType: case NativeMethods.ErrorType.PyramidNotSupportRate: throw new NotSupportedException(); } }
public static void ResizeImage(MatrixBase matrix, double scale) { if (matrix == null) { throw new ArgumentNullException(nameof(matrix)); } if (scale <= 0) { throw new ArgumentException($"{nameof(scale)} is less than or equal to zero.", nameof(scale)); } matrix.ThrowIfDisposed(nameof(matrix)); var inType = matrix.MatrixElementType.ToNativeMatrixElementType(); var ret = NativeMethods.resize_image_matrix_scale(inType, matrix.NativePtr, matrix.TemplateRows, matrix.TemplateColumns, scale); switch (ret) { case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport: throw new ArgumentException($"{nameof(matrix.TemplateColumns)} or {nameof(matrix.TemplateRows)} is not supported."); case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{matrix.MatrixElementType} is not supported."); } }
public FullObjectDetection Detect(MatrixBase image, MModRect rect) { this.ThrowIfDisposed(); if (image == null) { throw new ArgumentNullException(nameof(image)); } if (rect == null) { throw new ArgumentNullException(nameof(rect)); } image.ThrowIfDisposed(); rect.ThrowIfDisposed(); var inType = image.MatrixElementType.ToNativeMatrixElementType(); var ret = NativeMethods.shape_predictor_matrix_operator_mmod_rect(this.NativePtr, inType, image.NativePtr, rect.NativePtr, out var fullObjDetect); switch (ret) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"Input {inType} is not supported."); } return(new FullObjectDetection(fullObjDetect)); }
public void Operator(MatrixBase image, out IEnumerable <RectDetection> detections, double threshold = 0d) { this.ThrowIfDisposed(); if (image == null) { throw new ArgumentNullException(nameof(image)); } image.ThrowIfDisposed(); using (var dets = new StdVector <RectDetection>()) { var inType = image.MatrixElementType.ToNativeMatrixElementType(); var ret = NativeMethods.frontal_face_detector_matrix_operator2(this.NativePtr, inType, image.NativePtr, threshold, dets.NativePtr); switch (ret) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"Input {inType} is not supported."); } detections = dets.ToArray(); } }
public static MatrixOp JoinRows(MatrixBase matrix1, MatrixBase matrix2) { if (matrix1 == null) { throw new ArgumentNullException(nameof(matrix1)); } if (matrix2 == null) { throw new ArgumentNullException(nameof(matrix2)); } matrix1.ThrowIfDisposed(); matrix2.ThrowIfDisposed(); // Need not to check whether both TemplateColumns and TemplateRows are same var templateRows = matrix1.TemplateRows; var templateColumns = matrix1.TemplateColumns; // In the future, these statement shold be removed because above comment if (templateRows != matrix2.TemplateRows) { throw new ArgumentException(); } if (templateColumns != matrix2.TemplateColumns) { throw new ArgumentException(); } var type1 = matrix1.MatrixElementType.ToNativeMatrixElementType(); var type2 = matrix2.MatrixElementType.ToNativeMatrixElementType(); if (type1 != type2) { throw new ArgumentException(); } var ret = NativeMethods.matrix_join_rows(type1, matrix1.NativePtr, matrix2.NativePtr, templateRows, templateColumns, out var matrixOp); switch (ret) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{type1} is not supported."); } var imageType = matrix1.MatrixElementType; return(new MatrixOp(NativeMethods.ElementType.OpJoinRows, imageType, matrixOp, templateRows, templateColumns)); }
public ImageWindow(MatrixBase matrix) { if (matrix == null) { throw new ArgumentNullException(nameof(matrix)); } matrix.ThrowIfDisposed(nameof(matrix)); var type = matrix.MatrixElementType.ToNativeMatrixElementType(); this.NativePtr = NativeMethods.image_window_new_matrix1(type, matrix.NativePtr); }
public static MatrixOp JoinRows(MatrixBase matrix1, MatrixBase matrix2) { if (matrix1 == null) { throw new ArgumentNullException(nameof(matrix1)); } if (matrix2 == null) { throw new ArgumentNullException(nameof(matrix2)); } matrix1.ThrowIfDisposed(); matrix2.ThrowIfDisposed(); var templateRows = matrix1.TemplateRows; var templateColumns = matrix1.TemplateColumns; if (templateRows != matrix2.TemplateRows) { throw new ArgumentException(); } if (templateColumns != matrix2.TemplateColumns) { throw new ArgumentException(); } var type1 = matrix1.MatrixElementType.ToNativeMatrixElementType(); var type2 = matrix2.MatrixElementType.ToNativeMatrixElementType(); if (type1 != type2) { throw new ArgumentException(); } var ret = Native.matrix_join_rows(type1, matrix1.NativePtr, matrix2.NativePtr, templateRows, templateColumns, out var matrixOp); switch (ret) { case Native.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{type1} is not supported."); } var imageType = matrix1.MatrixElementType; return(new MatrixOp(Native.ElementType.OpJoinRows, imageType, matrixOp, templateRows, templateColumns)); }
public static void PyramidUp(MatrixBase image) { if (image == null) { throw new ArgumentNullException(nameof(image)); } image.ThrowIfDisposed(nameof(image)); var type = image.MatrixElementType.ToNativeMatrixElementType(); var ret = NativeMethods.pyramid_up_matrix(type, image.NativePtr); if (ret == NativeMethods.ErrorType.MatrixElementTypeNotSupport) { throw new ArgumentException($"{image.MatrixElementType} is not supported."); } }
public ImageWindow(MatrixBase matrix, string title) { if (matrix == null) { throw new ArgumentNullException(nameof(matrix)); } if (title == null) { throw new ArgumentNullException(nameof(title)); } matrix.ThrowIfDisposed(nameof(matrix)); var type = matrix.MatrixElementType.ToNativeMatrixElementType(); var str = Dlib.Encoding.GetBytes(title); this.NativePtr = NativeMethods.image_window_new_matrix2(type, matrix.NativePtr, str, str.Length); }
public void SetImage(MatrixBase matrix) { this.ThrowIfDisposed(); if (matrix == null) { throw new ArgumentNullException(nameof(matrix)); } matrix.ThrowIfDisposed(); var ret = NativeMethods.image_window_set_image_matrix(this.NativePtr, matrix.MatrixElementType.ToNativeMatrixElementType(), matrix.NativePtr); switch (ret) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{matrix.MatrixElementType} is not supported."); } }
public static double Length(MatrixBase matrix) { if (matrix == null) { throw new ArgumentNullException(nameof(matrix)); } matrix.ThrowIfDisposed(); var type = matrix.MatrixElementType.ToNativeMatrixElementType(); var ret = Native.matrix_length(type, matrix.NativePtr, matrix.TemplateRows, matrix.TemplateColumns, out var length); switch (ret) { case Native.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{type} is not supported."); } return(length); }
public static Matrix <T> ExtractImageChip <T>(MatrixBase image, ChipDetails chipLocation, InterpolationTypes type = InterpolationTypes.NearestNeighbor) where T : struct { if (image == null) { throw new ArgumentNullException(nameof(image)); } if (chipLocation == null) { throw new ArgumentNullException(nameof(chipLocation)); } image.ThrowIfDisposed(); chipLocation.ThrowIfDisposed(); if (!chipLocation.IsValid()) { throw new ArgumentException($"{nameof(chipLocation)} is invalid item."); } var chip = new Matrix <T>(); var elementType = image.MatrixElementType.ToNativeMatrixElementType(); var ret = Native.extract_image_chip_matrix2(elementType, image.NativePtr, chipLocation.NativePtr, chip.MatrixElementType.ToNativeMatrixElementType(), type.ToNativeInterpolationTypes(), chip.NativePtr); switch (ret) { case Native.ErrorType.InputElementTypeNotSupport: throw new ArgumentException($"{image.MatrixElementType} is not supported."); case Native.ErrorType.OutputElementTypeNotSupport: throw new ArgumentException($"{chip.MatrixElementType} is not supported."); } return(chip); }
/// <summary> /// This function saves matrix to disk as JPEG (Joint Photographic Experts Group) file. /// </summary> /// <typeparam name="T">The type of element in the matrix.</typeparam> /// <param name="matrix">The matrix.</param> /// <param name="path">A string that contains the name of the file to which to save matrix.</param> /// <param name="quality">The quality of file. It must be 0 - 100. The default value is 75.</param> /// <exception cref="ArgumentException">The specified type of matrix is not supported.</exception> /// <exception cref="ArgumentException">The <see cref="MatrixBase.TemplateRows"/> or <see cref="MatrixBase.TemplateColumns"/> is not supported.</exception> /// <exception cref="ArgumentException"><see cref="TwoDimensionObjectBase.Rows"/> or <see cref="TwoDimensionObjectBase.Columns"/> are less than or equal to zero.</exception> /// <exception cref="ArgumentNullException"><paramref name="matrix"/> or <paramref name="path"/> is null.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="quality"/> is less than zero or greater than 100.</exception> /// <exception cref="ObjectDisposedException"><paramref name="matrix"/> is disposed.</exception> public static void SaveJpeg(MatrixBase matrix, string path, int quality = 75) { if (matrix == null) { throw new ArgumentNullException(nameof(matrix)); } matrix.ThrowIfDisposed(); if (path == null) { throw new ArgumentNullException(nameof(path)); } if (matrix.Rows <= 0 || matrix.Columns <= 0) { throw new ArgumentException($"{nameof(matrix.Columns)} and {nameof(matrix.Rows)} is less than or equal to zero.", nameof(matrix)); } if (quality < 0) { throw new ArgumentOutOfRangeException(nameof(quality), $"{nameof(quality)} is less than zero."); } if (quality > 100) { throw new ArgumentOutOfRangeException(nameof(quality), $"{nameof(quality)} is greater than 100."); } var str = Encoding.GetBytes(path); var matrixElementType = matrix.MatrixElementType.ToNativeMatrixElementType(); var ret = NativeMethods.save_jpeg_matrix(matrixElementType, matrix.NativePtr, matrix.TemplateRows, matrix.TemplateColumns, str, quality); switch (ret) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{matrix.MatrixElementType} is not supported."); case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport: throw new ArgumentException($"{nameof(matrix.TemplateColumns)} or {nameof(matrix.TemplateRows)} is not supported."); } }
public static Rectangle GetRect(MatrixBase matrix) { if (matrix == null) { throw new ArgumentNullException(nameof(matrix)); } matrix.ThrowIfDisposed(); var type = matrix.MatrixElementType.ToNativeMatrixElementType(); var ret = NativeMethods.rectangle_get_rect_matrix(type, matrix.NativePtr, matrix.TemplateRows, matrix.TemplateColumns, out var rect); if (ret == NativeMethods.ErrorType.MatrixElementTypeNotSupport) { throw new ArgumentException($"{matrix.MatrixElementType} is not supported."); } return(new Rectangle(rect)); }
public void Train(MatrixBase exampleIn, double exampleOut) { if (exampleIn == null) { throw new ArgumentNullException(nameof(exampleIn)); } exampleIn.ThrowIfDisposed(); var kernelType = this._MultilayerPerceptronKernelType.ToNativeMlpKernelType(); var type = exampleIn.MatrixElementType.ToNativeMatrixElementType(); var ret = Dlib.Native.mlp_kernel_train(kernelType, this.NativePtr, type, exampleIn.NativePtr, exampleOut); switch (ret) { case Dlib.Native.ErrorType.InputElementTypeNotSupport: throw new ArgumentException($"{type} is not supported."); case Dlib.Native.ErrorType.MlpKernelNotSupport: throw new ArgumentException($"{kernelType} is not supported."); } }
public Rectangle[] Operator(MatrixBase image, double threshold = 0d) { this.ThrowIfDisposed(); if (image == null) { throw new ArgumentNullException(nameof(image)); } image.ThrowIfDisposed(); using (var dets = new StdVector <Rectangle>()) { var inType = image.MatrixElementType.ToNativeMatrixElementType(); var ret = Native.frontal_face_detector_matrix_operator(this.NativePtr, inType, image.NativePtr, threshold, dets.NativePtr); switch (ret) { case Dlib.Native.ErrorType.InputElementTypeNotSupport: throw new ArgumentException($"Input {inType} is not supported."); } return(dets.ToArray()); } }
public static Array <Matrix <T> > ExtractImageChips <T>(MatrixBase image, IEnumerable <ChipDetails> chipLocations) where T : struct { if (image == null) { throw new ArgumentNullException(nameof(image)); } if (chipLocations == null) { throw new ArgumentNullException(nameof(chipLocations)); } if (chipLocations.Any(chip => chip == null || chip.IsDisposed || !chip.IsValid())) { throw new ArgumentException($"{nameof(chipLocations)} includes invalid item."); } image.ThrowIfDisposed(); using (var vectorOfChips = new StdVector <ChipDetails>(chipLocations)) { var array = new Array <Matrix <T> >(); var matrixElementType = image.MatrixElementType.ToNativeMatrixElementType(); var ret = NativeMethods.extract_image_chips_matrix(matrixElementType, image.NativePtr, vectorOfChips.NativePtr, array.MatrixElementTypes.ToNativeMatrixElementType(), array.NativePtr); switch (ret) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{image.MatrixElementType} is not supported."); } return(array); } }
public static void AssignImage(MatrixBase src, MatrixBase dest) { if (src == null) { throw new ArgumentNullException(nameof(src)); } if (dest == null) { throw new ArgumentNullException(nameof(dest)); } src.ThrowIfDisposed(nameof(src)); dest.ThrowIfDisposed(nameof(dest)); var inType = src.MatrixElementType.ToNativeMatrixElementType(); var outType = dest.MatrixElementType.ToNativeMatrixElementType(); var ret = Native.assign_image_matrix(outType, dest.NativePtr, inType, src.NativePtr); switch (ret) { case Native.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{outType} is not supported."); } }