Ejemplo n.º 1
0
        /// <summary>
        /// Load the Mat from file
        /// </summary>
        /// <param name="fileName">The name of the file</param>
        /// <param name="loadType">File loading method</param>
        public Mat(String fileName, CvEnum.ImreadModes loadType = ImreadModes.Color)
            : this(MatInvoke.cveMatCreate(), true, false)
        {
            using (CvString s = new CvString(fileName))
            {
                CvInvoke.cveImread(s, loadType, this);

                if (this.IsEmpty) //failed to load in the first attempt
                {
#if !(NETFX_CORE || UNITY_ANDROID || UNITY_IPHONE || UNITY_STANDALONE || UNITY_METRO || UNITY_EDITOR)
                    if (File.Exists(fileName))
                    {
                        //try again to see if this is a Unicode issue in the file name.
                        //Work around for Open CV ticket:
                        //https://github.com/Itseez/opencv/issues/4292
                        //https://github.com/Itseez/opencv/issues/4866

                        byte[] raw = File.ReadAllBytes(fileName);
                        CvInvoke.Imdecode(raw, loadType, this);

                        if (IsEmpty)
                        {
                            throw new ArgumentException(String.Format("Unable to decode file: {0}", fileName));
                        }
                    }
                    else
                    {
                        throw new ArgumentException(String.Format("File {0} do not exist", fileName));
                    }
#endif
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Copy data from managed array to this Mat
        /// </summary>
        /// <typeparam name="T">The type of managed data array</typeparam>
        /// <param name="data">The managed array where data will be copied from</param>
        public void SetTo <T>(T[] data)
        {
            Debug.Assert(data.Length == Total.ToInt32() * ElementSize / Toolbox.SizeOf <T>(), String.Format("Invalid data length, expecting {0} but was {1}", Total.ToInt32() * ElementSize / Toolbox.SizeOf <T>(), data.Length));
            GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);

            MatInvoke.cveMatCopyDataFrom(this, handle.AddrOfPinnedObject());
            handle.Free();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Computes a cross-product of two 3-element vectors.
        /// </summary>
        /// <param name="m">Another cross-product operand.</param>
        /// <returns>Cross-product of two 3-element vectors.</returns>
        public Mat Cross(IInputArray m)
        {
            Mat result = new Mat();

            using (InputArray iaM = m.GetInputArray())
                MatInvoke.cvMatCross(Ptr, iaM, result);
            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Release all the unmanaged memory associated with this object.
        /// </summary>
        protected override void DisposeObject()
        {
            if (_needDispose && _ptr != IntPtr.Zero)
            {
                MatInvoke.cvMatRelease(ref _ptr);
            }

            base.DisposeObject();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Copy data from this Mat to the managed array
        /// </summary>
        /// <typeparam name="T">The type of managed data array</typeparam>
        /// <param name="data">The managed array where data will be copied to.</param>
        public void CopyTo <T>(T[] data)
        {
            Debug.Assert(Toolbox.SizeOf <T>() * data.Length >= Total.ToInt32() * ElementSize,
                         String.Format("Size of data is not enough, required at least {0}, but was {1} ", Total.ToInt32() * ElementSize / Toolbox.SizeOf <T>(), data.Length));
            GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);

            MatInvoke.cveMatCopyDataTo(this, handle.AddrOfPinnedObject());
            handle.Free();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Convert this Mat to Image
        /// </summary>
        /// <typeparam name="TColor">The type of Color</typeparam>
        /// <typeparam name="TDepth">The type of Depth</typeparam>
        /// <returns>The image</returns>
        public Image <TColor, TDepth> ToImage <TColor, TDepth>(bool tryShareData = false)
            where TColor : struct, IColor
            where TDepth : new()
        {
            TColor c = new TColor();

            int numberOfChannels = NumberOfChannels;

            if (typeof(TDepth) == CvInvoke.GetDepthType(this.Depth) && c.Dimension == numberOfChannels)
            {
                //same color, same depth
                if (tryShareData)
                {
                    return(new Image <TColor, TDepth>(MatInvoke.cveMatToIplImage(Ptr)));
                }
                else
                {
                    Image <TColor, TDepth> img = new Image <TColor, TDepth>(Size);
                    CopyTo(img);
                    return(img);
                }
            }
            else if (typeof(TDepth) != CvInvoke.GetDepthType(this.Depth) && c.Dimension == numberOfChannels)
            {
                //different depth, same color
                Image <TColor, TDepth> result = new Image <TColor, TDepth>(Size);
                ConvertTo(result, CvInvoke.GetDepthType(typeof(TDepth)));
                return(result);
            }
            else if (typeof(TDepth) == CvInvoke.GetDepthType(this.Depth) && c.Dimension != numberOfChannels)
            {
                //same depth, different color
                Image <TColor, TDepth> result = new Image <TColor, TDepth>(Size);
                CvInvoke.CvtColor(
                    this, result,
                    numberOfChannels == 1 ? typeof(Gray) :
                    numberOfChannels == 3 ? typeof(Bgr) :
                    typeof(Bgra),
                    typeof(TColor));

                return(result);
            }
            else
            {
                //different color, different depth
                using (Mat tmp = new Mat())
                {
                    ConvertTo(tmp, CvInvoke.GetDepthType(typeof(TDepth)));
                    return(tmp.ToImage <TColor, TDepth>());
                }
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Pointer to the OutputArray
 /// </summary>
 public OutputArray GetOutputArray()
 {
     return(new OutputArray(MatInvoke.cveOutputArrayFromMat(_ptr)));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Changes the shape and/or the number of channels of a 2D matrix without copying the data.
 /// </summary>
 /// <param name="cn">New number of channels. If the parameter is 0, the number of channels remains the same.</param>
 /// <param name="rows">New number of rows. If the parameter is 0, the number of rows remains the same.</param>
 /// <returns>A new mat header that has different shape</returns>
 public Mat Reshape(int cn, int rows = 0)
 {
     return(new Mat(MatInvoke.cvMatReshape(Ptr, cn, rows), true, false));
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Computes a dot-product of two vectors.
 /// </summary>
 /// <param name="m">Another dot-product operand</param>
 /// <returns>The dot-product of two vectors.</returns>
 public double Dot(IInputArray m)
 {
     using (InputArray iaM = m.GetInputArray())
         return(MatInvoke.cvMatDot(Ptr, iaM));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Create an empty cv::Mat
 /// </summary>
 public Mat()
     : this(MatInvoke.cvMatCreate(), true, true)
 {
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Create a mat header for the specific ROI
 /// </summary>
 /// <param name="mat">The mat where the new Mat header will share data from</param>
 /// <param name="roi">The region of interest</param>
 public Mat(Mat mat, Rectangle roi)
     : this(MatInvoke.cvMatCreateFromRect(mat.Ptr, ref roi), true, true)
 {
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Convert this Mat to UMat
 /// </summary>
 /// <param name="access">Access type</param>
 /// <param name="usageFlags">Usage flags</param>
 /// <returns>The UMat</returns>
 public UMat GetUMat(CvEnum.AccessType access, UMat.Usage usageFlags = UMat.Usage.Default)
 {
     return(new UMat(MatInvoke.cvMatGetUMat(Ptr, access, usageFlags), true));
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Create a mat header for the specific ROI
 /// </summary>
 /// <param name="mat">The mat where the new Mat header will share data from</param>
 /// <param name="rowRange">The region of interest</param>
 /// <param name="colRange">The region of interest</param>
 public Mat(Mat mat, Range rowRange, Range colRange)
     : this(MatInvoke.cveMatCreateFromRange(mat.Ptr, ref rowRange, ref colRange), true, true)
 {
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Create multi-dimension mat using existing data.
 /// </summary>
 /// <param name="sizes">The sizes of each dimension</param>
 /// <param name="type">The type of data</param>
 /// <param name="data">The pointer to the unmanaged data</param>
 /// <param name="steps">The steps</param>
 public Mat(int[] sizes, CvEnum.DepthType type, IntPtr data, IntPtr[] steps = null)
     : this(MatInvoke.cveMatCreateMultiDimWithData(sizes, type, data, steps), true, true)
 {
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Pointer to the InputOutputArray
 /// </summary>
 public InputOutputArray GetInputOutputArray()
 {
     return(new InputOutputArray(MatInvoke.cveInputOutputArrayFromMat(_ptr)));
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Convert this Mat to Image
        /// </summary>
        /// <typeparam name="TColor">The type of Color</typeparam>
        /// <typeparam name="TDepth">The type of Depth</typeparam>
        /// <returns>The image</returns>
        public Image <TColor, TDepth> ToImage <TColor, TDepth>(bool tryShareData = false)
            where TColor : struct, IColor
            where TDepth : new()
        {
            TColor c = new TColor();

            int numberOfChannels = NumberOfChannels;

            if (typeof(TDepth) == CvInvoke.GetDepthType(this.Depth) && c.Dimension == numberOfChannels)
            {
                //same color, same depth
                if (tryShareData)
                {
                    return(new Image <TColor, TDepth>(MatInvoke.cveMatToIplImage(Ptr)));
                }
                else
                {
                    Image <TColor, TDepth> img = new Image <TColor, TDepth>(Size);
                    CopyTo(img);
                    return(img);
                }
            }
            else if (typeof(TDepth) != CvInvoke.GetDepthType(this.Depth) && c.Dimension == numberOfChannels)
            {
                //different depth, same color
                Image <TColor, TDepth> result = new Image <TColor, TDepth>(Size);
                ConvertTo(result, CvInvoke.GetDepthType(typeof(TDepth)));

                /*
                 * if (numberOfChannels == 1)
                 * {
                 * using (Image<Gray, TDepth> tmp = this.ToImage<Gray, TDepth>())
                 *    result.ConvertFrom(tmp);
                 * }
                 * else if (numberOfChannels == 3)
                 * {
                 * using (Image<Bgr, TDepth> tmp = this.ToImage<Bgr, TDepth>())
                 *    result.ConvertFrom(tmp);
                 * }
                 * else if (numberOfChannels == 4)
                 * {
                 * using (Image<Bgra, TDepth> tmp = this.ToImage<Bgra, TDepth>())
                 *    result.ConvertFrom(tmp);
                 * }
                 * else
                 * {
                 * throw new Exception("Unsupported conversion");
                 * }*/
                return(result);
            }
            else if (typeof(TDepth) == CvInvoke.GetDepthType(this.Depth) && c.Dimension != numberOfChannels)
            {
                //same depth, different color
                Image <TColor, TDepth> result = new Image <TColor, TDepth>(Size);
                CvInvoke.CvtColor(
                    this, result,
                    numberOfChannels == 1 ? typeof(Gray) :
                    numberOfChannels == 3 ? typeof(Bgr) :
                    typeof(Bgra),
                    typeof(TColor));

                /*
                 * CvEnum.DepthType depth = Depth;
                 * if (depth == CvEnum.DepthType.Cv8U)
                 * {
                 * using (Image<TColor, Byte> tmp = this.ToImage<TColor, Byte>())
                 *    result.ConvertFrom(tmp);
                 * }
                 * else if (depth == CvEnum.DepthType.Cv8S)
                 * {
                 * using (Image<TColor, SByte> tmp = this.ToImage<TColor, SByte>())
                 *    result.ConvertFrom(tmp);
                 * }
                 * else if (depth == CvEnum.DepthType.Cv16U)
                 * {
                 * using (Image<TColor, UInt16> tmp = this.ToImage<TColor, UInt16>())
                 *    result.ConvertFrom(tmp);
                 * }
                 * else if (depth == CvEnum.DepthType.Cv16S)
                 * {
                 * using (Image<TColor, Int16> tmp = this.ToImage<TColor, Int16>())
                 *    result.ConvertFrom(tmp);
                 * }
                 * else if (depth == CvEnum.DepthType.Cv32S)
                 * {
                 * using (Image<TColor, Int32> tmp = this.ToImage<TColor, Int32>())
                 *    result.ConvertFrom(tmp);
                 * }
                 * else if (depth == CvEnum.DepthType.Cv32F)
                 * {
                 * using (Image<TColor, float> tmp = this.ToImage<TColor, float>())
                 *    result.ConvertFrom(tmp);
                 * }
                 * else if (depth == CvEnum.DepthType.Cv64F)
                 * {
                 * using (Image<TColor, double> tmp = this.ToImage<TColor, double>())
                 *    result.ConvertFrom(tmp);
                 * }
                 * else
                 * {
                 * throw new Exception("Unsupported conversion");
                 * }*/
                return(result);
            }
            else
            {
                //different color, different depth
                using (Mat tmp = new Mat())
                {
                    ConvertTo(tmp, CvInvoke.GetDepthType(typeof(TDepth)));
                    return(tmp.ToImage <TColor, TDepth>());
                }
            }
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Convert this Mat to UMat
 /// </summary>
 /// <param name="access">Access type</param>
 /// <returns>The UMat</returns>
 public UMat ToUMat(CvEnum.AccessType access)
 {
     return(new UMat(MatInvoke.cvMatGetUMat(Ptr, access), true));
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Set the mat to the specific value
 /// </summary>
 /// <param name="value">The value to set to</param>
 /// <param name="mask">Optional mask</param>
 public void SetTo(IInputArray value, IInputArray mask = null)
 {
     using (InputArray iaValue = value.GetInputArray())
         using (InputArray iaMask = mask == null ? InputArray.GetEmpty() : mask.GetInputArray())
             MatInvoke.cvMatSetTo(Ptr, iaValue, iaMask);
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Allocates new array data if needed.
 /// </summary>
 /// <param name="rows">New number of rows.</param>
 /// <param name="cols">New number of columns.</param>
 /// <param name="type">New matrix element depth type.</param>
 /// <param name="channels">New matrix number of channels</param>
 public void Create(int rows, int cols, CvEnum.DepthType type, int channels)
 {
     MatInvoke.cvMatCreateData(_ptr, rows, cols, CvInvoke.MakeType(type, channels));
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Create a Mat header from existing 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">Mat element type</param>
 /// <param name="channels">Number of channels</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.</param>
 public Mat(int rows, int cols, CvEnum.DepthType type, int channels, IntPtr data, int step)
     : this(MatInvoke.cvMatCreateWithData(rows, cols, CvInvoke.MakeType(type, channels), data, new IntPtr(step)), true, true)
 {
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Copy the data in this cv::Mat to a CvArray
 /// </summary>
 /// <param name="m">The input array to copy to</param>
 /// <param name="mask">Operation mask. Its non-zero elements indicate which matrix elements need to be copied.</param>
 public void CopyTo(IOutputArray m, IInputArray mask = null)
 {
     using (OutputArray oaM = m.GetOutputArray())
         using (InputArray iaMask = mask == null ? InputArray.GetEmpty() : mask.GetInputArray())
             MatInvoke.cvMatCopyTo(Ptr, oaM, iaMask);
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Load the Mat from file
 /// </summary>
 /// <param name="fileName">The name of the file</param>
 /// <param name="loadType">File loading method</param>
 public Mat(String fileName, CvEnum.LoadImageType loadType)
     : this(MatInvoke.cvMatCreate(), true, false)
 {
     using (CvString s = new CvString(fileName))
         CvInvoke.cveImread(s, loadType, this);
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Converts an array to another data type with optional scaling.
 /// </summary>
 /// <param name="m">Output matrix; if it does not have a proper size or type before the operation, it is reallocated.</param>
 /// <param name="rtype">Desired output matrix type or, rather, the depth since the number of channels are the same as the input has; if rtype is negative, the output matrix will have the same type as the input.</param>
 /// <param name="alpha">Optional scale factor.</param>
 /// <param name="beta">Optional delta added to the scaled values.</param>
 public void ConvertTo(IOutputArray m, CvEnum.DepthType rtype, double alpha = 1.0, double beta = 0.0)
 {
     using (OutputArray oaM = m.GetOutputArray())
         MatInvoke.cvMatConvertTo(Ptr, oaM, rtype, alpha, beta);
 }