Я использовал функцию OpenCV cv::dnn::NMSBoxes() в модуле dnn внутри исходного кода OpenCV. Из-за ограничений среды я не могу использовать функции OpenCV, поэтому мне приходится реализовывать их вручную с нуля. Я просмотрел исходный код NMSBoxes() OpenCV и попытался собрать его самостоятельно, но существует множество зависимостей и функций, которые затрудняют простое копирование/вставку кода.
Как я могу реализовать эту функцию самостоятельно, чтобы имитировать точную функцию NMSBoxes()? Я новичок в C++.
Я использовал исходную функцию вот так. Все хорошо:
// Apply Non-Maximum Suppression to remove redundant overlapping boxes
std::vector indices;
cv::dnn::NMSBoxes(boxes, confidences, conf_thres, nms_thres, indices, 0.5);
А теперь вот что я создал на основе модуля dnn OpenCV для вызова вручную:
template
inline bool SortScorePairDescend(const std::pair& pair1,
const std::pair& pair2)
{
return pair1.first > pair2.first;
}
void GetMaxScoreIndex(const std::vector& scores, const float threshold, const int top_k,
std::vector& score_index_vec)
{
CV_DbgAssert(score_index_vec.empty());
// Generate index score pairs.
for (size_t i = 0; i < scores.size(); ++i)
{
if (scores > threshold)
{
score_index_vec.push_back(std::make_pair(scores, (int)i));
}
}
// Sort the score pair according to the scores in descending order
std::stable_sort(score_index_vec.begin(), score_index_vec.end(),
SortScorePairDescend);
// Keep top_k scores if needed.
if (top_k > 0 && top_k < (int)score_index_vec.size())
{
score_index_vec.resize(top_k);
}
}
template static inline
double jaccardDistance(const Rect_& a, const Rect_& b) {
_Tp Aa = a.area();
_Tp Ab = b.area();
if ((Aa + Ab) distance = 0
return 0.0;
}
double Aab = (a & b).area();
// distance = 1 - jaccard_index
return 1.0 - Aab / (Aa + Ab - Aab);
}
template
static inline float rectOverlap(const T& a, const T& b)
{
return 1.f - static_cast(jaccardDistance(a, b));
}
// NMSBoxesManual(boxes, confidences, conf_thres, nms_thres, indices, 0.5);
template
void NMSBoxesManual(const std::vector& bboxes,
const std::vector& scores,
const float score_threshold,
const float nms_threshold, std::vector& indices, const float eta)
{
CV_Assert(bboxes.size() == scores.size());
const int top_k = 100;
float (*computeOverlap)(const BoxType&, const BoxType&);
size_t limit = std::numeric_limits::max();
// Get top_k scores (with corresponding indices).
std::vector score_index_vec;
GetMaxScoreIndex(scores, score_threshold, top_k, score_index_vec);
// Do nms.
float adaptive_threshold = nms_threshold;
indices.clear();
for (size_t i = 0; i < score_index_vec.size(); ++i) {
const int idx = score_index_vec.second;
bool keep = true;
for (int k = 0; k < (int)indices.size() && keep; ++k) {
const int kept_idx = indices[k];
float overlap = computeOverlap(bboxes[idx], bboxes[kept_idx]);
keep = overlap = limit) {
break;
}
}
if (keep && eta < 1 && adaptive_threshold > 0.5) {
adaptive_threshold *= eta;
}
}
}
И вот какую ошибку я получаю прямо сейчас.
draw.cpp:58:30: error: 'Rect_' does not name a type
58 | double jaccardDistance(const Rect_& a, const Rect_& b) {
| ^~~~~
draw.cpp:58:35: error: expected ',' or '...' before '
Подробнее здесь: https://stackoverflow.com/questions/786 ... nnnmsboxes
Ошибка: «Rect_» не называет тип, как переопределить dnn::NMSBoxes OpenCV? ⇐ C++
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение