21xrx.com
2025-04-17 15:50:06 Thursday
文章检索 我的文章 写文章
C++编程实现多类别的极大值抑制算法
2023-07-06 20:44:30 深夜i     19     0
C++编程 多类别 极大值抑制 算法 实现

极大值抑制(Non-Maximum Suppression, NMS)是一种经典的计算机视觉算法,用于筛选图像中的极大值点。在目标检测等任务中经常用到。C++编程实现多类别的极大值抑制算法是一项挑战性很高的任务,在本文中我们将介绍一种实现方法。

首先,我们需要实现一种数据结构来存储检测到的目标。我们可以定义一个结构体或类来表示每个目标的位置、大小、置信度和类别。类别可以是整数或字符串,表示目标所属的类别。

接下来,我们需要实现一个NMS算法来筛选目标。算法的核心思想是对于同一类别的目标,选择置信度最高的目标,并将其它与该目标高度重叠的目标删除。这个过程不断进行,直到所有目标完成筛选。

在实现过程中,我们可以使用一些现成的库,例如OpenCV,来加速处理图像和计算目标之间的IoU(交并比)。

下面是C++代码实现的示例:

#include <vector>
#include <algorithm>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
struct DetectionResult height; // 目标的宽高
  float confidence; // 置信度
  string category; // 目标所属的类别
;
vector<DetectionResult> NMS(vector<DetectionResult>& detections, float threshold) {
  vector<DetectionResult> results;
  sort(detections.begin(), detections.end(), [](const DetectionResult& a, const DetectionResult& b)
    return a.confidence > b.confidence;
  );
  while (!detections.empty()) {
    auto max_iter = detections.begin();
    for (auto iter = max_iter + 1; iter != detections.end(); ) {
      if (iter->category == max_iter->category) {
        float iou = (float)(max_iter->width * max_iter->height) / ((max_iter->width + iter->width) * (max_iter->height + iter->height));
        if (iou > threshold) {
          iter = detections.erase(iter);
        } else {
          ++iter;
        }
      } else {
        ++iter;
      }
    }
    results.push_back(*max_iter);
    detections.erase(max_iter);
  }
  return results;
}
int main() {
  Mat image = imread("input.jpg");
  // 假设使用了一个目标检测器,得到了以下检测结果
  vector<DetectionResult> detections = {
     100,
     "person" ,
     130,
     0.6,
     150,
  };
  vector<DetectionResult> results = NMS(detections, 0.5);
  // 显示筛选后的结果
  for (auto& result : results) {
    rectangle(image, Point(result.x, result.y), Point(result.x + result.width, result.y + result.height), Scalar(0, 255, 0), 2);
    putText(image, result.category, Point(result.x, result.y), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255), 2);
  }
  imshow("Result", image);
  waitKey(0);
  return 0;
}

这段代码中,我们使用sort函数将检测结果按照置信度从大到小排序。然后,对于每个置信度最高的检测结果,我们计算其与其它检测结果的IoU,并将与其重叠度高于阈值的检测结果删除。最后,将保留下来的检测结果存入结果列表中返回,并进行显示。

在实际使用中,我们需要根据具体的应用场景调整参数,例如阈值等。同时,我们需要确保目标检测器输出的检测结果符合NMS算法的输入要求。如果需要考虑多帧的连续处理,则需要加入一些额外的逻辑,例如对于同一目标的跟踪等。总之,C++编程实现多类别的极大值抑制算法是一个麻烦而重要的任务,需要对图像算法、数据结构和C++编程技能有较高的要求。

  
  

评论区

请求出错了