ToArray() public method

Convert the standard vector to an array of Byte
public ToArray ( ) : Byte[]
return Byte[]
Esempio n. 1
0
      public void TestMatToFileStorage()
      {
         //create a matrix m with random values
         Mat m = new Mat(120, 240, DepthType.Cv8U, 1);
         using (ScalarArray low = new ScalarArray(0))
         using (ScalarArray high = new ScalarArray(255))
         CvInvoke.Randu(m, low, high);

         //Convert the random matrix m to yml format, good for matrix that contains values such as calibration, homography etc.
         String mStr;
         using (FileStorage fs = new FileStorage(".yml", FileStorage.Mode.Write | FileStorage.Mode.Memory))
         {
            fs.Write(m, "m");
            mStr = fs.ReleaseAndGetString();
         }

         //Treat the Mat as image data and convert it to png format.
         using (VectorOfByte bytes = new VectorOfByte())
         {
            CvInvoke.Imencode(".png", m, bytes);

            byte[] rawData =  bytes.ToArray();
         }


      }
Esempio n. 2
0
        /// <summary>
        /// Computes an optimal affine transformation between two 3D point sets.
        /// </summary>
        /// <param name="src">First input 3D point set.</param>
        /// <param name="dst">Second input 3D point set.</param>
        /// <param name="estimate">Output 3D affine transformation matrix.</param>
        /// <param name="inliers">Output vector indicating which points are inliers.</param>
        /// <param name="ransacThreshold">Maximum reprojection error in the RANSAC algorithm to consider a point as an inlier.</param>
        /// <param name="confidence">Confidence level, between 0 and 1, for the estimated transformation. Anything between 0.95 and 0.99 is usually good enough. Values too close to 1 can slow down the estimation significantly. Values lower than 0.8-0.9 can result in an incorrectly estimated transformation.</param>
        /// <returns>The result</returns>
        public static int EstimateAffine3D(MCvPoint3D32f[] src, MCvPoint3D32f[] dst, out Matrix <double> estimate,
                                           out Byte[] inliers, double ransacThreshold, double confidence)
        {
            GCHandle srcHandle = GCHandle.Alloc(src, GCHandleType.Pinned);

            GCHandle dstHandle = GCHandle.Alloc(dst, GCHandleType.Pinned);
            int      result;

            estimate = new Matrix <double>(3, 4);

            int sizeOfPoint3D32f = Toolbox.SizeOf <MCvPoint3D64f>();

            using (
                Matrix <float> srcMat = new Matrix <float>(1, src.Length, 3, srcHandle.AddrOfPinnedObject(),
                                                           sizeOfPoint3D32f * src.Length))
                using (
                    Matrix <float> dstMat = new Matrix <float>(1, dst.Length, 3, dstHandle.AddrOfPinnedObject(),
                                                               sizeOfPoint3D32f * dst.Length))
                    using (Util.VectorOfByte vectorOfByte = new Util.VectorOfByte())
                    {
                        result  = EstimateAffine3D(srcMat, dstMat, estimate, vectorOfByte, ransacThreshold, confidence);
                        inliers = vectorOfByte.ToArray();
                    }

            srcHandle.Free();
            dstHandle.Free();

            return(result);
        }
Esempio n. 3
0
      /// <summary>
      /// Saves the image to the specified file. The image format is chosen depending on the filename extension, see cvLoadImage. Only 8-bit single-channel or 3-channel (with 'BGR' channel order) images can be saved using this function. If the format, depth or channel order is different, use cvCvtScale and cvCvtColor to convert it before saving, or use universal cvSave to save the image to XML or YAML format
      /// </summary>
      /// <param name="filename">The name of the file to be saved to</param>
      /// <param name="image">The image to be saved</param>
      /// <param name="parameters">The parameters</param>
      /// <returns>true if success</returns>
      public static bool Imwrite(String filename, IInputArray image, params int[] parameters)
      {
         using (Util.VectorOfInt vec = new Util.VectorOfInt())
         {
            if (parameters.Length > 0)
               vec.Push(parameters);
            using (CvString s = new CvString(filename))
            using (InputArray iaImage = image.GetInputArray())
            {
#if !(__IOS__ || __ANDROID__ || NETFX_CORE)
               bool containsUnicode = (s.Length != filename.Length);
               if (containsUnicode &&
                   (Emgu.Util.Platform.OperationSystem != OS.MacOSX) &&
                   (Emgu.Util.Platform.OperationSystem != OS.Linux))
               {
                  //Handle unicode in Windows platform
                  //Work around for Open CV ticket:
                  //https://github.com/Itseez/opencv/issues/4292
                  //https://github.com/Itseez/opencv/issues/4866     
                  System.IO.FileInfo fi = new System.IO.FileInfo(filename);

                  using (VectorOfByte vb = new VectorOfByte())
                  {
                     CvInvoke.Imencode(fi.Extension, image, vb, parameters);
                     byte[] arr = vb.ToArray();
                     System.IO.File.WriteAllBytes(filename, arr);
                     return true;
                  }
               }
               else
#endif
                  return cveImwrite(s, iaImage, vec);
            }
         }
      }
