public Point2f FindExtremePoint(Mat<float> image, bool white2black) { if (image == null) { 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 (white2black) { //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 _); Cv2.CornerSubPix(image, new[] { minLoc }, new Size(3, 3), new Size(-1, -1), criteria); extremaVal = minVal; extremaLoc = minLoc; } else { Cv2.MinMaxLoc(image, out minVal,out maxVal, out Point _, out Point maxLoc); Cv2.CornerSubPix(image, new[] { maxLoc }, new Size(3, 3), new Size(-1, -1), criteria); extremaVal = maxVal; extremaLoc = maxLoc; } return extremaLoc; } 用 Point2f[] CornerSubPix(InputArray image, IEnumerable<Point2f> inputCorners, Size winSize, Size zeroZone, TermCriteria criteria);方法优化,请生成新函数
时间: 2024-02-10 07:32:52 浏览: 151
public Point2f FindExtremePoint(Mat<float> image, bool white2black)
{
if (image == null)
{
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 (white2black)
{
//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];
}
else
{
Cv2.MinMaxLoc(image, out minVal,out maxVal, out Point _, out Point maxLoc);
Point2f[] corners = new[] { new Point2f(maxLoc.X, maxLoc.Y) };
Cv2.CornerSubPix(image, corners, new Size(3, 3), new Size(-1, -1), criteria);
extremaVal = maxVal;
extremaLoc = corners[0];
}
return extremaLoc;
}
阅读全文