OpenCV中DPM(Deformable Part Model)目标检测类cv::dpm::DPMDetector
- 操作系统:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 编程语言:C++11
算法描述
OpenCV 中用于基于可变形部件模型(DPM) 的目标检测器,主要用于行人、人脸等目标的检测。它是一种传统的基于特征的目标检测方法,不依赖深度学习,而是使用 HOG 特征 + 部件模型来进行检测。
示例代码
#include <iostream>
#include <opencv2/dpm.hpp>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace cv::dpm;
using namespace std;int main()
{// 指定模型文件路径(支持多个类别)vector< String > modelPaths = { "car.xml" }; // 可添加多个如 "car.xml"vector< String > classNames = { "car" }; // 类别名称// 创建 DPM 检测器Ptr< DPMDetector > detector = DPMDetector::create( modelPaths, classNames );if ( detector.empty() ){cerr << "Failed to create DPMDetector!" << endl;return -1;}// 读取图像Mat image = imread( "/media/dingxin/data/study/OpenCV/sources/images/cars.png" );if ( image.empty() ){cerr << "Failed to load image!" << endl;return -1;}imshow( "Original", image );// 执行检测vector< DPMDetector::ObjectDetection > detections;detector->detect( image, detections );// 显示检测结果for ( const auto& det : detections ){if(det.score > 1.0)rectangle( image, det.rect, Scalar( 0, 255, 0 ), 2 );cout << "Detected object with score: " << det.score << endl;}imshow( "Detections", image );waitKey();return 0;
}
运行结果
模型文件获取
你需要下载 .xml 格式的 DPM 模型文件才能运行检测。
官方支持的模型包括:
类别 | 文件名 |
---|---|
行人 | (Pedestrian) |
轿车(Car) | car.xml |
自行车(Bicycle) | bicycle.xml |
你可以从 OpenCV 的额外测试数据仓库获取这些模型:
🔗 OpenCV Extra GitHub - dpm 测试数据