/// <summary> /// Convert the raw keypoints and descriptors to ImageFeature /// </summary> /// <param name="keyPointsVec">The raw keypoints vector</param> /// <param name="descriptors">The raw descriptor matrix</param> /// <returns>An array of image features</returns> public static ImageFeature <TDescriptor>[] ConvertFromRaw(VectorOfKeyPoint keyPointsVec, Matrix <TDescriptor> descriptors) { if (keyPointsVec.Size == 0) { return(new ImageFeature <TDescriptor> [0]); } Debug.Assert(keyPointsVec.Size == descriptors.Rows, "Size of keypoints vector do not match the rows of the descriptors matrix."); int sizeOfdescriptor = descriptors.Cols; MKeyPoint[] keyPoints = keyPointsVec.ToArray(); ImageFeature <TDescriptor>[] features = new ImageFeature <TDescriptor> [keyPoints.Length]; MCvMat header = descriptors.MCvMat; long address = header.data.ToInt64(); int rowSizeInByte = sizeOfdescriptor * Marshal.SizeOf(typeof(TDescriptor)); for (int i = 0; i < keyPoints.Length; i++, address += header.step) { features[i].KeyPoint = keyPoints[i]; TDescriptor[] desc = new TDescriptor[sizeOfdescriptor]; GCHandle handler = GCHandle.Alloc(desc, GCHandleType.Pinned); Toolbox.memcpy(handler.AddrOfPinnedObject(), new IntPtr(address), rowSizeInByte); handler.Free(); features[i].Descriptor = desc; } return(features); }
/// <summary> /// Convert the image features to keypoint vector and descriptor matrix /// </summary> public static void ConvertToRaw(ImageFeature <TDescriptor>[] features, out VectorOfKeyPoint keyPoints, out Matrix <TDescriptor> descriptors) { if (features.Length == 0) { keyPoints = null; descriptors = null; return; } keyPoints = new VectorOfKeyPoint(); keyPoints.Push( #if NETFX_CORE Extensions. #else Array. #endif ConvertAll <ImageFeature <TDescriptor>, MKeyPoint>(features, delegate(ImageFeature <TDescriptor> feature) { return(feature.KeyPoint); })); descriptors = new Matrix <TDescriptor>(features.Length, features[0].Descriptor.Length); int descriptorLength = features[0].Descriptor.Length; int rowSizeInByte = descriptorLength * Marshal.SizeOf(typeof(TDescriptor)); MCvMat header = descriptors.MCvMat; long address = header.data.ToInt64(); for (int i = 0; i < features.Length; i++, address += header.step) { GCHandle handler = GCHandle.Alloc(features[i].Descriptor, GCHandleType.Pinned); Toolbox.memcpy(new IntPtr(address), handler.AddrOfPinnedObject(), rowSizeInByte); handler.Free(); } }
private static Matrix <double> IntPtrToDoubleMatrix(IntPtr matPtr) { if (matPtr == IntPtr.Zero) { return(null); } MCvMat mat = (MCvMat)Marshal.PtrToStructure(matPtr, typeof(MCvMat)); Matrix <double> result = new Matrix <double>(mat.rows, mat.cols, 1, mat.data, mat.step); Debug.Assert(mat.type == result.MCvMat.type, "Matrix type is not double"); return(result); }
/// <summary> /// Convert arrays of points to matrix /// </summary> /// <param name="points">Arrays of points</param> /// <returns>A two dimension matrix that represent the points</returns> public static Matrix <double> GetMatrixFromPoints(MCvPoint2D64f[][] points) { int rows = points.Length; int cols = points[0].Length; Matrix <double> res = new Matrix <double>(rows, cols, 2); MCvMat cvMat = res.MCvMat; for (int i = 0; i < rows; i++) { IntPtr dst = new IntPtr(cvMat.Data.ToInt64() + cvMat.Step * i); CopyVector(points[i], dst); } return(res); }
/* * /// <summary> * /// Returns information about one of or all of the registered modules * /// </summary> * /// <param name="pluginName">The list of names and versions of the optimized plugins that CXCORE was able to find and load</param> * /// <param name="versionName">Information about the module(s), including version</param> * public static void GetModuleInfo(out String pluginName, out String versionName) * { * IntPtr version = IntPtr.Zero; * IntPtr pluginInfo = IntPtr.Zero; * CvInvoke.cvGetModuleInfo(IntPtr.Zero, ref version, ref pluginInfo); * * pluginName = Marshal.PtrToStringAnsi(pluginInfo); * versionName = Marshal.PtrToStringAnsi(version); * } * * /// <summary> * /// Enable or diable IPL optimization for opencv * /// </summary> * /// <param name="enable">true to enable optimization, false to disable</param> * public static void OptimizeCV(bool enable) * { * CvInvoke.cvUseOptimized(enable); * } * * * /// <summary> * /// Get the OpenCV matrix depth enumeration from depth type * /// </summary> * /// <param name="typeOfDepth">The depth of type</param> * /// <returns>OpenCV Matrix depth</returns> * public static CvEnum.DepthType GetMatrixDepth(Type typeOfDepth) * { * if (typeOfDepth == typeof(Single)) * return CvEnum.DepthType.Cv32F; * if (typeOfDepth == typeof(Int32)) * return Emgu.CV.CvEnum.DepthType.Cv32S; * if (typeOfDepth == typeof(SByte)) * return Emgu.CV.CvEnum.DepthType.Cv8S; * if (typeOfDepth == typeof(Byte)) * return CvEnum.DepthType.Cv8U; * if (typeOfDepth == typeof(Double)) * return CvEnum.DepthType.Cv64F; * if (typeOfDepth == typeof(UInt16)) * return CvEnum.DepthType.Cv16U; * if (typeOfDepth == typeof(Int16)) * return CvEnum.DepthType.Cv16S; * throw new NotImplementedException("Unsupported matrix depth"); * }*/ /// <summary> /// Convert arrays of data to matrix /// </summary> /// <param name="data">Arrays of data</param> /// <returns>A two dimension matrix that represent the array</returns> public static Matrix <T> GetMatrixFromArrays <T>(T[][] data) where T : struct { int rows = data.Length; int cols = data[0].Length; Matrix <T> res = new Matrix <T>(rows, cols); MCvMat mat = res.MCvMat; long dataPos = mat.Data.ToInt64(); //int rowSizeInBytes = Marshal.SizeOf(typeof(T)) * cols; for (int i = 0; i < rows; i++, dataPos += mat.Step) { CopyVector(data[i], new IntPtr(dataPos)); } return(res); }
/// <summary> /// Convert an array of descriptors to row by row matrix /// </summary> /// <param name="descriptors">An array of descriptors</param> /// <returns>A matrix where each row is a descriptor</returns> public static Matrix <float> GetMatrixFromDescriptors(float[][] descriptors) { int rows = descriptors.Length; int cols = descriptors[0].Length; Matrix <float> res = new Matrix <float>(rows, cols); MCvMat mat = res.MCvMat; long dataPos = mat.data.ToInt64(); for (int i = 0; i < rows; i++) { Marshal.Copy(descriptors[i], 0, new IntPtr(dataPos), cols); dataPos += mat.step; } return(res); }
/// <summary> /// Convert arrays of points to matrix /// </summary> /// <param name="points">Arrays of points</param> /// <returns>A two dimension matrix that represent the points</returns> public static Matrix <double> GetMatrixFromPoints(MCvPoint2D64f[][] points) { int rows = points.Length; int cols = points[0].Length; Matrix <double> res = new Matrix <double>(rows, cols, 2); MCvMat cvMat = res.MCvMat; for (int i = 0; i < rows; i++) { GCHandle handleTmp = GCHandle.Alloc(points[i], GCHandleType.Pinned); IntPtr dst = new IntPtr(cvMat.Data.ToInt64() + cvMat.Step * i); Emgu.Util.Toolbox.memcpy(dst, handleTmp.AddrOfPinnedObject(), points[i].Length * Marshal.SizeOf(typeof(MCvPoint2D64f))); handleTmp.Free(); } return(res); }
/* * /// <summary> * /// Get the OpenCV matrix depth enumeration from depth type * /// </summary> * /// <param name="typeOfDepth">The depth of type</param> * /// <returns>OpenCV Matrix depth</returns> * public static CvEnum.DepthType GetMatrixDepth(Type typeOfDepth) * { * if (typeOfDepth == typeof(Single)) * return CvEnum.DepthType.Cv32F; * if (typeOfDepth == typeof(Int32)) * return Emgu.CV.CvEnum.DepthType.Cv32S; * if (typeOfDepth == typeof(SByte)) * return Emgu.CV.CvEnum.DepthType.Cv8S; * if (typeOfDepth == typeof(Byte)) * return CvEnum.DepthType.Cv8U; * if (typeOfDepth == typeof(Double)) * return CvEnum.DepthType.Cv64F; * if (typeOfDepth == typeof(UInt16)) * return CvEnum.DepthType.Cv16U; * if (typeOfDepth == typeof(Int16)) * return CvEnum.DepthType.Cv16S; * throw new NotImplementedException("Unsupported matrix depth"); * }*/ /// <summary> /// Convert arrays of data to matrix /// </summary> /// <param name="data">Arrays of data</param> /// <returns>A two dimension matrix that represent the array</returns> public static Matrix <T> GetMatrixFromArrays <T>(T[][] data) where T : struct { int rows = data.Length; int cols = data[0].Length; Matrix <T> res = new Matrix <T>(rows, cols); MCvMat mat = res.MCvMat; long dataPos = mat.Data.ToInt64(); int rowSizeInBytes = Marshal.SizeOf(typeof(T)) * cols; for (int i = 0; i < rows; i++, dataPos += mat.Step) { GCHandle handle = GCHandle.Alloc(data[i], GCHandleType.Pinned); Emgu.Util.Toolbox.memcpy(new IntPtr(dataPos), handle.AddrOfPinnedObject(), rowSizeInBytes); handle.Free(); } return(res); }
static void Main(string[] args) { /* * Goal * The sample application will: * * Determinate the distortion matrix * Determinate the camera matrix * Input from Camera, Video and Image file list * Configuration from XML/YAML file * Save the results into XML/YAML file * Calculate re-projection error */ Capture capture; int board_w = 9; int board_h = 6; int n_boards = 8; //numbers of boards int board_n = board_h * board_w; //matrix MCvMat image_points = new MCvMat(); try { capture = new Capture(); while (true) { Image <Bgr, Byte> ImageFrame = capture.QueryFrame(); CvInvoke.cvShowImage("gray scale input image", ImageFrame.Ptr); CvInvoke.cvWaitKey(1); } //CvInvoke.cvWaitKey(0); } catch (NullReferenceException excpt) { Console.WriteLine(excpt.Message); } }
/// <summary> /// Convert the raw keypoints and descriptors to ImageFeature /// </summary> /// <param name="keyPointsVec">The raw keypoints vector</param> /// <param name="descriptors">The raw descriptor matrix</param> /// <returns>An array of image features</returns> public static ImageFeature[] ConvertToImageFeature(VectorOfKeyPoint keyPointsVec, Matrix <float> descriptors) { if (keyPointsVec.Size == 0) { return(new ImageFeature[0]); } Debug.Assert(keyPointsVec.Size == descriptors.Rows, "Size of keypoints vector do not match the rows of the descriptors matrix."); int sizeOfdescriptor = descriptors.Cols; MKeyPoint[] keyPoints = keyPointsVec.ToArray(); ImageFeature[] features = new ImageFeature[keyPoints.Length]; MCvMat header = descriptors.MCvMat; long address = header.data.ToInt64(); for (int i = 0; i < keyPoints.Length; i++, address += header.step) { features[i].KeyPoint = keyPoints[i]; float[] desc = new float[sizeOfdescriptor]; Marshal.Copy(new IntPtr(address), desc, 0, sizeOfdescriptor); features[i].Descriptor = desc; } return(features); }
/// <summary> /// Convert the raw keypoints and descriptors to array of managed structure. /// </summary> /// <param name="modelKeyPointVec">The model keypoint vector</param> /// <param name="modelDescriptorMat">The mode descriptor vector</param> /// <param name="observedKeyPointVec">The observerd keypoint vector</param> /// <param name="observedDescriptorMat">The observed descriptor vector</param> /// <param name="indices">The indices matrix</param> /// <param name="dists">The distances matrix</param> /// <param name="mask">The mask</param> /// <returns>The managed MatchedImageFeature array</returns> public static MatchedImageFeature[] ConvertToMatchedImageFeature( VectorOfKeyPoint modelKeyPointVec, Matrix <TDescriptor> modelDescriptorMat, VectorOfKeyPoint observedKeyPointVec, Matrix <TDescriptor> observedDescriptorMat, Matrix <int> indices, Matrix <float> dists, Matrix <Byte> mask) { MKeyPoint[] modelKeyPoints = modelKeyPointVec.ToArray(); MKeyPoint[] observedKeyPoints = observedKeyPointVec.ToArray(); int resultLength = (mask == null) ? observedKeyPoints.Length : CvInvoke.cvCountNonZero(mask); MatchedImageFeature[] result = new MatchedImageFeature[resultLength]; MCvMat modelMat = (MCvMat)Marshal.PtrToStructure(modelDescriptorMat.Ptr, typeof(MCvMat)); long modelPtr = modelMat.data.ToInt64(); int modelStep = modelMat.step; MCvMat observedMat = (MCvMat)Marshal.PtrToStructure(observedDescriptorMat.Ptr, typeof(MCvMat)); long observedPtr = observedMat.data.ToInt64(); int observedStep = observedMat.step; int descriptorLength = modelMat.cols; int descriptorSizeInByte = descriptorLength * Marshal.SizeOf(typeof(TDescriptor)); int k = dists.Cols; TDescriptor[] tmp = new TDescriptor[descriptorLength]; GCHandle handle = GCHandle.Alloc(tmp, GCHandleType.Pinned); IntPtr address = handle.AddrOfPinnedObject(); int resultIdx = 0; for (int i = 0; i < observedKeyPoints.Length; i++) { if (mask != null && mask.Data[i, 0] == 0) { continue; } SimilarFeature[] features = new SimilarFeature[k]; for (int j = 0; j < k; j++) { features[j].Distance = dists.Data[i, j]; ImageFeature <TDescriptor> imgFeature = new ImageFeature <TDescriptor>(); int idx = indices.Data[i, j]; if (idx == -1) { Array.Resize(ref features, j); break; } imgFeature.KeyPoint = modelKeyPoints[idx]; imgFeature.Descriptor = new TDescriptor[descriptorLength]; Emgu.Util.Toolbox.memcpy(address, new IntPtr(modelPtr + modelStep * idx), descriptorSizeInByte); tmp.CopyTo(imgFeature.Descriptor, 0); features[j].Feature = imgFeature; } result[resultIdx].SimilarFeatures = features; ImageFeature <TDescriptor> observedFeature = new ImageFeature <TDescriptor>(); observedFeature.KeyPoint = observedKeyPoints[i]; observedFeature.Descriptor = new TDescriptor[descriptorLength]; Emgu.Util.Toolbox.memcpy(address, new IntPtr(observedPtr + observedStep * i), descriptorSizeInByte); tmp.CopyTo(observedFeature.Descriptor, 0); result[resultIdx].ObservedFeature = observedFeature; resultIdx++; } handle.Free(); return(result); }