OpenCvSharp4.6 编写的 C# 代码版本 // 求取极值点 void Caliper::FindExtremePoint() { // 1. 检查搜索路径像素数据是否为空 if(pathPixelValueAfterFilter.empty()) return; // 2. 遍历滤波后的数据,查找极值点 extremePoints.clear(); switch (polarity) { // 不考虑极性 case 0 : for(size_t i = 1; i < pathPixelValueAfterFilter.size() - 1; ++i) { // 两边异号为极值点 if (size_t(std::abs(pathPixelValueAfterFilter.at(i))) >= contrastThreshold && (0 > (pathPixelValueAfterFilter.at(i) - pathPixelValueAfterFilter.at(i - 1)) * (pathPixelValueAfterFilter.at(i + 1) - pathPixelValueAfterFilter.at(i))) ) { extremePoints.push_back(ExtremPointInfo(path.at(i), static_cast<int>(i), static_cast<int>(pathPixelValueAfterFilter.at(i)))); } } break; // 黑到白 case 1 : for(size_t i = 1; i < pathPixelValueAfterFilter.size() - 1; ++i) { // 两边异号为极值点 if (pathPixelValueAfterFilter.at(i) > 0 && size_t(std::abs(pathPixelValueAfterFilter.at(i))) >= contrastThreshold && (0 > (pathPixelValueAfterFilter.at(i) - pathPixelValueAfterFilter.at(i - 1)) * (pathPixelValueAfterFilter.at(i + 1) - pathPixelValueAfterFilter.at(i))) ) { extremePoints.push_back(ExtremPointInfo(path.at(i), static_cast<int>(i), static_cast<int>(pathPixelValueAfterFilter.at(i)))); } } break; // 白到黑 case -1: for(size_t i = 1; i < pathPixelValueAfterFilter.size() - 1; ++i) { // 两边异号为极值点 if (pathPixelValueAfterFilter.at(i) < 0 && size_t(std::abs(pathPixelValueAfterFilter.at(i))) >= contrastThreshold && (0 > (pathPixelValueAfterFilter.at(i) - pathPixelValueAfterFilter.at(i - 1)) * (pathPixelValueAfterFilter.at(i + 1) - pathPixelValueAfterFilter.at(i))) ) { extremePoints.push_back(ExtremPointInfo(path.at(i), static_cast<int>(i), static_cast<int>(pathPixelValueAfterFilter.at(i)))); } } break; default: assert(false); break; }
时间: 2024-04-26 07:22:55 浏览: 168
这段代码是用于在图像中搜索极值点的,主要包括以下步骤:
1. 检查搜索路径像素数据是否为空;
2. 遍历滤波后的数据,查找极值点;
3. 根据极性类型分别处理不同的情况,包括不考虑极性、黑到白和白到黑;
4. 找到极值点后将其信息保存到 `extremePoints` 中。
其中,`pathPixelValueAfterFilter` 是经过滤波后的搜索路径像素数据,`contrastThreshold` 是对比度的阈值,`path` 是搜索路径的像素坐标,`polarity` 是极性类型。
相关问题
OpenCvSharp4.6 编写的 C# 代码版本 // 求取极值点 void Caliper::FindExtremePoint() { // 1. 检查搜索路径像素数据是否为空 if(pathPixelValueAfterFilter.empty()) return; // 2. 遍历滤波后的数据,查找极值点 extremePoints.clear(); switch (polarity) { // 不考虑极性 case 0 : for(size_t i = 1; i < pathPixelValueAfterFilter.size() - 1; ++i) { // 两边异号为极值点 if (size_t(std::abs(pathPixelValueAfterFilter.at(i))) >= contrastThreshold && (0 > (pathPixelValueAfterFilter.at(i) - pathPixelValueAfterFilter.at(i - 1)) * (pathPixelValueAfterFilter.at(i + 1) - pathPixelValueAfterFilter.at(i))) ) { extremePoints.push_back(ExtremPointInfo(path.at(i), static_cast<int>(i), static_cast<int>(pathPixelValueAfterFilter.at(i)))); } } break; // 黑到白 case 1 : for(size_t i = 1; i < pathPixelValueAfterFilter.size() - 1; ++i) { // 两边异号为极值点 if (pathPixelValueAfterFilter.at(i) > 0 && size_t(std::abs(pathPixelValueAfterFilter.at(i))) >= contrastThreshold && (0 > (pathPixelValueAfterFilter.at(i) - pathPixelValueAfterFilter.at(i - 1)) * (pathPixelValueAfterFilter.at(i + 1) - pathPixelValueAfterFilter.at(i))) ) { extremePoints.push_back(ExtremPointInfo(path.at(i), static_cast<int>(i), static_cast<int>(pathPixelValueAfterFilter.at(i)))); } } break; // 白到黑 case -1: for(size_t i = 1; i < pathPixelValueAfterFilter.size() - 1; ++i) { // 两边异号为极值点 if (pathPixelValueAfterFilter.at(i) < 0 && size_t(std::abs(pathPixelValueAfterFilter.at(i))) >= contrastThreshold && (0 > (pathPixelValueAfterFilter.at(i) - pathPixelValueAfterFilter.at(i - 1)) * (pathPixelValueAfterFilter.at(i + 1) - pathPixelValueAfterFilter.at(i))) ) { extremePoints.push_back(ExtremPointInfo(path.at(i), static_cast<int>(i), static_cast<int>(pathPixelValueAfterFilter.at(i)))); } } break; default: assert(false); break; }OpenCvSharp4.6 编写的 C# 代码版本函数
这段代码是用于在图像中搜索极值点的,主要包括以下步骤:
1. 检查搜索路径像素数据是否为空;
2. 遍历滤波后的数据,查找极值点;
3. 根据极性类型分别处理不同的情况,包括不考虑极性、黑到白和白到黑;
4. 找到极值点后将其信息保存到 `extremePoints` 中。
其中,`pathPixelValueAfterFilter` 是经过滤波后的搜索路径像素数据,`contrastThreshold` 是对比度的阈值,`path` 是搜索路径的像素坐标,`polarity` 是极性类型。
用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; } }
以下是使用 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);
}
}
}
```
注意,由于缺少完整的代码,我无法保证此代码的完全正确性。因此,如果您在使用此代码时遇到任何问题,请自行进行调试和修改。
阅读全文