private static Matrix<float> ToMatrix<T>(T[][] data) where T: struct { int elementCount = 0; #if NETFX_CORE int structureSize = Marshal.SizeOf<T>(); int floatValueInStructure = structureSize / Marshal.SizeOf<float>(); #else int structureSize = Marshal.SizeOf(typeof(T)); int floatValueInStructure = structureSize / Marshal.SizeOf(typeof(float)); #endif foreach (T[] d in data) elementCount += d.Length; Matrix<float> res = new Matrix<float>(elementCount, floatValueInStructure); Int64 address = res.MCvMat.Data.ToInt64(); foreach (T[] d in data) { int lengthInBytes = d.Length * structureSize; GCHandle handle = GCHandle.Alloc(d, GCHandleType.Pinned); CvToolbox.Memcpy(new IntPtr(address), handle.AddrOfPinnedObject(), lengthInBytes); handle.Free(); address += lengthInBytes; } return res; }
private static Matrix <float> ToMatrix <T>(T[][] data) where T : struct { int elementCount = 0; int structureSize = Emgu.Util.Toolbox.SizeOf <T>(); int floatValueInStructure = structureSize / Emgu.Util.Toolbox.SizeOf <float>(); foreach (T[] d in data) { elementCount += d.Length; } Matrix <float> res = new Matrix <float>(elementCount, floatValueInStructure); Int64 address = res.MCvMat.Data.ToInt64(); foreach (T[] d in data) { int lengthInBytes = d.Length * structureSize; GCHandle handle = GCHandle.Alloc(d, GCHandleType.Pinned); CvToolbox.Memcpy(new IntPtr(address), handle.AddrOfPinnedObject(), lengthInBytes); handle.Free(); address += lengthInBytes; } return(res); }
/// <summary> /// return an enumerator of the elements in the sequence /// </summary> /// <returns>an enumerator of the elements in the sequence</returns> public IEnumerator <T> GetEnumerator() { using (PinnedArray <T> buffer = new PinnedArray <T>(1)) { IntPtr address = buffer.AddrOfPinnedObject(); for (int i = 0; i < Total; i++) { CvToolbox.Memcpy(address, CvInvoke.cvGetSeqElem(_ptr, i), _sizeOfElement); yield return(buffer.Array[0]); //yield return (T)Marshal.PtrToStructure(CvInvoke.cvGetSeqElem(_ptr, i), typeof(T)); //yield return this[i]; } } }