Skip to content

Instantly share code, notes, and snippets.

@tiagoshibata
Created December 29, 2018 01:26
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tiagoshibata/f322466e8b31c14a4b98d53bf74e4f6c to your computer and use it in GitHub Desktop.
Save tiagoshibata/f322466e8b31c14a4b98d53bf74e4f6c to your computer and use it in GitHub Desktop.
commit 6fac225de0575b18550f9421196f90a2279c44af
Author: Tiago Koji Castro Shibata <tiago.shibata@gmail.com>
Date: Fri Dec 28 23:13:14 2018 -0200
Fix compilation with latest OpenCV
C APIs were removed and must be replaced with C++ calls
diff --git a/Makefile b/Makefile
index 63e15e6..c148d4b 100644
--- a/Makefile
+++ b/Makefile
@@ -42,8 +42,8 @@ CFLAGS+=$(OPTS)
ifeq ($(OPENCV), 1)
COMMON+= -DOPENCV
CFLAGS+= -DOPENCV
-LDFLAGS+= `pkg-config --libs opencv` -lstdc++
-COMMON+= `pkg-config --cflags opencv`
+LDFLAGS+= `pkg-config --libs opencv 2> /dev/null || pkg-config --libs opencv4` -lstdc++
+COMMON+= `pkg-config --cflags opencv 2> /dev/null || pkg-config --cflags opencv4`
endif
ifeq ($(GPU), 1)
diff --git a/src/image_opencv.cpp b/src/image_opencv.cpp
index 7511280..0109fad 100644
--- a/src/image_opencv.cpp
+++ b/src/image_opencv.cpp
@@ -9,30 +9,36 @@ using namespace cv;
extern "C" {
-IplImage *image_to_ipl(image im)
+Mat image_to_mat(image im)
{
+ assert(im.c == 3 || im.c == 1);
int x,y,c;
- IplImage *disp = cvCreateImage(cvSize(im.w,im.h), IPL_DEPTH_8U, im.c);
- int step = disp->widthStep;
+ image copy = copy_image(im);
+ constrain_image(copy);
+ if(im.c == 3) rgbgr_image(copy);
+ unsigned char *data = (unsigned char *)malloc(im.w * im.h * im.c);
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);
+ float val = copy.data[c*im.h*im.w + y*im.w + x];
+ data[y*im.w*im.c + x*im.c + c] = (unsigned char)(val*255);
}
}
}
- return disp;
+ Mat m(im.h, im.w, CV_MAKETYPE(CV_8U, im.c), data);
+ free_image(copy);
+ free(data);
+ return m;
}
-image ipl_to_image(IplImage* src)
+image mat_to_image(Mat m)
{
- int h = src->height;
- int w = src->width;
- int c = src->nChannels;
+ int h = m.rows;
+ int w = m.cols;
+ int c = m.channels();
image im = make_image(w, h, c);
- unsigned char *data = (unsigned char *)src->imageData;
- int step = src->widthStep;
+ unsigned char *data = (uint8_t*)m.data;
+ int step = m.step;
int i, j, k;
for(i = 0; i < h; ++i){
@@ -42,26 +48,6 @@ image ipl_to_image(IplImage* src)
}
}
}
- return im;
-}
-
-Mat image_to_mat(image im)
-{
- image copy = copy_image(im);
- constrain_image(copy);
- if(im.c == 3) rgbgr_image(copy);
-
- 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);
return im;
}
@@ -72,9 +58,9 @@ void *open_video_stream(const char *f, int c, int w, int h, int fps)
if(f) cap = new VideoCapture(f);
else cap = new VideoCapture(c);
if(!cap->isOpened()) return 0;
- if(w) cap->set(CV_CAP_PROP_FRAME_WIDTH, w);
- if(h) cap->set(CV_CAP_PROP_FRAME_HEIGHT, w);
- if(fps) cap->set(CV_CAP_PROP_FPS, w);
+ 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;
}
@@ -121,9 +107,9 @@ int show_image_cv(image im, const char* name, int ms)
void make_window(char *name, int w, int h, int fullscreen)
{
- namedWindow(name, WINDOW_NORMAL);
+ namedWindow(name, WINDOW_NORMAL);
if (fullscreen) {
- setWindowProperty(name, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
+ setWindowProperty(name, WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
} else {
resizeWindow(name, w, h);
if(strcmp(name, "Demo") == 0) moveWindow(name, 0, 0);
@Jian-Han0504
Copy link

Hello,
Thanks for your patch! However, I don't see the remaining functions, like show_image_cv. Are they all omitted?

@tiagoshibata
Copy link
Author

The patch applies cleanly on darknet's master, which has these functions.

If you need help using patches, see man patch and man git-apply: http://man7.org/linux/man-pages/man1/patch.1.html and https://git-scm.com/docs/git-apply . Using git apply /path/to/patch on your darknet repo should work.

If you need a PR, see https://github.com/pjreddie/darknet/pull/1348/files

@crmann25
Copy link

Thanks for this fix!! Applied the patch and Darknet compiled successfully.

FYI I am running Ubuntu 18.04, CUDA enabled, OpenCV 4.2 built from source w/ contrib modules.

@Choikyungho9
Copy link

How should I use this patch?

I pasted the code in darknet's makefile, but only getting an error.

Anyone can tell me how to apply the patch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment