当前位置:   article > 正文

C++应用中调用YOLOv3(darknet)进行目标检测_darknet 的c++版本

darknet 的c++版本

YOLOv3论文:https://pjreddie.com/media/files/papers/YOLOv3.pdf

官网和代码:https://pjreddie.com/darknet/

yolo属于one-stage(检测一步到位),兼顾准确率和速度,特别是最近的v3版本提高了小目标的检测率,是移动端目标检测的热门算法。关于YOLO原理的介绍网上有很多资料请自行百度,本文主要介绍如何在自己的cpp中调用yolov3进行目标检测。

yolo采用自定义的image格式进行图像读取和处理,而一般我们工程中使用较多的是OpenCV或者指向图像数据的指针,因此此处先对图像转换和缩放操作进行修改,代码如下:

  1. #ifndef IMPROCESS_H
  2. #define IMPROCESS_H
  3. #include<opencv2/opencv.hpp>
  4. void imgConvert(const cv::Mat& img, float* dst);
  5. void imgResize(float* src, float* dst,int srcWidth,int srcHeight,int dstWidth,int dstHeight);
  6. void resizeInner(float *src, float* dst,int srcWidth,int srcHeight,int dstWidth,int dstHeight);
  7. #endif // IMPROCESS_H
  1. #include<improcess.h>
  2. void imgConvert(const cv::Mat& img, float* dst){
  3. uchar *data = img.data;
  4. int h = img.rows;
  5. int w = img.cols;
  6. int c = img.channels();
  7. for(int k= 0; k < c; ++k){
  8. for(int i = 0; i < h; ++i){
  9. for(int j = 0; j < w; ++j){
  10. dst[k*w*h+i*w+j] = data[(i*w + j)*c + k]/255.;
  11. }
  12. }
  13. }
  14. }
  15. void imgResize(float *src, float* dst,int srcWidth,int srcHeight,int dstWidth,int dstHeight){
  16. int new_w = srcWidth;
  17. int new_h = srcHeight;
  18. if (((float)dstWidth/srcWidth) < ((float)dstHeight/srcHeight)) {
  19. new_w = dstWidth;
  20. new_h = (srcHeight * dstWidth)/srcWidth;
  21. } else {
  22. new_h = dstHeight;
  23. new_w = (srcWidth * dstHeight)/srcHeight;
  24. }
  25. float* ImgReInner;
  26. size_t sizeInner=new_w*new_h*3*sizeof(float);
  27. ImgReInner=
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/736295
推荐阅读
相关标签
  

闽ICP备14008679号