Exemplo n.º 1
0
        public static unsafe IEnumerable <float> GetPixelAsFloats(Mat source, uint column, uint row)
        {
            OpenCvSharp.MatType format = source.Type();
            var depth = format.Depth;

            uint channelCount = (uint)source.Channels();

            if (channelCount == 0)
            {
                return(new float[0]);
            }

            uint width  = (uint)source.Width;
            uint height = (uint)source.Height;

            float[] output = new float[(int)channelCount];

            row    %= height;
            column %= width;

            switch (depth)
            {
            case OpenCvSharp.MatType.CV_8U:
            {
                byte *d = (byte *)source.Data.ToPointer();
                for (uint channel = 0; channel < channelCount; channel++)
                {
                    output[(int)channel] = (float)d[(column + row * width) * channelCount + channel];
                }
                break;
            }

            case OpenCvSharp.MatType.CV_32F:
            {
                float *d = (float *)source.Data.ToPointer();
                for (uint channel = 0; channel < channelCount; channel++)
                {
                    output[(int)channel] = (float)d[(column + row * width) * channelCount + channel];
                }
                break;
            }

            case OpenCvSharp.MatType.CV_64F:
            {
                double *d = (double *)source.Data.ToPointer();
                for (uint channel = 0; channel < channelCount; channel++)
                {
                    output[(int)channel] = (float)d[(column + row * width) * channelCount + channel];
                }
                break;
            }
            }
            return(output);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 指定したサイズ・型の2次元の行列として初期化
        /// </summary>
        /// <param name="rows">2次元配列における行数.</param>
        /// <param name="cols">2次元配列における列数.</param>
        /// <param name="type">配列の型.1-4 チャンネルの行列を作成するには MatType.CV_8UC1, ..., CV_64FC4 を,
        /// マルチチャンネルの行列を作成するには,MatType.CV_8UC(n), ..., CV_64FC(n) を利用してください.</param>
#else
        /// <summary>
        /// constructs 2D matrix of the specified size and type
        /// </summary>
        /// <param name="rows">Number of rows in a 2D array.</param>
        /// <param name="cols">Number of columns in a 2D array.</param>
        /// <param name="type">Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices,
        /// or MatType. CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.</param>
#endif
        protected Mat(int rows, int cols, MatType type)
            : base(rows, cols, type)
        {
        }
Exemplo n.º 3
0
        /// <summary>
        /// N次元行列として初期化
        /// </summary>
        /// <param name="sizes">n-次元配列の形状を表す,整数型の配列.</param>
        /// <param name="type">配列の型.1-4 チャンネルの行列を作成するには MatType.CV_8UC1, ..., CV_64FC4 を,
        /// マルチチャンネルの行列を作成するには,MatType.CV_8UC(n), ..., CV_64FC(n) を利用してください.</param>
#else
        /// <summary>
        /// constructs n-dimensional matrix
        /// </summary>
        /// <param name="sizes">Array of integers specifying an n-dimensional array shape.</param>
        /// <param name="type">Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices,
        /// or MatType. CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.</param>
#endif
        protected Mat(IEnumerable <int> sizes, MatType type)
            : base(sizes, type)
        {
        }
Exemplo n.º 4
0
        /// <summary>
        /// N次元行列として初期化
        /// </summary>
        /// <param name="sizes">n-次元配列の形状を表す,整数型の配列.</param>
        /// <param name="type">配列の型.1-4 チャンネルの行列を作成するには MatType.CV_8UC1, ..., CV_64FC4 を,
        /// マルチチャンネルの行列を作成するには,MatType.CV_8UC(n), ..., CV_64FC(n) を利用してください.</param>
        /// <param name="s">各行列要素を初期化するオプション値.初期化の後ですべての行列要素を特定の値にセットするには,
        /// コンストラクタの後で,SetTo(Scalar value) メソッドを利用してください.</param>
#else
        /// <summary>
        /// constructs n-dimensional matrix
        /// </summary>
        /// <param name="sizes">Array of integers specifying an n-dimensional array shape.</param>
        /// <param name="type">Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices,
        /// or MatType. CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.</param>
        /// <param name="s">An optional value to initialize each matrix element with.
        /// To set all the matrix elements to the particular value after the construction, use SetTo(Scalar s) method .</param>
#endif
        protected Mat(IEnumerable <int> sizes, MatType type, Scalar s)
            : base(sizes, type, s)
        {
        }
Exemplo n.º 5
0
        /// <summary>
        /// 利用者が別に確保したデータで初期化
        /// </summary>
        /// <param name="rows">2次元配列における行数.</param>
        /// <param name="cols">2次元配列における列数.</param>
        /// <param name="type">配列の型.1-4 チャンネルの行列を作成するには MatType.CV_8UC1, ..., CV_64FC4 を,
        /// マルチチャンネルの行列を作成するには,MatType.CV_8UC(n), ..., CV_64FC(n) を利用してください.</param>
        /// <param name="data">ユーザデータへのポインタ. data と step パラメータを引数にとる
        /// 行列コンストラクタは,行列データ領域を確保しません.代わりに,指定のデータを指し示す
        /// 行列ヘッダを初期化します.つまり,データのコピーは行われません.
        /// この処理は,非常に効率的で,OpenCV の関数を利用して外部データを処理することができます.
        /// 外部データが自動的に解放されることはありませんので,ユーザが解放する必要があります.</param>
        /// <param name="step">行列の各行が占めるバイト数を指定できます.
        /// この値は,各行の終端にパディングバイトが存在すれば,それも含みます.
        /// このパラメータが指定されない場合,パディングは存在しないとみなされ,
        /// 実際の step は cols*elemSize() として計算されます.</param>
#else
        /// <summary>
        /// constructor for matrix headers pointing to user-allocated data
        /// </summary>
        /// <param name="rows">Number of rows in a 2D array.</param>
        /// <param name="cols">Number of columns in a 2D array.</param>
        /// <param name="type">Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices,
        /// or MatType. CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.</param>
        /// <param name="data">Pointer to the user data. Matrix constructors that take data and step parameters do not allocate matrix data.
        /// Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied.
        /// This operation is very efficient and can be used to process external data using OpenCV functions.
        /// The external data is not automatically deallocated, so you should take care of it.</param>
        /// <param name="step">Number of bytes each matrix row occupies. The value should include the padding bytes at the end of each row, if any.
        /// If the parameter is missing (set to AUTO_STEP ), no padding is assumed and the actual step is calculated as cols*elemSize() .</param>
#endif
        protected Mat(int rows, int cols, MatType type, Array data, long step = 0)
            : base(rows, cols, type, data, step)
        {
        }
Exemplo n.º 6
0
        /// <summary>
        /// 利用者が別に確保したデータで初期化
        /// </summary>
        /// <param name="sizes">n-次元配列の形状を表す,整数型の配列.</param>
        /// <param name="type">配列の型.1-4 チャンネルの行列を作成するには MatType.CV_8UC1, ..., CV_64FC4 を,
        /// マルチチャンネルの行列を作成するには,MatType.CV_8UC(n), ..., CV_64FC(n) を利用してください.</param>
        /// <param name="data">ユーザデータへのポインタ. data と step パラメータを引数にとる
        /// 行列コンストラクタは,行列データ領域を確保しません.代わりに,指定のデータを指し示す
        /// 行列ヘッダを初期化します.つまり,データのコピーは行われません.
        /// この処理は,非常に効率的で,OpenCV の関数を利用して外部データを処理することができます.
        /// 外部データが自動的に解放されることはありませんので,ユーザが解放する必要があります.</param>
        /// <param name="steps">多次元配列における ndims-1 個のステップを表す配列
        /// (最後のステップは常に要素サイズになります).これが指定されないと,
        /// 行列は連続したものとみなされます.</param>
#else
        /// <summary>
        /// constructor for matrix headers pointing to user-allocated data
        /// </summary>
        /// <param name="sizes">Array of integers specifying an n-dimensional array shape.</param>
        /// <param name="type">Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices,
        /// or MatType. CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.</param>
        /// <param name="data">Pointer to the user data. Matrix constructors that take data and step parameters do not allocate matrix data.
        /// Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied.
        /// This operation is very efficient and can be used to process external data using OpenCV functions.
        /// The external data is not automatically deallocated, so you should take care of it.</param>
        /// <param name="steps">Array of ndims-1 steps in case of a multi-dimensional array (the last step is always set to the element size).
        /// If not specified, the matrix is assumed to be continuous.</param>
#endif
        protected Mat(IEnumerable <int> sizes, MatType type, Array data, IEnumerable <long> steps = null)
            : base(sizes, type, data, steps)
        {
        }
Exemplo n.º 7
0
        /// <summary>
        /// 指定したサイズ・型の2次元の行列で、要素をスカラー値で埋めて初期化
        /// </summary>
        /// <param name="rows">2次元配列における行数.</param>
        /// <param name="cols">2次元配列における列数.</param>
        /// <param name="type">配列の型.1-4 チャンネルの行列を作成するには MatType.CV_8UC1, ..., CV_64FC4 を,
        /// マルチチャンネルの行列を作成するには,MatType.CV_8UC(n), ..., CV_64FC(n) を利用してください.</param>
        /// <param name="s">各行列要素を初期化するオプション値.初期化の後ですべての行列要素を特定の値にセットするには,
        /// コンストラクタの後で,SetTo(Scalar value) メソッドを利用してください.</param>
#else
        /// <summary>
        /// constucts 2D matrix and fills it with the specified Scalar value.
        /// </summary>
        /// <param name="rows">Number of rows in a 2D array.</param>
        /// <param name="cols">Number of columns in a 2D array.</param>
        /// <param name="type">Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices,
        /// or MatType. CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.</param>
        /// <param name="s">An optional value to initialize each matrix element with.
        /// To set all the matrix elements to the particular value after the construction, use SetTo(Scalar s) method .</param>
#endif
        protected Mat(int rows, int cols, MatType type, Scalar s)
            : base(rows, cols, type, s)
        {
        }
Exemplo n.º 8
0
        /// <summary>
        /// 指定したサイズ・型の2次元の行列で、要素をスカラー値で埋めて初期化
        /// </summary>
        /// <param name="size"> 2 次元配列のサイズ: Size(cols, rows) . Size() コンストラクタでは,行数と列数が逆順になっていることに注意してください.</param>
        /// <param name="type">配列の型.1-4 チャンネルの行列を作成するには MatType.CV_8UC1, ..., CV_64FC4 を,
        /// マルチチャンネルの行列を作成するには,MatType.CV_8UC(n), ..., CV_64FC(n) を利用してください.</param>
        /// <param name="s">各行列要素を初期化するオプション値.初期化の後ですべての行列要素を特定の値にセットするには,
        /// コンストラクタの後で,SetTo(Scalar value) メソッドを利用してください.</param>
#else
        /// <summary>
        /// constucts 2D matrix and fills it with the specified Scalar value.
        /// </summary>
        /// <param name="size">2D array size: Size(cols, rows) . In the Size() constructor,
        /// the number of rows and the number of columns go in the reverse order.</param>
        /// <param name="type">Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices,
        /// or CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices.</param>
        /// <param name="s">An optional value to initialize each matrix element with.
        /// To set all the matrix elements to the particular value after the construction, use SetTo(Scalar s) method .</param>
#endif
        protected Mat(Size size, MatType type, Scalar s)
            : base(size, type, s)
        {
        }
Exemplo n.º 9
0
 /// <summary>
 /// Ensures that size of the given matrix is not less than (rows, cols) size
 /// and matrix type is match specified one too
 /// </summary>
 /// <param name="size">Number of rows and columns in a 2D array.</param>
 /// <param name="type">Array type.</param>
 /// <param name="m"></param>
 public static void EnsureSizeIsEnough(Size size, MatType type, GpuMat m)
 {
     ThrowIfGpuNotAvailable();
     EnsureSizeIsEnough(size.Height, size.Width, type, m);
 }
Exemplo n.º 10
0
        /// <summary>
        /// 指定したサイズ・型の2次元の行列として初期化
        /// </summary>
        /// <param name="size">2次元配列のサイズ: Size(cols, rows) . Size コンストラクタでは,行数と列数が逆順になっていることに注意してください.</param>
        /// <param name="type">配列の型.1-4 チャンネルの行列を作成するには MatType.CV_8UC1, ..., CV_64FC4 を,
        /// マルチチャンネルの行列を作成するには,MatType.CV_8UC(n), ..., CV_64FC(n) を利用してください.</param>
#else
        /// <summary>
        /// constructs 2D matrix of the specified size and type
        /// </summary>
        /// <param name="size">2D array size: Size(cols, rows) . In the Size() constructor,
        /// the number of rows and the number of columns go in the reverse order.</param>
        /// <param name="type">Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices,
        /// or MatType.CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.</param>
#endif
        protected Mat(Size size, MatType type)
            : base(size, type)
        {
        }
Exemplo n.º 11
0
 /// <summary>
 /// Creates continuous GPU matrix
 /// </summary>
 /// <param name="size">Number of rows and columns in a 2D array.</param>
 /// <param name="type">Array type.</param>
 /// <returns></returns>
 public static GpuMat CreateContinuous(Size size, MatType type)
 {
     ThrowIfGpuNotAvailable();
     return(CreateContinuous(size.Height, size.Width, type));
 }
Exemplo n.º 12
0
 /// <summary>
 /// Creates continuous GPU matrix
 /// </summary>
 /// <param name="size">Number of rows and columns in a 2D array.</param>
 /// <param name="type">Array type.</param>
 /// <param name="m"></param>
 public static void CreateContinuous(Size size, MatType type, GpuMat m)
 {
     ThrowIfGpuNotAvailable();
     CreateContinuous(size.Height, size.Width, type, m);
 }
Exemplo n.º 13
0
 public Mat NewMat(Size size, MatType matType, Scalar s)
 {
     return(T(new Mat(size, matType, s)));
 }
 public UnsupportedMatTypeException(OpenCvSharp.MatType type)
     : base($"Unsupported mat type {type}")
 {
     Type = type;
 }
Exemplo n.º 15
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private static MatType EstimateType(Type t)
        {
#if NET20 || NET40
            if (!t.IsValueType)
#else
            if (!t.GetTypeInfo().IsValueType)
#endif
            { throw new ArgumentException(); }

            // Primitive types
#if false
            if (t == typeof(byte))
            {
                return(MatType.CV_8UC1);
            }
            if (t == typeof(sbyte))
            {
                return(MatType.CV_8SC1);
            }
            if (t == typeof(ushort) || t == typeof(char))
            {
                return(MatType.CV_16UC1);
            }
            if (t == typeof(short))
            {
                return(MatType.CV_16SC1);
            }
            if (t == typeof(int) || t == typeof(uint))
            {
                return(MatType.CV_32SC1);
            }
            if (t == typeof(float))
            {
                return(MatType.CV_32FC1);
            }
            if (t == typeof(double))
            {
                return(MatType.CV_64FC1);
            }
#else
            var code = System.Type.GetTypeCode(t);
            switch (code)
            {
            case TypeCode.Byte:
                return(MatType.CV_8UC1);

            case TypeCode.SByte:
                return(MatType.CV_8SC1);

            case TypeCode.UInt16:
                return(MatType.CV_16UC1);

            case TypeCode.Int16:
            case TypeCode.Char:
                return(MatType.CV_16SC1);

            case TypeCode.UInt32:
            case TypeCode.Int32:
                return(MatType.CV_32SC1);

            case TypeCode.Single:
                return(MatType.CV_32FC1);

            case TypeCode.Double:
                return(MatType.CV_64FC1);
            }
#endif

            // OpenCV struct types
            if (t == typeof(Point))
            {
                return(MatType.CV_32SC2);
            }
            if (t == typeof(Point2f))
            {
                return(MatType.CV_32FC2);
            }
            if (t == typeof(Point2d))
            {
                return(MatType.CV_64FC2);
            }
            if (t == typeof(Point3i))
            {
                return(MatType.CV_32SC3);
            }
            if (t == typeof(Point3f))
            {
                return(MatType.CV_32FC3);
            }
            if (t == typeof(Point3d))
            {
                return(MatType.CV_32FC3);
            }
            if (t == typeof(Range))
            {
                return(MatType.CV_32SC2);
            }
            if (t == typeof(Rangef))
            {
                return(MatType.CV_32FC2);
            }
            if (t == typeof(Rect))
            {
                return(MatType.CV_32SC4);
            }
            if (t == typeof(Size))
            {
                return(MatType.CV_32SC2);
            }
            if (t == typeof(Size2f))
            {
                return(MatType.CV_32FC2);
            }

            if (t == typeof(Vec2b))
            {
                return(MatType.CV_8UC2);
            }
            if (t == typeof(Vec3b))
            {
                return(MatType.CV_8UC3);
            }
            if (t == typeof(Vec4b))
            {
                return(MatType.CV_8UC4);
            }
            if (t == typeof(Vec6b))
            {
                return(MatType.CV_8UC(6));
            }
            if (t == typeof(Vec2s))
            {
                return(MatType.CV_16SC2);
            }
            if (t == typeof(Vec3s))
            {
                return(MatType.CV_16SC3);
            }
            if (t == typeof(Vec4s))
            {
                return(MatType.CV_16SC4);
            }
            if (t == typeof(Vec6s))
            {
                return(MatType.CV_16SC(6));
            }
            if (t == typeof(Vec2w))
            {
                return(MatType.CV_16UC2);
            }
            if (t == typeof(Vec3w))
            {
                return(MatType.CV_16UC3);
            }
            if (t == typeof(Vec4w))
            {
                return(MatType.CV_16UC4);
            }
            if (t == typeof(Vec6w))
            {
                return(MatType.CV_16UC(6));
            }
            if (t == typeof(Vec2i))
            {
                return(MatType.CV_32SC2);
            }
            if (t == typeof(Vec3i))
            {
                return(MatType.CV_32SC3);
            }
            if (t == typeof(Vec4i))
            {
                return(MatType.CV_32SC4);
            }
            if (t == typeof(Vec6i))
            {
                return(MatType.CV_32SC(6));
            }
            if (t == typeof(Vec2f))
            {
                return(MatType.CV_32FC2);
            }
            if (t == typeof(Vec3f))
            {
                return(MatType.CV_32FC3);
            }
            if (t == typeof(Vec4f))
            {
                return(MatType.CV_32FC4);
            }
            if (t == typeof(Vec6f))
            {
                return(MatType.CV_32FC(6));
            }
            if (t == typeof(Vec2d))
            {
                return(MatType.CV_64FC2);
            }
            if (t == typeof(Vec3d))
            {
                return(MatType.CV_64FC3);
            }
            if (t == typeof(Vec4d))
            {
                return(MatType.CV_64FC4);
            }
            if (t == typeof(Vec6d))
            {
                return(MatType.CV_64FC(6));
            }

            throw new ArgumentException("Not supported value type for InputArray");
        }