C++实现DPM/LatentSVM 完整代码下载 --- 第三篇

这是我的DPM代码的第三篇博文,最初发表在http://blog.csdn.net/j56754gefge/article/details/40708679,均是我原创文章,他人转载请注明出处!

 

C++实现DPM/LatentSVM,完整代码下载!BUG持续改进!关注DPM实现的网友可以持续关注下该文章!使用代码发现BUG的网友请不吝及时向我反馈,我把BUG改进后再在这里更新出来,让更多的人受益!

20191123:由于之前上传的代码到了CSDN资源,然后下载积分被系统搞成了很高,为了不影响大家下载使用,我特地把资源重新上传到了百度云盘了:https://pan.baidu.com/s/1Bs7R_NPWAZkzXn4OH6mMXQ

-------------------------------------------------------

 

此前我已经将Cascade DPM的C++代码以及Original DPM的C++编译库发布出来,很多人给我发邮件,希望能得到Original DPM的C++代码,现在我就把这个源代码发布出来。

发布内容:Original DPM,我也称之为完整版DPM,是Pedro. Felzenswalb发明的”deformable part based models for object detection”的程序实现,原始程序和参考文献已经在我另一篇博文里列出了,这里不再赘述。我的工作是将原始的Matlab代码voc-release5中检测部分用

  • 13
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 61
    评论
DPM (Deformable Part-based Models) 算法是一种目标检测算法,它采用了分层的结构来描述目标,并通过学习模型参数来实现目标的快速检测。 以下是一个简单的 C++ 实现 DPM 算法的示例代码: ```cpp #include <opencv2/opencv.hpp> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream> #include <fstream> #include <vector> using namespace cv; using namespace std; struct Feature { Mat image; int x, y, w, h; }; struct Model { vector<Feature> features; float score; }; vector<Model> models; void load_model(string filename) { ifstream file(filename); if (!file.is_open()) { cerr << "Failed to open file: " << filename << endl; exit(1); } string line; while (getline(file, line)) { Model model; model.score = stof(line); while (getline(file, line)) { if (line.empty()) break; Feature feature; stringstream ss(line); ss >> feature.x >> feature.y >> feature.w >> feature.h; Mat image(feature.h, feature.w, CV_8UC1); for (int y = 0; y < feature.h; y++) { for (int x = 0; x < feature.w; x++) { int pixel; ss >> pixel; image.at<uchar>(y, x) = pixel; } } feature.image = image; model.features.push_back(feature); } models.push_back(model); } } float match_feature(const Feature& feature, const Mat& image, int x, int y) { float sum = 0; for (int j = 0; j < feature.h; j++) { for (int i = 0; i < feature.w; i++) { int pixel = image.at<uchar>(y + j, x + i); sum += pixel * feature.image.at<uchar>(j, i); } } return sum; } float match_model(const Model& model, const Mat& image, int x, int y) { float sum = model.score; for (const auto& feature : model.features) { sum += match_feature(feature, image, x + feature.x, y + feature.y); } return sum; } vector<Rect> detect_objects(const Mat& image) { vector<Rect> objects; for (int y = 0; y < image.rows - 64; y += 8) { for (int x = 0; x < image.cols - 64; x += 8) { float max_score = -INFINITY; for (const auto& model : models) { float score = match_model(model, image, x, y); if (score > max_score) { max_score = score; } } if (max_score > 0.5) { objects.push_back(Rect(x, y, 64, 64)); } } } return objects; } int main() { load_model("model.txt"); Mat image = imread("image.jpg", IMREAD_GRAYSCALE); vector<Rect> objects = detect_objects(image); Mat result; cvtColor(image, result, COLOR_GRAY2BGR); for (const auto& object : objects) { rectangle(result, object, Scalar(0, 0, 255), 2); } imshow("Result", result); waitKey(0); return 0; } ``` 上述代码中,我们首先定义了 `Feature` 和 `Model` 结构体,分别表示特征和模型。然后,我们通过 `load_model` 函数从文件中加载模型,并将其存储在 `models` 变量中。 接着,我们实现了 `match_feature` 和 `match_model` 函数,用于计算特征和模型之间的相似度得分。最后,我们实现了 `detect_objects` 函数,用于在图像中检测目标。 在 `main` 函数中,我们首先调用 `load_model` 函数加载模型,然后读取待检测的图像,并调用 `detect_objects` 函数来检测目标。最后,我们在图像上绘制检测结果,并显示出来。 需要注意的是,上述代码只是一个简单的示例,实际应用中还需要进行许多优化和改进,以提高检测的准确率和效率。
评论 61
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值