Esempio n. 4
0
        /// <summary>
        /// Convert the standard vector to arrays of int
        /// </summary>
        /// <returns>Arrays of int</returns>
        public Byte[][] ToArrayOfArray()
        {
            int size = Size;

            Byte[][] res = new Byte[size][];
            for (int i = 0; i < size; i++)
            {
                using (VectorOfByte v = this[i])
                {
                    res[i] = v.ToArray();
                }
            }
            return(res);
        }
Esempio n. 5
0
        /// <summary>
        /// Computes an optimal affine transformation between two 3D point sets.
        /// </summary>
        /// <param name="src">First input 3D point set.</param>
        /// <param name="dst">Second input 3D point set.</param>
        /// <param name="estimate">Output 3D affine transformation matrix.</param>
        /// <param name="inliers">Output vector indicating which points are inliers.</param>
        /// <param name="ransacThreshold">Maximum reprojection error in the RANSAC algorithm to consider a point as an inlier.</param>
        /// <param name="confidence">Confidence level, between 0 and 1, for the estimated transformation. Anything between 0.95 and 0.99 is usually good enough. Values too close to 1 can slow down the estimation significantly. Values lower than 0.8-0.9 can result in an incorrectly estimated transformation.</param>
        /// <returns></returns>
        public static int CvEstimateAffine3D(MCvPoint3D32f[] src, MCvPoint3D32f[] dst, out Matrix <double> estimate, out Byte[] inliers, double ransacThreshold, double confidence)
        {
            GCHandle srcHandle = GCHandle.Alloc(src, GCHandleType.Pinned);

            GCHandle dstHandle = GCHandle.Alloc(dst, GCHandleType.Pinned);
            int      result;

            estimate = new Matrix <double>(3, 4);
            using (Util.Mat affineEstimate = new Util.Mat())
                using (Matrix <float> srcMat = new Matrix <float>(1, src.Length, 3, srcHandle.AddrOfPinnedObject(), Marshal.SizeOf(typeof(MCvPoint3D32f)) * src.Length))
                    using (Matrix <float> dstMat = new Matrix <float>(1, dst.Length, 3, dstHandle.AddrOfPinnedObject(), Marshal.SizeOf(typeof(MCvPoint3D32f)) * dst.Length))
                        using (Util.VectorOfByte vectorOfByte = new Util.VectorOfByte())
                        {
                            result  = _CvEstimateAffine3D(srcMat, dstMat, affineEstimate, vectorOfByte, ransacThreshold, confidence);
                            inliers = vectorOfByte.ToArray();
                            CvInvoke.cvMatCopyToCvArr(affineEstimate, estimate);
                        }

            srcHandle.Free();
            dstHandle.Free();

            return(result);
        }
