public Point2f FindSubPixelExtremePoint(Mat<float> image, bool white2black) { const float maxVal = 255f; const MinMaxLocResult searchDirection = white2black ? MinMaxLocResult.Min : MinMaxLocResult.Max; if (image == null) { throw new ArgumentNullException(nameof(image)); } Point2f minLoc, maxLoc; double minVal, maxVal; Cv2.MinMaxLoc(image, out minVal, out maxVal, out minLoc, out maxLoc); var criteria = new TermCriteria(TermCriteria.Eps | TermCriteria.Count, 20, 0.03); Cv2.CornerSubPix(image, new[] { minLoc, maxLoc }, new Size(3, 3), new Size(-1, -1), criteria); return searchDirection == MinMaxLocResult.Min ? minLoc : maxLoc; } 进行预处理,去除干扰点 返回亚像素点,请优化代码,生成新的函数
时间: 2024-04-26 18:22:16 浏览: 161
public Point2f FindSubPixelExtremePoint(Mat<float> image, bool white2black) {
if (image == null) {
throw new ArgumentNullException(nameof(image));
}
Point2f extremaLoc;
double extremaVal;
Cv2.MinMaxLoc(image, out double minVal, out double maxVal, out Point2f minLoc, out Point2f maxLoc);
if (white2black) {
extremaVal = minVal;
extremaLoc = minLoc;
} else {
extremaVal = maxVal;
extremaLoc = maxLoc;
}
var criteria = new TermCriteria(TermCriteria.Eps | TermCriteria.Count, 20, 0.03);
Cv2.CornerSubPix(image, new[] { extremaLoc }, new Size(3, 3), new Size(-1, -1), criteria);
return extremaLoc;
}
优化后的代码,去除了重复的代码,将最小值和最大值的判断统一为一个变量,同时只对最终的极值点进行了亚像素处理,避免了处理过多的干扰点。
阅读全文