Public Static voidFindmatch (Mat Modelimage, Mat Observedimage, out LongMatchtime, outVectorofkeypoint Modelkeypoints, outVectorofkeypoint observedkeypoints, Vectorofvectorofdmatch matches, outMat Mask, outMat homography) { intK =2; DoubleUniquenessthreshold =0.8; DoubleHessianthresh = -; Stopwatch Watch; Homography=NULL; Modelkeypoints=NewVectorofkeypoint (); Observedkeypoints=NewVectorofkeypoint (); using(Umat umodelimage =Modelimage.toumat (accesstype.read))using(Umat uobservedimage =Observedimage.toumat (Accesstype.read)) {SURF surfcpu=NewSURF (Hessianthresh); //extract features from the object imageUmat modeldescriptors =NewUmat (); Surfcpu.detectandcompute (Umodelimage,NULL, Modelkeypoints, Modeldescriptors,false); Watch=stopwatch.startnew (); //extract features from the observed imageUmat observeddescriptors =NewUmat (); Surfcpu.detectandcompute (Uobservedimage,NULL, Observedkeypoints, Observeddescriptors,false); Bfmatcher Matcher=NewBfmatcher (DISTANCETYPE.L2); Matcher. ADD (modeldescriptors); Matcher. Knnmatch (observeddescriptors, matches, K,NULL); Mask=NewMat (matches. Size,1, depthtype.cv8u,1); Mask. Setto (NewMcvscalar (255)); Features2dtoolbox.voteforuniqueness (matches, uniquenessthreshold, mask); intNonzerocount =Cvinvoke.countnonzero (mask); if(Nonzerocount >=4) {Nonzerocount=features2dtoolbox.voteforsizeandorientation (modelkeypoints, observedkeypoints, matches, mask, /c4>1.5, -); if(Nonzerocount >=4) homography=features2dtoolbox.gethomographymatrixfrommatchedfeatures (modelkeypoints, Observedkeypoints, Matches, mask,2); } watch. Stop (); } matchtime=Watch. Elapsedmilliseconds; } /// <summary> ///Draw the model image and observed image, the matched features and homography projection. /// </summary> /// <param name= "Modelimage" >The model image</param> /// <param name= "Observedimage" >The observed image</param> /// <param name= "Matchtime" >The output total time for computing the homography matrix.</param> /// <returns>the model image and observed image, the matched features and homography projection.</returns> Public StaticMat Draw (Mat Modelimage, Mat Observedimage, out Longmatchtime) {Mat homography; Vectorofkeypoint modelkeypoints; Vectorofkeypoint observedkeypoints; using(Vectorofvectorofdmatch matches =NewVectorofvectorofdmatch ()) {Mat mask; Findmatch (Modelimage, Observedimage, outMatchtime, outModelkeypoints, outobservedkeypoints, Matches, outMask outhomography); //Draw the matched keypointsMat result =NewMat (); Features2dtoolbox.drawmatches (Modelimage, modelkeypoints, Observedimage, observedkeypoints, matches, res Ult,NewMcvscalar (255,255,255),NewMcvscalar (255,255,255), mask); #regionDraw the projected region on the imageif(Homography! =NULL) { //draw a rectangle along the projected modelRectangle rect =NewRectangle (Point.empty, modelimage.size); Pointf[] pts=Newpointf[] {NewPointF (rect. Left, rect. Bottom),NewPointF (rect. Right, rect. Bottom),NewPointF (rect. Right, rect. TOP),NewPointF (rect. Left, rect. TOP)}; PTS=Cvinvoke.perspectivetransform (pts, homography); Point[] Points= array.convertall<pointf, point>(pts, point.round); using(Vectorofpoint VP =NewVectorofpoint (points)) {Cvinvoke.polylines (result, VP,true,NewMcvscalar (255,0,0,255),5); } } #endregion returnresult; } }
SURF Characteristic method