/// <summary> /// Copy constructor /// </summary> /// <param name="other"></param> public MatchesInfo(MatchesInfo other) { if (other == null) { throw new ArgumentNullException(nameof(other)); } SrcImgIdx = other.SrcImgIdx; DstImgIdx = other.DstImgIdx; Matches = other.Matches; InliersMask = other.InliersMask; NumInliers = other.NumInliers; H = other.H; Confidence = other.Confidence; }
/// <summary> /// Performs images matching. /// </summary> /// <param name="features">Features of the source images</param> /// <param name="mask">Mask indicating which image pairs must be matched</param> /// <returns>Found pairwise matches</returns> public virtual MatchesInfo[] Apply( IEnumerable <ImageFeatures> features, Mat?mask = null) { if (features == null) { throw new ArgumentNullException(nameof(features)); } ThrowIfDisposed(); var featuresArray = features.CastOrToArray(); if (featuresArray.Length == 0) { throw new ArgumentException("Empty features array", nameof(features)); } var keypointVecs = new VectorOfKeyPoint?[featuresArray.Length]; var wImageFeatures = new WImageFeatures[featuresArray.Length]; try { for (int i = 0; i < featuresArray.Length; i++) { if (featuresArray[i].Descriptors == null) { throw new ArgumentException("features contain null descriptor mat", nameof(features)); } featuresArray[i].Descriptors.ThrowIfDisposed(); keypointVecs[i] = new VectorOfKeyPoint(); wImageFeatures[i] = new WImageFeatures { ImgIdx = featuresArray[i].ImgIdx, ImgSize = featuresArray[i].ImgSize, Keypoints = keypointVecs[i] !.CvPtr, Descriptors = featuresArray[i].Descriptors.CvPtr, }; } using var srcImgIndexVecs = new VectorOfInt32(); using var dstImgIndexVecs = new VectorOfInt32(); using var matchesVec = new VectorOfVectorDMatch(); using var inlinersMaskVec = new VectorOfVectorByte(); using var numInliersVecs = new VectorOfInt32(); using var hVecs = new VectorOfMat(); using var confidenceVecs = new VectorOfDouble(); NativeMethods.HandleException( NativeMethods.stitching_FeaturesMatcher_apply2( ptr, wImageFeatures, wImageFeatures.Length, mask?.CvPtr ?? IntPtr.Zero, srcImgIndexVecs.CvPtr, dstImgIndexVecs.CvPtr, matchesVec.CvPtr, inlinersMaskVec.CvPtr, numInliersVecs.CvPtr, hVecs.CvPtr, confidenceVecs.CvPtr )); var srcImgIndices = srcImgIndexVecs.ToArray(); var dstImgIndices = dstImgIndexVecs.ToArray(); var matches = matchesVec.ToArray(); var inlinersMasks = inlinersMaskVec.ToArray(); var numInliers = numInliersVecs.ToArray(); var hs = hVecs.ToArray(); var confidences = confidenceVecs.ToArray(); var result = new MatchesInfo[srcImgIndices.Length]; for (int i = 0; i < srcImgIndices.Length; i++) { result[i] = new MatchesInfo( srcImgIndices[i], dstImgIndices[i], matches[i], inlinersMasks[i], numInliers[i], hs[i], confidences[i]); } return(result); } finally { foreach (var vec in keypointVecs) { vec?.Dispose(); } GC.KeepAlive(this); } }