예제 #1
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));
        }
예제 #2
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));
        }
예제 #3
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);
        }
예제 #4
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);
        }
예제 #5
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);
        }
예제 #6
0
        private static StdVectorImp <TItem> CreateImp(object[] param = null)
        {
            if (SupportTypes.TryGetValue(typeof(TItem), out var type))
            {
                switch (type)
                {
                case ElementTypes.Int32:
                    return(new StdVectorInt32Imp() as StdVectorImp <TItem>);

                case ElementTypes.UInt32:
                    return(new StdVectorUInt32Imp() as StdVectorImp <TItem>);

                case ElementTypes.Long:
                    return(new StdVectorLongImp() as StdVectorImp <TItem>);

                case ElementTypes.VectorDouble:
                    return(new StdVectorVectorDoubleImp() as StdVectorImp <TItem>);

                case ElementTypes.Rectangle:
                    return(new StdVectorRectangleImp() as StdVectorImp <TItem>);

                case ElementTypes.ChipDetails:
                    return(new StdVectorChipDetailsImp() as StdVectorImp <TItem>);

                case ElementTypes.FullObjectDetection:
                    return(new StdVectorFullObjectDetectionImp() as StdVectorImp <TItem>);

                case ElementTypes.ImageWindowOverlayLine:
                    return(new StdVectorImageWindowOverlayLineImp() as StdVectorImp <TItem>);

                case ElementTypes.PerspectiveWindowOverlayDot:
                    return(new StdVectorPerspectiveWindowOverlayDotImp() as StdVectorImp <TItem>);

                case ElementTypes.MModRect:
                    return(new StdVectorMModRectImp() as StdVectorImp <TItem>);

                case ElementTypes.SurfPoint:
                    return(new StdVectorSurfPointImp() as StdVectorImp <TItem>);

                case ElementTypes.SamplePair:
                    return(new StdVectorSamplePairImp() as StdVectorImp <TItem>);

                case ElementTypes.StdVectorRectangle:
                    return(new StdVectorStdVectorRectangleImp() as StdVectorImp <TItem>);

                case ElementTypes.StdVectorMModRect:
                    return(new StdVectorStdVectorMModRectImp() as StdVectorImp <TItem>);
                }
            }
            else
            {
                var t      = typeof(TItem);
                var matrix = typeof(MatrixBase);
                if (matrix.IsAssignableFrom(t))
                {
                    var arg = GenericHelper.GetTypeParameter(t);
                    if (MatrixBase.TryParse(arg, out var r))
                    {
                        var templateRows    = 0;
                        var templateColumns = 0;
                        if (param != null && param.Length == 1 && param[0] is int[] array)
                        {
                            templateRows    = array[0];
                            templateColumns = array[1];
                        }

                        switch (r)
                        {
                        case MatrixElementTypes.UInt8:
                            return(new StdVectorMatrixImp <byte>(templateRows, templateColumns) as StdVectorImp <TItem>);

                        case MatrixElementTypes.UInt16:
                            return(new StdVectorMatrixImp <ushort>(templateRows, templateColumns) as StdVectorImp <TItem>);

                        case MatrixElementTypes.UInt32:
                            return(new StdVectorMatrixImp <uint>(templateRows, templateColumns) as StdVectorImp <TItem>);

                        case MatrixElementTypes.Int8:
                            return(new StdVectorMatrixImp <sbyte>(templateRows, templateColumns) as StdVectorImp <TItem>);

                        case MatrixElementTypes.Int16:
                            return(new StdVectorMatrixImp <short>(templateRows, templateColumns) as StdVectorImp <TItem>);

                        case MatrixElementTypes.Int32:
                            return(new StdVectorMatrixImp <int>(templateRows, templateColumns) as StdVectorImp <TItem>);

                        case MatrixElementTypes.Float:
                            return(new StdVectorMatrixImp <float>(templateRows, templateColumns) as StdVectorImp <TItem>);

                        case MatrixElementTypes.Double:
                            return(new StdVectorMatrixImp <double>(templateRows, templateColumns) as StdVectorImp <TItem>);

                        case MatrixElementTypes.RgbPixel:
                            return(new StdVectorMatrixImp <RgbPixel>(templateRows, templateColumns) as StdVectorImp <TItem>);

                        case MatrixElementTypes.RgbAlphaPixel:
                            return(new StdVectorMatrixImp <RgbAlphaPixel>(templateRows, templateColumns) as StdVectorImp <TItem>);

                        case MatrixElementTypes.HsiPixel:
                            return(new StdVectorMatrixImp <HsiPixel>(templateRows, templateColumns) as StdVectorImp <TItem>);
                        }
                    }
                }
            }

            throw new ArgumentOutOfRangeException(nameof(type), type, null);
        }