C++调用编译好的darknet来进行物体监测

前言

darknet是一个基于C写出的框架,yolov3速度快,定位准确率比较高,很适合落地到自己的工程中。那么如何调用编译好的darknet.so动态链接库呢?

正文

首先需要去darknet官网下载好darknet-master,然后进行编译,编译的时候最好选择GPU格式的,这样在后面监测的时候速度会非常快。

好啦,直接上代码吧~代码主要包含两个头文件和两个源文件。

imprcoess.h

#ifndef IMPROCESS_H
#define IMPROCESS_H

#include<opencv2/opencv.hpp>

void imgConvert(const cv::Mat& img, float* dst);

void imgResize(float* src, float* dst,int srcWidth,int srcHeight,int dstWidth,int dstHeight);

void resizeInner(float *src, float* dst,int srcWidth,int srcHeight,int dstWidth,int dstHeight);

#endif // IMPROCESS_H

imprcoess.cpp

#include<improcess.h>

void imgConvert(const cv::Mat& img, float* dst){
    uchar *data = img.data;
    int h = img.rows;
    int w = img.cols;
    int c = img.channels();

    for(int k= 0; k < c; ++k){
        for(int i = 0; i < h; ++i){
            for(int j = 0; j < w; ++j){
                dst[k*w*h+i*w+j] = data[(i*w + j)*c + k]/255.;
            }
        }
    }
}

void imgResize(float *src, float* dst,int srcWidth,int srcHeight,int dstWidth,int dstHeight){
    int new_w = srcWidth;
    int new_h = srcHeight;
    if (((float)dstWidth/srcWidth) < ((float)dstHeight/srcHeight)) {
        new_w = dstWidth;
        new_h = (srcHeight * dstWidth)/srcWidth;
    } else {
        new_h = dstHeight;
        new_w = (srcWidth * dstHeight)/srcHeight;
    }

    float* ImgReInner;
    size_t sizeInner=new_w*new_h*3*sizeof(float);
    ImgReInner=(float*)malloc(sizeInner);
    resizeInner(src,ImgReInner,srcWidth,srcHeight,new_w,new_h);

    for(int i=0;i<dstWidth*dstHeight*3;i++){
        dst[i]=0.5;
    }

    for(int k = 0; k < 3; ++k){
        for(int y = 0; y < new_h; ++y){
            for(int x = 0; x < new_w; ++x){
                float val = ImgReInner[k*new_w*new_h+y*new_w+x];
                dst[k*dstHeight*dstWidth 
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值