Yolov3模型框架darknet研究(八)如何将darknet在opencv4.1上编译运行通过

前言

目前darknet c框架只支持opencv2.x和3.x。如果想要运行在opencv4.x上,需要首先修改Makefile来显性指定opencv版本为4.x。具体解释见前面博文 ubuntu上opencv4和其它版本opencv如何共存

然后make clean;make 重新编译,结果遇到下面的编译错误

分析 

上面这个错误opencv版本兼容性问题。随着opencv版本的演化,c++11语法越来越占主流, 尤其到了opencv4后,基本上所有c语法都 被放弃了,都是类的概念。回到上面这个错误,IplImage是一个典型的c struct,在opencv2用的很多,opencv3用的比较少了,但支持。到了opencv4,该图像数据结构被摒弃,只能用Mat这个class来表示图像结构。 

除了某些c的数据结构类型外,还有一些宏也被修改了。比方说下面这些宏在opencv4下面编译也会出错,需要把前面的"CV_"去掉才可以。

代码 

要想darknet在opencv4下面编译并能够运行,必须要将image_opencv.cpp做如上所示修改。 我把修改后的完整代码贴在这里,供大家参考。

#ifdef OPENCV

#include "stdio.h"
#include "stdlib.h"
#include "opencv2/opencv.hpp"
#include "image.h"

using namespace cv;

extern "C" {

/*IplImage *image_to_ipl(image im)
{
    int x,y,c;
    IplImage *disp = cvCreateImage(cvSize(im.w,im.h), IPL_DEPTH_8U, im.c);
    int step = disp->widthStep;
    for(y = 0; y < im.h; ++y){
        for(x = 0; x < im.w; ++x){
            for(c= 0; c < im.c; ++c){
                float val = im.data[c*im.h*im.w + y*im.w + x];
                disp->imageData[y*step + x*im.c + c] = (unsigned char)(val*255);
            }
        }
    }
    return disp;
}

image ipl_to_image(IplImage* src)
{
    int h = src->height;
    int w = src->width;
    int c = src->nChannels;
    image im = make_image(w, h, c);
    unsigned char *data = (unsigned char *)src->imageData;
    int step = src->widthStep;
    int i, j, k;

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

Mat image_to_mat(image im)
{
    image copy = copy_image(im);
    constrain_image(copy);
    if(im.c == 3) rgbgr_image(copy);

	int x, y, c;
	Mat m = Mat(Size(im.w, im.h), CV_8UC(im.c));
	int step = m.step;

	for (y = 0; y < im.h; ++y) {
			for (x = 0; x < im.w; ++x) {
					for (c = 0; c < im.c; ++c) {
							float val = copy.data[c*im.h*im.w + y * im.w + x];
							m.data[y*step + x * im.c + c] = (unsigned char)(val * 255);
					}
			}
	}

/*    IplImage *ipl = image_to_ipl(copy);
    Mat m = cvarrToMat(ipl, true);
    cvReleaseImage(&ipl);*/
    free_image(copy);
    return m;
}

image mat_to_image(Mat m)
{
/*    IplImage ipl = m;
    image im = ipl_to_image(&ipl);
    rgbgr_image(im);*/
	int h = m.rows;
	int w = m.cols;
	int c = m.channels();
	image im = make_image(w, h, c);
	unsigned char *data = (unsigned char *)m.data;
	int step = m.step;
	int i, j, k;

	for (i = 0; i < h; ++i) {
			for (k = 0; k < c; ++k) {
					for (j = 0; j < w; ++j) {
							im.data[k*w*h + i * w + j] = data[i*step + j * c + k] / 255.;
					}
			}
	}
    if (im.c == 3) rgbgr_image(im);
	
    return im;
}

void *open_video_stream(const char *f, int c, int w, int h, int fps)
{
    VideoCapture *cap;
    if(f) cap = new VideoCapture(f);
    else cap = new VideoCapture(c);
    if(!cap->isOpened()) return 0;
    if(w) cap->set(CAP_PROP_FRAME_WIDTH, w);
    if(h) cap->set(CAP_PROP_FRAME_HEIGHT, w);
    if(fps) cap->set(CAP_PROP_FPS, w);
    return (void *) cap;
}

image get_image_from_stream(void *p)
{
    VideoCapture *cap = (VideoCapture *)p;
    Mat m;
    *cap >> m;
    if(m.empty()) return make_empty_image(0,0,0);
    return mat_to_image(m);
}

image load_image_cv(char *filename, int channels)
{
    int flag = -1;
    if (channels == 0) flag = -1;
    else if (channels == 1) flag = 0;
    else if (channels == 3) flag = 1;
    else {
        fprintf(stderr, "OpenCV can't force load with %d channels\n", channels);
    }
    Mat m;
    m = imread(filename, flag);
    if(!m.data){
        fprintf(stderr, "Cannot load image \"%s\"\n", filename);
        char buff[256];
        sprintf(buff, "echo %s >> bad.list", filename);
        system(buff);
        return make_image(10,10,3);
        //exit(0);
    }
    image im = mat_to_image(m);
    return im;
}

int show_image_cv(image im, const char* name, int ms)
{
    Mat m = image_to_mat(im);
    imshow(name, m);
    int c = waitKey(ms);
    if (c != -1) c = c%256;
    return c;
}

void make_window(char *name, int w, int h, int fullscreen)
{
    namedWindow(name, WINDOW_NORMAL); 
    if (fullscreen) {
        setWindowProperty(name, WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
    } else {
        resizeWindow(name, w, h);
        if(strcmp(name, "Demo") == 0) moveWindow(name, 0, 0);
    }
}

}

#endif

 

 

你可能感兴趣的:(深度学习)