用opencvsharp编写函数实现以下功能 // 极值点评分 void Caliper::ExtremePointRating() { if(extremePoints.empty()) return; // 评分: value * (1 - |i - 0.5 * le| / 0.5 * len) int halfLen = static_cast<int>(path.size() / 2); for(auto& e: extremePoints) { e.score = e.value * (1 - static_cast<double>(std::abs(e.ps - halfLen)) / halfLen); } //按评分排序:降序 sort(extremePoints.begin(), extremePoints.end(), CompareExtremPointInfoBysocre); result.peak = extremePoints.at(0).pt; if(extremePoints.size() == 1) { result.center = result.front = result.back = extremePoints.at(0).pt; return; } if(extremePoints.size() == 2) { int a = std::abs(extremePoints.at(0).ps - halfLen); int b = std::abs(extremePoints.at(1).ps - halfLen); if(a > b) { result.front = result.back = extremePoints.at(0).pt; result.center = extremePoints.at(1).pt; }else { } }else { std::vector<ExtremPointInfo> epf(extremePoints.begin(), extremePoints.begin() + 3); sort(epf.begin(), epf.end(), CompareExtremPointInfoByPs);//降序 result.back = epf.at(0).pt; result.center = epf.at(1).pt; result.front = epf.at(2).pt; } }
时间: 2024-03-09 21:44:47 浏览: 69
以下是使用 OpenCvSharp3 编写的实现上述功能的代码:
```csharp
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
public class Caliper
{
public void ExtremePointRating()
{
if (extremePoints.Count == 0)
return;
int halfLen = path.Count / 2;
foreach (var e in extremePoints)
{
e.score = e.value * (1 - Math.Abs(e.ps - 0.5 * path.Count) / halfLen);
}
extremePoints.Sort(new CompareExtremPointInfoBysocre());
result.peak = extremePoints[0].pt;
if (extremePoints.Count == 1)
{
result.center = result.front = result.back = extremePoints[0].pt;
return;
}
if (extremePoints.Count == 2)
{
int a = Math.Abs(extremePoints[0].ps - halfLen);
int b = Math.Abs(extremePoints[1].ps - halfLen);
if (a > b)
{
result.front = result.back = extremePoints[0].pt;
result.center = extremePoints[1].pt;
}
else
{
result.front = result.back = extremePoints[1].pt;
result.center = extremePoints[0].pt;
}
return;
}
var epf = extremePoints.Take(3).ToList();
epf.Sort(new CompareExtremPointInfoByPs());
result.back = epf[0].pt;
result.center = epf[1].pt;
result.front = epf[2].pt;
}
private class CompareExtremPointInfoBysocre : IComparer<ExtremPointInfo>
{
public int Compare(ExtremPointInfo x, ExtremPointInfo y)
{
return -x.score.CompareTo(y.score);
}
}
private class CompareExtremPointInfoByPs : IComparer<ExtremPointInfo>
{
public int Compare(ExtremPointInfo x, ExtremPointInfo y)
{
return -x.ps.CompareTo(y.ps);
}
}
}
```
注意,由于缺少完整的代码,我无法保证此代码的完全正确性。因此,如果您在使用此代码时遇到任何问题,请自行进行调试和修改。
阅读全文