Esempio n. 6
0
        /// <summary>
        /// Calculates optical flow for a sparse feature set using iterative Lucas-Kanade method in pyramids
        /// </summary>
        /// <param name="prev">First frame, at time t</param>
        /// <param name="curr">Second frame, at time t + dt </param>
        /// <param name="prevFeatures">Array of points for which the flow needs to be found</param>
        /// <param name="winSize">Size of the search window of each pyramid level</param>
        /// <param name="level">Maximal pyramid level number. If 0 , pyramids are not used (single level), if 1 , two levels are used, etc</param>
        /// <param name="criteria">Specifies when the iteration process of finding the flow for each point on each pyramid level should be stopped</param>
        /// <param name="flags">Flags</param>
        /// <param name="currFeatures">Array of 2D points containing calculated new positions of input features in the second image</param>
        /// <param name="status">Array. Every element of the array is set to 1 if the flow for the corresponding feature has been found, 0 otherwise</param>
        /// <param name="trackError">Array of double numbers containing difference between patches around the original and moved points</param>
        /// <param name="minEigThreshold">the algorithm calculates the minimum eigen value of a 2x2 normal matrix of optical flow equations (this matrix is called a spatial gradient matrix in [Bouguet00]), divided by number of pixels in a window; if this value is less than minEigThreshold, then a corresponding feature is filtered out and its flow is not processed, so it allows to remove bad points and get a performance boost.</param>
        public static void CalcOpticalFlowPyrLK(
            IInputArray prev,
            IInputArray curr,
            PointF[] prevFeatures,
            Size winSize,
            int level,
            MCvTermCriteria criteria,
            out PointF[] currFeatures,
            out Byte[] status,
            out float[] trackError,
            Emgu.CV.CvEnum.LKFlowFlag flags = CvEnum.LKFlowFlag.Default,
            double minEigThreshold          = 1.0e-4)
        {
            using (Util.VectorOfPointF prevPts = new Util.VectorOfPointF())
                using (Util.VectorOfPointF nextPts = new Util.VectorOfPointF())
                    using (Util.VectorOfByte statusVec = new Util.VectorOfByte())
                        using (Util.VectorOfFloat errorVec = new Util.VectorOfFloat())
                        {
                            prevPts.Push(prevFeatures);

                            CalcOpticalFlowPyrLK(
                                prev,
                                curr,
                                prevPts,
                                nextPts,
                                statusVec,
                                errorVec,
                                winSize,
                                level,
                                criteria,
                                flags,
                                minEigThreshold);
                            status       = statusVec.ToArray();
                            trackError   = errorVec.ToArray();
                            currFeatures = nextPts.ToArray();
                        }
        }
Esempio n. 7
0
      /// <summary>
      /// Detect all the characters in the image.
      /// </summary>
      /// <returns>All the characters in the image</returns>
      public Character[] GetCharacters()
      {
         using (VectorOfByte textSeq = new VectorOfByte())
         using (VectorOfTesseractResult results = new VectorOfTesseractResult())
         {
            //Seq<byte> textSeq = new Seq<byte>(stor);
            //Seq<TesseractResult> results = new Seq<TesseractResult>(stor);
            OcrInvoke.TessBaseAPIExtractResult(_ptr, textSeq, results);

            byte[] bytes = textSeq.ToArray();
            TesseractResult[] trs = results.ToArray();

            Character[] res = new Character[trs.Length];
            int idx = 0;
            for (int i = 0; i < trs.Length; i++)
            {
               TesseractResult tr = trs[i];
               res[i].Text = _utf8.GetString(bytes, idx, tr.Length).Replace("\n", Environment.NewLine);

               idx += tr.Length;
               res[i].Cost = tr.Cost;
               if (tr.Cost == 0)
                  res[i].Region = Rectangle.Empty;
               else
                  res[i].Region = tr.Region;
            }
            return res;
         }
      }
Esempio n. 8
0
 /// <summary>
 /// Get all the text in the image
 /// </summary>
 /// <returns>All the text in the image</returns>
 public string GetText()
 {
    using (Util.VectorOfByte bytes = new Util.VectorOfByte())
    {
       OcrInvoke.TessBaseAPIGetUTF8Text(_ptr, bytes);
       return _utf8.GetString(bytes.ToArray()).Replace("\n", Environment.NewLine);
    }
 }
