예제 #1
0
        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.");
            }
        }
예제 #2
0
        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();
            }
        }
예제 #3
0
        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();
            }
        }
예제 #4
0
        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));
        }
예제 #5
0
        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));
            }
        }
예제 #6
0
        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));
        }
예제 #7
0
        /// <summary>
        /// This function loads image file into an <see cref="Matrix{T}"/>.
        /// </summary>
        /// <typeparam name="T">The type of element in the matrix.</typeparam>
        /// <param name="path">A string that contains the name of the file from which to create the <see cref="Matrix{T}"/>.</param>
        /// <returns>The <see cref="Matrix{T}"/> this method creates.</returns>
        /// <exception cref="ArgumentException">The specified type of matrix is not supported.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
        /// <exception cref="FileNotFoundException">The specified file does not exist.</exception>
        /// <exception cref="ImageLoadException">Failed to load image on dlib.</exception>
        public static Matrix <T> LoadImageAsMatrix <T>(string path)
            where T : struct
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException($"The specified {nameof(path)} does not exist.", path);
            }

            if (!MatrixBase.TryParse(typeof(T), out var type))
            {
                throw new NotSupportedException($"{typeof(T).Name} does not support");
            }

            var str = Encoding.GetBytes(path);

            var matrixElementType = type.ToNativeMatrixElementType();
            var ret = NativeMethods.load_image_matrix(matrixElementType, str, out var matrix, out var errorMessage);

            switch (ret)
            {
            case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                throw new ArgumentException($"{type} is not supported.");

            case NativeMethods.ErrorType.GeneralFileImageLoad:
                throw new ImageLoadException(path, StringHelper.FromStdString(errorMessage));
            }

            return(new Matrix <T>(matrix));
        }
예제 #8
0
        /// <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.");
            }
        }
예제 #9
0
        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.");
            }
        }
예제 #10
0
        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));
        }
예제 #11
0
        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.");
            }
        }
예제 #12
0
파일: Dlib.cs 프로젝트: TrojanOlx/AI
        /// <summary>
        /// This function loads image file into an <see cref="Matrix{T}"/>.
        /// </summary>
        /// <typeparam name="T">The type of element in the matrix.</typeparam>
        /// <param name="path">A string that contains the name of the file from which to create the <see cref="Matrix{T}"/>.</param>
        /// <returns>The <see cref="Matrix{T}"/> this method creates.</returns>
        /// <exception cref="ArgumentException">The specified type of matrix is not supported.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
        /// <exception cref="FileNotFoundException">The specified file does not exist.</exception>
        public static Matrix <T> LoadImageAsMatrix <T>(string path)
            where T : struct
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException($"The specified {nameof(path)} does not exist.", path);
            }

            if (!MatrixBase.TryParse(typeof(T), out var type))
            {
                throw new NotSupportedException($"{typeof(T).Name} does not support");
            }

            var str = Dlib.Encoding.GetBytes(path);

            var matrixElementType = type.ToNativeMatrixElementType();
            var ret = NativeMethods.load_image_matrix(matrixElementType, str, out var matrix);

            if (ret == NativeMethods.ErrorType.MatrixElementTypeNotSupport)
            {
                throw new ArgumentException($"{type} is not supported.");
            }

            return(new Matrix <T>(matrix));
        }
예제 #13
0
        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));
        }
예제 #14
0
파일: Dlib.cs 프로젝트: zhuxb711/DlibDotNet
        public static IEnumerable <Rectangle> EvaluateDetectors <T, U>(IEnumerable <ObjectDetector <ScanFHogPyramid <T, U> > > detectors,
                                                                       MatrixBase matrix,
                                                                       double adjustThreshold = 0)
            where T : class
            where U : class
        {
            if (detectors == null)
            {
                throw new ArgumentNullException(nameof(detectors));
            }
            if (matrix == null)
            {
                throw new ArgumentNullException(nameof(matrix));
            }
            var count = detectors.Count();

            if (count == 0)
            {
                throw new ArgumentException();
            }

            detectors.ThrowIfDisposed();

            var @params = detectors.Select(detector => detector.GetFHogPyramidParameter());

            var param = @params.First();
            var not   = @params.Any(p => p.PyramidRate != param.PyramidRate ||
                                    p.PyramidType != param.PyramidType ||
                                    p.FeatureExtractorType != param.FeatureExtractorType);

            if (not)
            {
                throw new ArgumentException();
            }

            var detectorArray = detectors.Select(det => det.NativePtr).ToArray();
            var elementType   = matrix.MatrixElementType.ToNativeMatrixElementType();
            var ret           = NativeMethods.scan_fhog_pyramid_evaluate_detectors(param.PyramidType,
                                                                                   param.PyramidRate,
                                                                                   param.FeatureExtractorType,
                                                                                   detectorArray,
                                                                                   count,
                                                                                   elementType,
                                                                                   matrix.NativePtr,
                                                                                   adjustThreshold,
                                                                                   out var vector);

            switch (ret)
            {
            case NativeMethods.ErrorType.FHogNotSupportExtractor:
            case NativeMethods.ErrorType.PyramidNotSupportRate:
            case NativeMethods.ErrorType.PyramidNotSupportType:
                throw new NotSupportedException();
            }

            using (var tmp = new StdVector <Rectangle>(vector))
                return(tmp.ToArray());
        }
