Example #1
0
        /*
         * internal static MatWithHandle PrepareArrayForCopy(DepthType dt, Size s, int channels, Array data)
         * {
         * int dimension = data.Rank;
         * int length0 = data.GetLength(0);
         * if (dimension == 1)
         * {
         *    if (data is byte[])
         *    {
         *       int elementSize =
         *       (dt == DepthType.Cv64F)
         *          ? 8
         *          : (dt == DepthType.Cv32S || dt == DepthType.Cv32F)
         *             ? 4
         *             : (dt == DepthType.Cv16S || dt == DepthType.Cv16U)
         *                ? 2
         *                : 1;
         *       if (length0 != s.Width * s.Height * channels * elementSize)
         *          throw new Exception(String.Format(
         *             "The total length of data ({0} bytes) does not match that of the Mat ({1} bytes)",
         *             length0, s.Width * s.Height * channels * elementSize));
         *    }
         *    else if (length0 != s.Width * s.Height * channels)
         *       throw new Exception(String.Format(
         *          "The total length of data ({0}) does not match that of the Mat ({1})",
         *          length0, s.Width * s.Height * channels));
         * }
         * else //dimension > 1
         * {
         *    if (length0 != s.Height)
         *       throw new Exception(String.Format(
         *          "The number of rows in data ({0}) does not match that of the Mat ({1})",
         *          length0, s.Height));
         *
         *    if (data.GetLength(1) != s.Width)
         *       throw new Exception(String.Format(
         *          "The number of cols in data ({0}) does not match that of the Mat ({1})",
         *          data.GetLength(1), s.Width));
         *
         *    if (dimension == 2 && (channels != 1))
         *    {
         *       throw new Exception(String.Format(
         *          "A 3 dimension data array is required for Mat that contains {0} channels", channels));
         *    }
         *
         *    if (dimension == 3)
         *    {
         *       if (data.GetLength(2) != channels)
         *          throw new Exception(
         *             String.Format(
         *                "The size of the 3rd dimension in data ({0}) does not match that of the number of channels of the Mat ({1})",
         *                data.GetLength(2), channels));
         *    }
         *    else if (dimension > 3)
         *    {
         *       throw new Exception(String.Format("The dimension of the data ({0}) is too high.", dimension));
         *    }
         * }
         *
         * int step;
         * if ((data is double[] || data is double[,] || data is double[, ,]) && dt == DepthType.Cv64F)
         * {
         *    step = sizeof(double) * s.Width * channels;
         * }
         * else if ((data is byte[] || data is byte[,] || data is byte[, ,]) && dt == DepthType.Cv8U)
         * {
         *    step = sizeof(byte) * s.Width * channels;
         * }
         * else if ((data is float[] || data is float[,] || data is float[, ,]) && dt == DepthType.Cv32F)
         * {
         *    step = sizeof(float) * s.Width * channels;
         * }
         * else if ((data is int[] || data is int[,] || data is int[, ,]) && dt == DepthType.Cv32S)
         * {
         *    step = sizeof(int) * s.Width * channels;
         * }
         * else if ((data is UInt16[] || data is UInt16[,] || data is UInt16[, ,]) && dt == DepthType.Cv16U)
         * {
         *    step = sizeof(UInt16) * s.Width * channels;
         * }
         * else if ((data is Int16[] || data is Int16[,] || data is Int16[, ,]) && dt == DepthType.Cv16S)
         * {
         *    step = sizeof(Int16) * s.Width * channels;
         * }
         * else
         * {
         *    throw new Exception(String.Format("The type of data doesn't match the type of the Mat ({0}).", dt));
         * }
         *
         * GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
         * return new MatWithHandle(handle, s, dt, channels, step);
         * }
         *
         * /// <summary>
         * /// Copies the values of the Mat to <paramref name="data"/>.
         * /// </summary>
         * /// <param name="data">The data storage, must match the size of the Mat</param>
         * public void CopyTo(Array data)
         * {
         * if (IsEmpty)
         * {
         *    throw new Exception("The Mat is empty");
         * }
         *
         * using (MatWithHandle m = PrepareArrayForCopy(Depth, Size, NumberOfChannels, data))
         *    CopyTo(m);
         * }*/

        /// <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.cveMatConvertTo(Ptr, oaM, rtype, alpha, beta);
        }