Compilation fails on OpenCV 4.0.1 (tested on official Arch package). Some C APIs, which were deprecated for a long time, were removed from the latest OpenCV release. Darknet code using functions that work on IplImage fails to compile.
The solution is to remove references to the C API and use the C++ API (eg. cv::Mat instead of IplImage). I have a patch ready; I'll compile OpenCV 3 on my machine and check if I didn't regress anything before opening a PR.
The specific error message is:
./src/image_opencv.cpp:12:1: error: ‘IplImage’ does not name a type; did you mean ‘image’?
IplImage *image_to_ipl(image im)
^~~~~~~~
image
Edit: Fix is at #1348 .
I can confirm this using the official Arch package. Compilation succeeds when OPENCV=0, but enabling opencv support results in pkgconfig complaining that it can't find opencv.pc in /usr/lib/pkgconfig/. Symlinking to opencv4.pc in the pkgconfig directory or renaming opencv to opencv4 in the relevant part of the Makefile allows the build to continue, but it fails later with the 'IplImage does not name a type` message.
@3th3r3um did you check the fix at #1348?
I applied your patch, it lets the build complete. I ran into some issues along the way with a few extra dependencies that I had to install with pacman, but as far as I can tell the patch fixed what it was intended to fix. Now I can now get the YOLOv3 detector to run with ./darknet detect cfg/yolov3-tiny.cfg yolo.weights data/dog.jpg, although it doesn't seem to find any bounding boxes. I've compiled with gpu, cudnn, and opencv but no luck. I ended up just moving onto using a python implementation that seems to work well.
I think the better "fix" for this would to just make an AUR package for darknet. Especially seeing as there are other dependencies that need to be installed in order to build everything.
I just run into this thread trying to compile darknet with opencv on Ubuntu 16.04.
I have installed opencv 4, and I had used a pkgconfig file from opencv 2.x.x.
That was giving me some grief after patching image_opencv.cpp and adding -std=c++11 to COMMON.
It had a couple of old libraries and missed a couple new. Just in case it helps someone, here is what worked for me:
$ cat /usr/local/lib/pkgconfig/opencv.pc
prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: opencv
Description: The opencv library
Version: 4.x.x
Cflags: -I${includedir}/opencv4 -I${includedir}/opencv4/opencv2
Libs: -L${libdir} -lopencv_calib3d -lopencv_imgproc -lopencv_core -lopencv_ml -lopencv_features2d -lopencv_objdetect -lopencv_flann -lopencv_video -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs
exactly the same error, opencv 4.0.1
./src/image_opencv.cpp:12:1: error: ‘IplImage’ does not name a type
IplImage *image_to_ipl(image im)
^~~~
@Jacob12138xieyuan did you try the fix at #1348?
@Jacob12138xieyuan did you try the fix at #1348?
i didn't get it. what changes should i make?
Did you try the commits at that pull request? You can get it at my branch (tiagoshibata/darknet) or see the diff here
The solution is to remove the IplImage conversion functions and just convert image directly to and from Mat.
`
Mat image_to_mat(image im)
{
image copy = copy_image(im);
constrain_image(copy);
if(im.c == 3) rgbgr_image(copy);
Mat m(cv::Size(im.w,im.h), CV_8UC(im.c));
int x,y,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 = im.data[c*im.h*im.w + y*im.w + x];
m.data[y*step + x*im.c + c] = (unsigned char)(val*255);
}
}
}
free_image(copy);
return m;
}
image mat_to_image(Mat m)
{
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.;
}
}
}
rgbgr_image(im);
return im;
}
`
您是否在拉取请求时尝试了提交?你可以在我的分店(tiagoshibata / darknet)获得它或在这里看到差异
I use your repo... and it got a new mistake....
many under like
/usr/lib/gcc/x86_64-pc-linux-gnu/7.4.1/../../../../lib/libopencv_viz.so: undefined reference tovtkOStreamWrapper::operator<<(char const)'`
Final...
collect2: error: ld returned 1 exit status
When i use pjreddie/darknet , it stills tell me ‘IplImage’ does not name a type
@live-xy Which distro are you using? Did you compile OpenCV yourself?
@tiagoshibata I got it . now it has been installed..
seems my vtk version is too high
Hi. How to apply the patch? Please guide me. Thank you!
See pull request #1384. You can clone my fork (https://github.com/tiagoshibata/darknet) and build it to get the fix for OpenCV 4.
Hi @tiagoshibata Thank you! I will do.
The solution is to remove the IplImage conversion functions and just convert image directly to and from Mat.
`
Mat image_to_mat(image im)
{
image copy = copy_image(im);
constrain_image(copy);
if(im.c == 3) rgbgr_image(copy);Mat m(cv::Size(im.w,im.h), CV_8UC(im.c)); int x,y,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 = im.data[c*im.h*im.w + y*im.w + x]; m.data[y*step + x*im.c + c] = (unsigned char)(val*255); } } } free_image(copy); return m;}
image mat_to_image(Mat m)
{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.; } } } rgbgr_image(im); return im;}
`
Works beautifully!
Did you try the commits at that pull request? You can get it at my branch (tiagoshibata/darknet) or see the diff here
it solved the problem for now
The solution is to remove the IplImage conversion functions and just convert image directly to and from Mat.
`
Mat image_to_mat(image im)
{
image copy = copy_image(im);
constrain_image(copy);
if(im.c == 3) rgbgr_image(copy);Mat m(cv::Size(im.w,im.h), CV_8UC(im.c)); int x,y,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 = im.data[c*im.h*im.w + y*im.w + x]; m.data[y*step + x*im.c + c] = (unsigned char)(val*255); } } } free_image(copy); return m;}
image mat_to_image(Mat m)
{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.; } } } rgbgr_image(im); return im;}
`
Excellent solution!!! works like a charm :+1:
Using OpenCV 4 in Ubuntu 18.04.5 LTS, just needed to change the MakeFile with correct version and update the functions in file image_opencv.cpp
_Makefile_
ifeq ($(OPENCV), 1)
COMMON+= -DOPENCV
CFLAGS+= -DOPENCV
LDFLAGS+= pkg-config --libs opencv4 -lstdc++
COMMON+= pkg-config --cflags opencv4
endif
Most helpful comment
Did you try the commits at that pull request? You can get it at my branch (tiagoshibata/darknet) or see the diff here