// 极值点评分 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; } }//ExtremePointRating 基于opencvsharp4.6 请优化代码,生成新的函数
时间: 2024-02-14 16:10:39 浏览: 116
为了优化代码,可以考虑以下几个方面:
1. 减少不必要的类型转换
在原有代码中,存在如下的类型转换:
```
int halfLen = static_cast<int>(path.size() / 2);
```
可以改为使用整数除法运算符 `/`,避免类型转换的开销:
```
int halfLen = path.size() / 2;
```
2. 避免不必要的容器复制
在原有代码中,存在如下的容器复制操作:
```
std::vector<ExtremPointInfo> epf(extremePoints.begin(), extremePoints.begin() + 3);
```
可以改为直接使用 `std::array`,避免不必要的容器复制:
```
std::array<ExtremPointInfo, 3> epf = {extremePoints[0], extremePoints[1], extremePoints[2]};
```
3. 使用 lambda 表达式简化代码
在原有代码中,使用了函数指针作为 `sort` 函数的比较函数:
```
sort(extremePoints.begin(), extremePoints.end(), CompareExtremPointInfoBysocre);
sort(epf.begin(), epf.end(), CompareExtremPointInfoByPs);
```
可以改为使用 lambda 表达式,使代码更加简洁:
```
sort(extremePoints.begin(), extremePoints.end(), [](const ExtremPointInfo& a, const ExtremPointInfo& b) { return a.score > b.score; });
sort(epf.begin(), epf.end(), [](const ExtremPointInfo& a, const ExtremPointInfo& b) { return a.ps > b.ps; });
```
基于以上优化,可以得到如下的新函数:
```
void Caliper::ExtremePointRatingOptimized()
{
if (extremePoints.empty())
return;
// 评分: value * (1 - |i - 0.5 * le| / 0.5 * len)
int halfLen = 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(), [](const ExtremPointInfo& a, const ExtremPointInfo& b) { return a.score > b.score; });
result.peak = extremePoints[0].pt;
if (extremePoints.size() == 1)
{
result.center = result.front = result.back = extremePoints[0].pt;
return;
}
if (extremePoints.size() == 2)
{
int a = std::abs(extremePoints[0].ps - halfLen);
int b = std::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;
}
}
else
{
std::array<ExtremPointInfo, 3> epf = {extremePoints[0], extremePoints[1], extremePoints[2]};
// 降序
sort(epf.begin(), epf.end(), [](const ExtremPointInfo& a, const ExtremPointInfo& b) { return a.ps > b.ps; });
result.back = epf[0].pt;
result.center = epf[1].pt;
result.front = epf[2].pt;
}
}
```
阅读全文