本文整理汇总了C#中Mat.FindContours方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.FindContours方法的具体用法?C# Mat.FindContours怎么用?C# Mat.FindContours使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mat
的用法示例。
在下文中一共展示了Mat.FindContours方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: prepareHandDetector
void prepareHandDetector()
{
try
{
Mat kernel = Cv2.GetStructuringElement(StructuringElementShape.Ellipse, new Size(5, 5));
string path1 = Application.dataPath + "/../1.png";
string path2 = Application.dataPath + "/../2.png";
string path3 = Application.dataPath + "/../3.png";
string path4 = Application.dataPath + "/../4.png";
Mat src1 = new Mat(path1, LoadMode.GrayScale);
Mat src2 = new Mat(path2, LoadMode.GrayScale);
Mat src3 = new Mat(path3, LoadMode.GrayScale);
Mat src4 = new Mat(path4, LoadMode.GrayScale);
Cv2.MorphologyEx(src1, src1, MorphologyOperation.Open, kernel);
Cv2.MorphologyEx(src1, src1, MorphologyOperation.Close, kernel);
Cv2.MorphologyEx(src2, src2, MorphologyOperation.Open, kernel);
Cv2.MorphologyEx(src2, src2, MorphologyOperation.Close, kernel);
Cv2.MorphologyEx(src3, src3, MorphologyOperation.Open, kernel);
Cv2.MorphologyEx(src3, src3, MorphologyOperation.Close, kernel);
Cv2.MorphologyEx(src4, src4, MorphologyOperation.Open, kernel);
Cv2.MorphologyEx(src4, src4, MorphologyOperation.Close, kernel);
Cv2.Blur(src1, src1, new Size(5, 5));
Cv2.Blur(src2, src2, new Size(5, 5));
Cv2.Blur(src3, src3, new Size(5, 5));
Cv2.Blur(src4, src4, new Size(5, 5));
//Detect edges using canny
//double cthresh = 0.0;
//Cv2.Canny(src1, src1, cthresh, cthresh * 2, 3, true);
//Cv2.Canny(src2, src2, cthresh, cthresh * 2, 3, true);
//Cv2.Canny(src3, src3, cthresh, cthresh * 2, 3, true);
//Cv2.Canny(src4, src4, cthresh, cthresh * 2, 3, true);
Mat[] contoursA;
OutputArray hierarchyA = InputOutputArray.Create(new List<Vec4i>());
src1.FindContours(out contoursA, hierarchyA, OpenCvSharp.ContourRetrieval.CComp, OpenCvSharp.ContourChain.ApproxTC89KCOS);
Mat[] contoursB;
OutputArray hierarchyB = InputOutputArray.Create(new List<Vec4i>());
src2.FindContours(out contoursB, hierarchyB, OpenCvSharp.ContourRetrieval.CComp, OpenCvSharp.ContourChain.ApproxTC89KCOS);
Mat[] contoursC;
OutputArray hierarchyC = InputOutputArray.Create(new List<Vec4i>());
src3.FindContours(out contoursC, hierarchyC, OpenCvSharp.ContourRetrieval.CComp, OpenCvSharp.ContourChain.ApproxTC89KCOS);
Mat[] contoursD;
OutputArray hierarchyD = InputOutputArray.Create(new List<Vec4i>());
src4.FindContours(out contoursD, hierarchyD, OpenCvSharp.ContourRetrieval.CComp, OpenCvSharp.ContourChain.ApproxTC89KCOS);
handsContours = new Mat[][] { contoursA, contoursB, contoursC, contoursD };
//Cv2.ImShow("1", src1);
//Cv2.ImShow("2", src2);
//Cv2.ImShow("3", src3);
//Cv2.ImShow("4", src4);
//Debug.Log(src1.Channels() + " " + src1.Depth() + " " + src1.Channels() + " " + src2.Depth());
///// Find contours
//Mat[] contours = new Mat[]();
//CvMemStorage storage = new CvMemStorage();
//src1.FindContours(out contours, storage, ContourRetrieval.Tree, ContourChain.ApproxSimple);
//contours = contours.ApproxPoly(storage, ApproxPolyMethod.DP, 3, true);
//SURF sift = new SURF(0.1, 4, 2, true, false);
// Detect the keypoints and generate their descriptors using SIFT
//KeyPoint[] keypoints1, keypoints2;
//MatOfFloat descriptors1 = new MatOfFloat();
//MatOfFloat descriptors2 = new MatOfFloat();
//sift.Run(gray1, null, out keypoints1, descriptors1);
//sift.Run(gray2, null, out keypoints2, descriptors2);
// Detect the keypoints and generate their descriptors using SIFT
//Cv2.InitModule_NonFree();
//SIFT sift = new SIFT();
//KeyPoint[] keypoints1, keypoints2;
//MatOfFloat descriptors1 = new MatOfFloat();
//MatOfFloat descriptors2 = new MatOfFloat();
//sift.Run(src1, null, out keypoints1, descriptors1);
//sift.Run(src2, null, out keypoints2, descriptors2);
//// Matching descriptor vectors with a brute force matcher
//BFMatcher matcher = new BFMatcher(NormType.L2, false);
//DMatch[] matches = matcher.Match(descriptors1, descriptors2);
//// Draw matches
//Mat view = new Mat();
//Cv2.DrawMatches(src1, keypoints1, src2, keypoints2, matches, view);
//Cv2.ImShow("SIFT-BF",view);
//// Match descriptor vectors FLANN
//FlannBasedMatcher flannMatcher = new FlannBasedMatcher();
//matches = flannMatcher.Match(descriptors1, descriptors2);
//// Draw matches
//Mat siftflann = new Mat();
//Cv2.DrawMatches(src1, keypoints1, src2, keypoints2, matches, siftflann);
//Cv2.ImShow("Sift-flann", siftflann);
////////SURF
//// Detect the keypoints and generate their descriptors using SURF
//SURF surf = new SURF(300, 4, 2, false);
//surf.Run(src1, null, out keypoints1, descriptors1);
//surf.Run(src2, null, out keypoints2, descriptors2);
//.........这里部分代码省略.........
示例2: DetectHands
bool DetectHands(CvBlob blob)
{
double cthresh = 0.0;
Mat blobMat = new Mat(fgthresh, blob.Rect);
Cv2.Blur(blobMat, blobMat, new Size(5, 5));
//Cv2.Canny(blobMat, blobMat, cthresh, cthresh * 2, 3);
Mat[] contoursQuery;
OutputArray hierarchyQ = InputOutputArray.Create(new List<Vec4i>());
blobMat.FindContours(out contoursQuery, hierarchyQ, OpenCvSharp.ContourRetrieval.CComp, OpenCvSharp.ContourChain.ApproxTC89KCOS);
double scoreTotal = 0.0;
double tested = 0.0;
foreach (var modelContours in handsContours)
{
foreach (var contourToMatch in contoursQuery)
{
foreach (var contourB in modelContours)
{
double ratio = Cv2.MatchShapes(contourToMatch, contourB, MatchShapesMethod.I1);
//Debug.Log("Matching: " + ratio + " " + contourB.IsContourConvex() + " " + contourToMatch.IsContourConvex() );
//if (ratio > 0.0000001)
//{
scoreTotal += ratio;
tested++;
//}
}
}
}
scoreTotal /= tested;
//Debug.Log(scoreTotal);
if (scoreTotal > 0.0000001 && scoreTotal <= handsThreshold)
{
return true;
}else{
return false;
}
}