public void FindExtremePoint(Mat image, bool searchDir_left2right, ref List<Point2f> zeroPList) { if (image.Width == 0) { return;// throw new ArgumentNullException(nameof(image)); } // 预处理:中值滤波 Cv2.MedianBlur(image, image, 3); // 计算极值和位置 Point2f extremaLoc; double extremaVal; double minVal, maxVal; var criteria = new TermCriteria(CriteriaTypes.Eps | CriteriaTypes.Count, 20, 0.03); //MinMaxLoc(InputArray src, out double minVal, out double maxVal, out Point minLoc, out Point maxLoc, InputArray? mask = null); Cv2.MinMaxLoc(image, out minVal, out maxVal, out Point minLoc, out Point _); Point2f[] corners = new[] { new Point2f(minLoc.X, minLoc.Y) }; Cv2.CornerSubPix(image, corners, new Size(3, 3), new Size(-1, -1), criteria); extremaVal = minVal; extremaLoc = corners[0]; zeroPList.Add(extremaLoc); } 增加searchDir_left2right的判断程序 基于opencvsharp4.6编写c#代码,生成新的函数
时间: 2024-01-23 07:03:55 浏览: 86
以下是增加了searchDir_left2right判断的基于OpenCvSharp4.6编写的C#函数:
```
public void FindExtremePoint(Mat image, bool searchDir_left2right, ref List<Point2f> zeroPList)
{
if (image.Width == 0)
{
return;// throw new ArgumentNullException(nameof(image));
}
// 预处理:中值滤波
Cv2.MedianBlur(image, image, 3);
// 计算极值和位置
Point2f extremaLoc;
double extremaVal;
double minVal, maxVal;
var criteria = new TermCriteria(CriteriaTypes.Eps | CriteriaTypes.Count, 20, 0.03);
if (searchDir_left2right)
{
// 从左往右找点
Cv2.MinMaxLoc(image, out _, out maxVal, out _, out Point maxLoc);
}
else
{
// 从右往左找点
Cv2.MinMaxLoc(image, out minVal, out _, out Point minLoc, out _, image);
Point2f[] corners = new[] { new Point2f(minLoc.X, minLoc.Y) };
Cv2.CornerSubPix(image, corners, new Size(3, 3), new Size(-1, -1), criteria);
extremaVal = minVal;
extremaLoc = corners[0];
zeroPList.Add(extremaLoc);
return;
}
Point2f[] corners2 = new[] { new Point2f(maxLoc.X, maxLoc.Y) };
Cv2.CornerSubPix(image, corners2, new Size(3, 3), new Size(-1, -1), criteria);
extremaVal = maxVal;
extremaLoc = corners2[0];
zeroPList.Add(extremaLoc);
}
```
增加了一个bool类型的参数searchDir_left2right,用于控制从左往右或从右往左查找极值点。如果searchDir_left2right为true,就从左往右查找最大值点;否则从右往左查找最小值点,并对其进行精细化处理。最后将极值点的位置信息加入到zeroPList列表中。
阅读全文