/// <summary> /// Create a matched feature structure. /// </summary> /// <param name="observedFeature">The feature from the observed image</param> /// <param name="modelFeatures">The matched feature from the model</param> /// <param name="dist">The distances between the feature from the observerd image and the matched feature from the model image</param> public MatchedSURFFeature(SURFFeature observedFeature, SURFFeature[] modelFeatures, double[] dist) { ObservedFeature = observedFeature; _similarFeatures = new SimilarFeature[modelFeatures.Length]; for (int i = 0; i < modelFeatures.Length; i++) { _similarFeatures[i] = new SimilarFeature(dist[i], modelFeatures[i]); } }
/// <summary> /// Create a matched feature structure. /// </summary> /// <param name="observedFeature">The feature from the observed image</param> /// <param name="modelFeatures">The matched feature from the model</param> /// <param name="dist">The distances between the feature from the observerd image and the matched feature from the model image</param> public MatchedImageFeature(ImageFeature <TDescriptor> observedFeature, ImageFeature <TDescriptor>[] modelFeatures, double[] dist) { ObservedFeature = observedFeature; _similarFeatures = new SimilarFeature[modelFeatures.Length]; for (int i = 0; i < modelFeatures.Length; i++) { _similarFeatures[i] = new SimilarFeature(dist[i], modelFeatures[i]); } }
/// <summary> /// Create a matched feature structure. /// </summary> /// <param name="observedFeature">The feature from the observed image</param> /// <param name="modelFeatures">The matched feature from the model</param> /// <param name="dist">The distances between the feature from the observerd image and the matched feature from the model image</param> public MatchedSURFFeature(SURFFeature observedFeature, SURFFeature[] modelFeatures, double[] dist) { ObservedFeature = observedFeature; _similarFeatures = new SimilarFeature[modelFeatures.Length]; for (int i = 0; i < modelFeatures.Length; i++) _similarFeatures[i] = new SimilarFeature(dist[i], modelFeatures[i]); }
/// <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); }