Esempio n. 9
0
      /*
      #region Kalman Filter
      /// <summary>
      /// Allocates CvKalman and all its matrices and initializes them somehow. 
      /// </summary>
      /// <param name="dynamParams">dimensionality of the state vector</param>
      /// <param name="measureParams">dimensionality of the measurement vector </param>
      /// <param name="controlParams">dimensionality of the control vector </param>
      /// <returns>Pointer to the created Kalman filter</returns>
      [DllImport(OpencvVideoLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
      public static extern IntPtr cvCreateKalman(int dynamParams, int measureParams, int controlParams);

      /// <summary>
      /// Adjusts stochastic model state on the basis of the given measurement of the model state.
      /// The function stores adjusted state at kalman->state_post and returns it on output
      /// </summary>
      /// <param name="kalman">Pointer to the structure to be updated</param>
      /// <param name="measurement">Pointer to the structure CvMat containing the measurement vector</param>
      /// <returns>The function stores adjusted state at kalman->state_post and returns it on output</returns>
      [DllImport(OpencvVideoLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
      public static extern IntPtr cvKalmanCorrect(ref MCvKalman kalman, IntPtr measurement);


      /// <summary>
      /// Estimates the subsequent stochastic model state by its current state and stores it at kalman->state_pre
      /// The function returns the estimated state
      /// </summary>
      /// <param name="kalman">Kalman filter state</param>
      /// <param name="control">Control vector (uk), should be NULL iff there is no external control (controlParams=0). </param>
      /// <returns>the estimated state</returns>
      [DllImport(OpencvVideoLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
      public static extern IntPtr cvKalmanPredict(ref MCvKalman kalman, IntPtr control);

      /// <summary>
      /// Releases the structure CvKalman and all underlying matrices
      /// </summary>
      /// <param name="kalman">reference of the pointer to the Kalman filter structure.</param>
      [DllImport(OpencvVideoLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
      public static extern void cvReleaseKalman(ref IntPtr kalman);
      #endregion
*/
      #region optical flow
      /// <summary>
      /// Calculates optical flow for a sparse feature set using iterative Lucas-Kanade method in pyramids
      /// </summary>
      /// <param name="prev">First frame, at time t</param>
      /// <param name="curr">Second frame, at time t + dt </param>
      /// <param name="prevFeatures">Array of points for which the flow needs to be found</param>
      /// <param name="winSize">Size of the search window of each pyramid level</param>
      /// <param name="level">Maximal pyramid level number. If 0 , pyramids are not used (single level), if 1 , two levels are used, etc</param>
      /// <param name="criteria">Specifies when the iteration process of finding the flow for each point on each pyramid level should be stopped</param>
      /// <param name="flags">Flags</param>
      /// <param name="currFeatures">Array of 2D points containing calculated new positions of input features in the second image</param>
      /// <param name="status">Array. Every element of the array is set to 1 if the flow for the corresponding feature has been found, 0 otherwise</param>
      /// <param name="trackError">Array of double numbers containing difference between patches around the original and moved points</param>
      /// <param name="minEigThreshold">the algorithm calculates the minimum eigen value of a 2x2 normal matrix of optical flow equations (this matrix is called a spatial gradient matrix in [Bouguet00]), divided by number of pixels in a window; if this value is less than minEigThreshold, then a corresponding feature is filtered out and its flow is not processed, so it allows to remove bad points and get a performance boost.</param>
      public static void CalcOpticalFlowPyrLK(
         IInputArray prev,
         IInputArray curr,
         PointF[] prevFeatures,
         Size winSize,
         int level,
         MCvTermCriteria criteria,
         out PointF[] currFeatures,
         out Byte[] status,
         out float[] trackError,
         Emgu.CV.CvEnum.LKFlowFlag flags = CvEnum.LKFlowFlag.Default,
         double minEigThreshold = 1.0e-4)
      {
         using (Util.VectorOfPointF prevPts = new Util.VectorOfPointF())
         using (Util.VectorOfPointF nextPts = new Util.VectorOfPointF())
         using (Util.VectorOfByte statusVec = new Util.VectorOfByte())
         using (Util.VectorOfFloat errorVec = new Util.VectorOfFloat())
         {
            prevPts.Push(prevFeatures);

            CalcOpticalFlowPyrLK(
               prev,
               curr,
               prevPts,
               nextPts,
               statusVec,
               errorVec,
               winSize,
               level,
               criteria,
               flags,
               minEigThreshold);
            status = statusVec.ToArray();
            trackError = errorVec.ToArray();
            currFeatures = nextPts.ToArray();
         }
      }
Esempio n. 10
0
      public void SetImage(Emgu.CV.Mat image)
	   {
	      if (image == null)
	      {
	         this.DisplayImage.Source = null;
	         return;
	      }
	      using (VectorOfByte vb = new VectorOfByte())
	      {
	         CvInvoke.Imencode(".jpg", image, vb);
	         byte[] rawData = vb.ToArray();
	            this.DisplayImage.Source = ImageSource.FromStream(() => new MemoryStream(rawData));
	      }
	   }
