std::vector<cv::Rect>

std::vector<cv::Rect> 是 C++ 中表示一个包含多个 cv::Rect 对象的动态数组(向量)的类型。这里,std::vector 是 C++ 标准库中的一个模板类,用于表示动态大小的数组,而 cv::Rect 是 OpenCV 库中用于表示矩形区域的一个类。

cv::Rect 通常包含四个成员变量:xywidth 和 height,分别表示矩形的左上角坐标和矩形的宽度与高度。

当你有一个 std::vector<cv::Rect> 时,你可以在这个向量中存储多个矩形区域,这在计算机视觉任务中非常有用,比如对象检测、面部识别或图像分割等场景。

以下是一个简单的示例,展示了如何使用 std::vector<cv::Rect>

 
#include <vector>  
#include <opencv2/opencv.hpp>  
#include <iostream>  
  
int main() {  
    // 创建一个 std::vector<cv::Rect> 并初始化  
    std::vector<cv::Rect> rects = {  
        cv::Rect(10, 10, 50, 50),  // 第一个矩形:左上角(10,10),宽50,高50  
        cv::Rect(70, 70, 40, 40)   // 第二个矩形:左上角(70,70),宽40,高40  
    };  
  
    // 遍历 rects 向量并打印每个矩形的信息  
    for (const auto& rect : rects) {  
        std::cout << "Rectangle: x=" << rect.x << ", y=" << rect.y  
                  << ", width=" << rect.width << ", height=" << rect.height << std::endl;  
    }  
  
    // 假设你有一个OpenCV的Mat对象(图像),你可以在这些矩形区域上绘制边界框  
    cv::Mat image = cv::Mat::zeros(200, 200, CV_8UC3); // 创建一个200x200的黑色图像  
    for (const auto& rect : rects) {  
        cv::rectangle(image, rect, cv::Scalar(0, 255, 0), 2); // 在图像上用绿色线条绘制矩形  
    }  
  
    // 显示图像(需要OpenCV的imshow函数)  
    cv::imshow("Rectangles", image);  
    cv::waitKey(0); // 等待用户按键  
  
    return 0;  
}

在这个例子中,我们首先创建了一个 std::vector<cv::Rect> 并初始化了两个矩形。然后,我们遍历这个向量并打印出每个矩形的信息。最后,我们假设有一个 OpenCV 的 Mat 对象(代表图像),并在这些矩形区域上绘制绿色的边界框,然后显示这个图像。

请注意,为了运行上述代码,你需要安装 OpenCV 库并正确配置你的项目以包含 OpenCV 的头文件和库文件。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在vs2015 c++ .h中加入这段代码会报重定义 namespace cv_dnn { namespace { template <typename T> static inline bool SortScorePairDescend(const std::pair<float, T>& pair1, const std::pair<float, T>& pair2) { return pair1.first > pair2.first; } } // namespace inline void GetMaxScoreIndex(const std::vector<float>& scores, const float threshold, const int top_k, std::vector<std::pair<float, int> >& score_index_vec) { for (size_t i = 0; i < scores.size(); ++i) { if (scores[i] > threshold) { score_index_vec.push_back(std::make_pair(scores[i], i)); } } std::stable_sort(score_index_vec.begin(), score_index_vec.end(), SortScorePairDescend<int>); if (top_k > 0 && top_k < (int)score_index_vec.size()) { score_index_vec.resize(top_k); } } template <typename BoxType> inline void NMSFast_(const std::vector<BoxType>& bboxes, const std::vector<float>& scores, const float score_threshold, const float nms_threshold, const float eta, const int top_k, std::vector<int>& indices, float(*computeOverlap)(const BoxType&, const BoxType&)) { CV_Assert(bboxes.size() == scores.size()); std::vector<std::pair<float, int> > 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[i].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 <= adaptive_threshold; } if (keep) indices.push_back(idx); if (keep && eta < 1 && adaptive_threshold > 0.5) { adaptive_threshold *= eta; } } } // copied from opencv 3.4, not exist in 3.0 template<typename Tp> static inline double jaccardDistance_(const Rect_<Tp>& a, const Rect<_Tp>& b) { Tp Aa = a.area(); Tp Ab = b.area(); if ((Aa + Ab) <= std::numeric_limits<Tp>::epsilon()) { // jaccard_index = 1 -> distance = 0 return 0.0; } double Aab = (a & b).area(); // distance = 1 - jaccard_index return 1.0 - Aab / (Aa + Ab - Aab); } template <typename T> static inline float rectOverlap(const T& a, const T& b) { return 1.f - static_cast<float>(jaccardDistance(a, b)); } void NMSBoxes(const std::vector<Rect>& bboxes, const std::vector<float>& scores, const float score_threshold, const float nms_threshold, std::vector<int>& indices, const float eta = 1, const int top_k = 0) { NMSFast(bboxes, scores, score_threshold, nms_threshold, eta, top_k, indices, rectOverlap); } }
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值