private Mat GoFindContours(Mat mat, string name, out Mat[] countoursMat)
{
countoursMat = null;
Mat useLessMat = new Mat();
Mat Gray;
Mat Bgr;
Mat binary = new Mat();
Mat NoNiosePointMat = new Mat();
if (!mat.GetGrayAndBgr(out Gray, out Bgr)) return null;
//在背景中提取亮目标,TRIANGLE法优于OTSU法,而在亮背景中提取暗目标,OTSU法优于TRIANGLE法
//Cv2.Threshold(Gray, binary, Parameters.ThreshVal, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
Cv2.Threshold(Gray, binary, Parameters.ThreshVal, 255, ThresholdTypes.Binary);
//当binary二值化图像存在噪点时,使用中值滤波去除噪点,使其更好的查找和绘制轮廓
Cv2.MedianBlur(binary, NoNiosePointMat, Parameters.KernelSize);
//腐蚀,查找精度更高的轮廓
InputArray kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3), new Point(-1, -1));
Cv2.Erode(NoNiosePointMat, NoNiosePointMat, kernel);
//获取Mat类型的轮廓,以便后续查找点位是否在轮廓上
Cv2.FindContours(NoNiosePointMat, out countoursMat, useLessMat, (RetrievalModes)Enum.Parse(typeof(RetrievalModes), Parameters.SelectRetrievalModes), ContourApproximationModes.ApproxNone);
ContourList[name] = contours.OrderBy(p => p.Length).ToArray();
//获取轮廓内所有的点
var isFind = NoNiosePointMat.FindNonZero().GetArray(out Point[] points);
//是一个闭合图像,有两条闭合轮廓
Parallel.ForEach(points, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, (p, loopState) =>
{
//是否返回距离值,如果是false,1表示在内面,0表示在边界上,-1表示在外部,true返回实际距离
if (Cv2.PointPolygonTest(countoursMat[0], p, false) != 0 && Cv2.PointPolygonTest(countoursMat[1], p, false) != 0)
{
}
})
return NoNiosePointMat;
}
标签:遍历,Mat,NoNiosePointMat,C#,Cv2,OpenCvSharp,new,轮廓,out From: https://www.cnblogs.com/KenZpp/p/16590776.html