Esempio n. 11
0
      /// <summary>
      /// Computes an optimal affine transformation between two 3D point sets.
      /// </summary>
      /// <param name="src">First input 3D point set.</param>
      /// <param name="dst">Second input 3D point set.</param>
      /// <param name="estimate">Output 3D affine transformation matrix.</param>
      /// <param name="inliers">Output vector indicating which points are inliers.</param>
      /// <param name="ransacThreshold">Maximum reprojection error in the RANSAC algorithm to consider a point as an inlier.</param>
      /// <param name="confidence">Confidence level, between 0 and 1, for the estimated transformation. Anything between 0.95 and 0.99 is usually good enough. Values too close to 1 can slow down the estimation significantly. Values lower than 0.8-0.9 can result in an incorrectly estimated transformation.</param>
      /// <returns></returns>
      public static int CvEstimateAffine3D(MCvPoint3D32f[] src, MCvPoint3D32f[] dst, out Matrix<double> estimate, out Byte[] inliers, double ransacThreshold, double confidence)
      {
         GCHandle srcHandle = GCHandle.Alloc(src, GCHandleType.Pinned);
         GCHandle dstHandle = GCHandle.Alloc(dst, GCHandleType.Pinned);
         int result;

         estimate = new Matrix<double>(3, 4);
         using (Util.Mat affineEstimate = new Util.Mat())
         using (Matrix<float> srcMat = new Matrix<float>(1,  src.Length, 3, srcHandle.AddrOfPinnedObject(), Marshal.SizeOf(typeof(MCvPoint3D32f)) * src.Length))
         using (Matrix<float> dstMat = new Matrix<float>(1,  dst.Length, 3, dstHandle.AddrOfPinnedObject(), Marshal.SizeOf(typeof(MCvPoint3D32f)) * dst.Length ))
         using (Util.VectorOfByte vectorOfByte = new Util.VectorOfByte())
         {
            result = _CvEstimateAffine3D(srcMat, dstMat, affineEstimate, vectorOfByte, ransacThreshold, confidence);
            inliers = vectorOfByte.ToArray();
            CvInvoke.cvMatCopyToCvArr(affineEstimate, estimate);  
         }

         srcHandle.Free();
         dstHandle.Free();

         return result;
      }
Esempio n. 12
0
      /// <summary>
      /// Computes an optimal affine transformation between two 3D point sets.
      /// </summary>
      /// <param name="src">First input 3D point set.</param>
      /// <param name="dst">Second input 3D point set.</param>
      /// <param name="estimate">Output 3D affine transformation matrix.</param>
      /// <param name="inliers">Output vector indicating which points are inliers.</param>
      /// <param name="ransacThreshold">Maximum reprojection error in the RANSAC algorithm to consider a point as an inlier.</param>
      /// <param name="confidence">Confidence level, between 0 and 1, for the estimated transformation. Anything between 0.95 and 0.99 is usually good enough. Values too close to 1 can slow down the estimation significantly. Values lower than 0.8-0.9 can result in an incorrectly estimated transformation.</param>
      /// <returns></returns>
      public static int EstimateAffine3D(MCvPoint3D32f[] src, MCvPoint3D32f[] dst, out Matrix<double> estimate,
         out Byte[] inliers, double ransacThreshold, double confidence)
      {
         GCHandle srcHandle = GCHandle.Alloc(src, GCHandleType.Pinned);
         GCHandle dstHandle = GCHandle.Alloc(dst, GCHandleType.Pinned);
         int result;

         estimate = new Matrix<double>(3, 4);
#if NETFX_CORE
         int sizeOfPoint3D32f = Marshal.SizeOf<MCvPoint3D32f>();
#else
         int sizeOfPoint3D32f = Marshal.SizeOf(typeof (MCvPoint3D32f));
#endif
         using (
            Matrix<float> srcMat = new Matrix<float>(1, src.Length, 3, srcHandle.AddrOfPinnedObject(),
               sizeOfPoint3D32f * src.Length))
         using (
            Matrix<float> dstMat = new Matrix<float>(1, dst.Length, 3, dstHandle.AddrOfPinnedObject(),
               sizeOfPoint3D32f * dst.Length))
         using (Util.VectorOfByte vectorOfByte = new Util.VectorOfByte())
         {
            result = EstimateAffine3D(srcMat, dstMat, estimate, vectorOfByte, ransacThreshold, confidence);
            inliers = vectorOfByte.ToArray();
         }

         srcHandle.Free();
         dstHandle.Free();

         return result;
      }
Esempio n. 13
0
      private String UtfByteVectorToString(VectorOfByte bytes)
      {
#if NETFX_CORE
         byte[] bArr = bytes.ToArray();
         return _utf8.GetString(bArr, 0, bArr.Length).Replace("\n", Environment.NewLine);
#else
         return _utf8.GetString(bytes.ToArray()).Replace("\n", Environment.NewLine);
#endif
      }