예제 #15
0
        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));
        }
예제 #16
0
        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);
        }
예제 #17
0
        public Matrix()
        {
            if (!MatrixBase.TryParse(typeof(TElement), out var type))
            {
                throw new NotSupportedException($"{typeof(TElement).Name} does not support");
            }

            this._MatrixElementTypes = type;
            this._ElementType        = type.ToNativeMatrixElementType();
            this.NativePtr           = Dlib.Native.matrix_new(this._ElementType);

            this._Indexer = this.CreateIndexer(type);
        }
        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));
        }
예제 #19
0
        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.");
            }
        }
예제 #20
0
        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);
        }
예제 #21
0
        internal Matrix(IntPtr ptr, bool isEnabledDispose = true)
            : base(isEnabledDispose)
        {
            if (ptr == IntPtr.Zero)
            {
                throw new ArgumentException("Can not pass IntPtr.Zero", nameof(ptr));
            }

            if (!MatrixBase.TryParse(typeof(TElement), out var type))
            {
                throw new NotSupportedException($"{typeof(TElement).Name} does not support");
            }

            this.NativePtr           = ptr;
            this._MatrixElementTypes = type;
            this._ElementType        = type.ToNativeMatrixElementType();

            this._Indexer = this.CreateIndexer(type);
        }
예제 #22
0
        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);
        }
예제 #24
0
        public Matrix(int row, int column)
        {
            if (!MatrixBase.TryParse(typeof(TElement), out var type))
            {
                throw new NotSupportedException($"{typeof(TElement).Name} does not support");
            }
            if (row < 0)
            {
                throw new ArgumentOutOfRangeException($"{nameof(row)}", $"{nameof(row)} should be positive value.");
            }
            if (column < 0)
            {
                throw new ArgumentOutOfRangeException($"{nameof(column)}", $"{nameof(column)} should be positive value.");
            }

            this._MatrixElementTypes = type;
            this._ElementType        = type.ToNativeMatrixElementType();
            this.NativePtr           = Dlib.Native.matrix_new1(this._ElementType, row, column);

            this._Indexer = this.CreateIndexer(type);
        }
예제 #25
0
        public static MatrixOp Heatmap(MatrixBase image)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            var matrixElementType = image.MatrixElementType.ToNativeMatrixElementType();
            var ret = Native.heatmap_matrix(matrixElementType,
                                            image.NativePtr,
                                            image.TemplateRows,
                                            image.TemplateColumns,
                                            out var matrix);

            if (ret == Native.ErrorType.MatrixElementTypeNotSupport)
            {
                throw new ArgumentException($"{image.MatrixElementType} is not supported.");
            }

            return(new MatrixOp(Native.ElementType.OpJet, image.MatrixElementType, matrix));
        }
예제 #26
0
        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);
        }
예제 #27
0
        /// <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.");
            }
        }
예제 #28
0
        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.");
            }
        }
예제 #29
0
        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));
        }
예제 #30
0
        public static MatrixOp Jet(MatrixBase image, double maxValue, double minValue = 0)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            var matrixElementType = image.MatrixElementType.ToNativeMatrixElementType();
            var ret = NativeMethods.jet2_matrix(matrixElementType,
                                                image.NativePtr,
                                                image.TemplateRows,
                                                image.TemplateColumns,
                                                maxValue,
                                                minValue,
                                                out var matrix);

            if (ret == NativeMethods.ErrorType.MatrixElementTypeNotSupport)
            {
                throw new ArgumentException($"{image.MatrixElementType} is not supported.");
            }

            return(new MatrixOp(NativeMethods.ElementType.OpJet, image.MatrixElementType, matrix));
        }