C# OpenCV 通过高度图去筛选轮廓
//输入图像
threshCropMap.ImWrite("D:\\test\\threshCropMap_BeforeFilterByBlob.bmp");
//设定我们要筛选的高度
var ResultHeight = 60;
//创建对应高度的图像,由于是高度信息图,所有要使用32位来存放数据
Mat mat = new Mat(filter.Rows, filter.Cols, MatType.CV_32F, new Scalar(ResultHeight));
mat.ImWrite("D:\\test\\mat.bmp");
Mat ResultMat = new Mat(filter.Rows, filter.Cols, MatType.CV_32F, new Scalar(0));
//将基础图像减去高度图像,将低于指定高度的图像置为负值
Cv2.Subtract(threshCropMap, mat, ResultMat);
ResultMat.ImWrite("D:\\test\\ResultMat.bmp");
//将负值的数值置为0,仅保留大于指定高度的图像
Mat mask = new Mat();
Cv2.Compare(ResultMat, new Scalar(0), mask, CmpType.GT);
mask.ImWrite("D:\\test\\mask.bmp");
Mat resultMat = new Mat();
ResultMat.CopyTo(resultMat, mask);
//将最终图像转换为8位灰度图,方便后续处理结果
resultMat.ConvertTo(resultMat, MatType.CV_8UC1);
int num = resultMat.CountNonZero();
Point[][] contours;
HierarchyIndex[] hierarchy;
Cv2.FindContours(resultMat, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple);