You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by zh...@apache.org on 2019/03/07 02:10:27 UTC

[incubator-mxnet] branch master updated: compatibility with opencv4 (#14313)

This is an automated email from the ASF dual-hosted git repository.

zhasheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/master by this push:
     new 7b8e3a9  compatibility with opencv4 (#14313)
7b8e3a9 is described below

commit 7b8e3a9685a668d216f1c594b34ee614373f4216
Author: JackieWu <wk...@live.cn>
AuthorDate: Thu Mar 7 10:10:02 2019 +0800

    compatibility with opencv4 (#14313)
    
    * compatibility with opencv4
    
    * update makefile
    
    * update Makefile
    
    * fix Makefile
    
    * fix lib path
    
    * update make.mk
    
    * add -lopencv_highgui
    
    * retrigger CI
    
    * update makefile
    
    * remove libs-L
    
    * Fix OpenCV linking order
    
    * try to fix the bug of linking static libraries
---
 Makefile                          | 30 +++++++++++++++++---
 make/config.mk                    |  4 +++
 make/crosscompile.jetson.mk       |  4 +++
 make/maven/maven_darwin_mkl.mk    |  4 +++
 make/maven/maven_linux_cu90mkl.mk |  4 +++
 make/maven/maven_linux_cu92mkl.mk |  4 +++
 make/maven/maven_linux_mkl.mk     |  4 +++
 make/osx.mk                       |  4 +++
 make/pip/pip_darwin_cpu.mk        |  4 +++
 make/pip/pip_darwin_mkl.mk        |  4 +++
 make/pip/pip_linux_cpu.mk         |  4 +++
 make/pip/pip_linux_cu100.mk       |  4 +++
 make/pip/pip_linux_cu100mkl.mk    |  4 +++
 make/pip/pip_linux_cu75.mk        |  4 +++
 make/pip/pip_linux_cu75mkl.mk     |  4 +++
 make/pip/pip_linux_cu80.mk        |  4 +++
 make/pip/pip_linux_cu80mkl.mk     |  4 +++
 make/pip/pip_linux_cu90.mk        |  4 +++
 make/pip/pip_linux_cu90mkl.mk     |  4 +++
 make/pip/pip_linux_cu91.mk        |  4 +++
 make/pip/pip_linux_cu91mkl.mk     |  4 +++
 make/pip/pip_linux_cu92.mk        |  4 +++
 make/pip/pip_linux_cu92mkl.mk     |  4 +++
 make/pip/pip_linux_mkl.mk         |  4 +++
 make/readthedocs.mk               |  4 +++
 src/io/image_aug_default.cc       |  1 +
 src/io/image_det_aug_default.cc   |  1 +
 src/io/image_io.cc                |  1 +
 src/io/opencv_compatibility.h     | 59 +++++++++++++++++++++++++++++++++++++++
 tools/im2rec.cc                   |  1 +
 30 files changed, 185 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile
index 5c5e77f..29443eb 100644
--- a/Makefile
+++ b/Makefile
@@ -129,7 +129,7 @@ ifdef CAFFE_PATH
 endif
 
 ifndef LINT_LANG
-	LINT_LANG="all"
+	LINT_LANG = "all"
 endif
 
 ifeq ($(USE_MKLDNN), 1)
@@ -146,11 +146,33 @@ endif
 
 # setup opencv
 ifeq ($(USE_OPENCV), 1)
-	CFLAGS += -DMXNET_USE_OPENCV=1 $(shell pkg-config --cflags opencv)
-	LDFLAGS += $(filter-out -lopencv_ts, $(shell pkg-config --libs opencv))
+	CFLAGS += -DMXNET_USE_OPENCV=1
+	ifneq ($(USE_OPENCV_INC_PATH), NONE)
+		CFLAGS += -I$(USE_OPENCV_INC_PATH)/include
+		ifeq ($(USE_OPENCV_LIB_PATH), NONE)
+$(error Please add the path of OpenCV shared library path into `USE_OPENCV_LIB_PATH`, when `USE_OPENCV_INC_PATH` is not NONE)
+		endif
+		LDFLAGS += -L$(USE_OPENCV_LIB_PATH)
+		ifneq ($(wildcard $(USE_OPENCV_LIB_PATH)/libopencv_imgcodecs.*),)
+			LDFLAGS += -lopencv_imgcodecs
+		endif
+		ifneq ($(wildcard $(USE_OPENCV_LIB_PATH)/libopencv_highgui.*),)
+			LDFLAGS += -lopencv_highgui
+		endif
+	else
+		ifeq ("$(shell pkg-config --exists opencv4; echo $$?)", "0")
+			OPENCV_LIB = opencv4
+		else
+			OPENCV_LIB = opencv
+		endif
+		CFLAGS += $(shell pkg-config --cflags $(OPENCV_LIB))
+		LDFLAGS += $(shell pkg-config --libs-only-L $(OPENCV_LIB))
+		LDFLAGS += $(filter -lopencv_imgcodecs -lopencv_highgui, $(shell pkg-config --libs-only-l $(OPENCV_LIB)))
+	endif
+	LDFLAGS += -lopencv_imgproc -lopencv_core
 	BIN += bin/im2rec
 else
-	CFLAGS+= -DMXNET_USE_OPENCV=0
+	CFLAGS += -DMXNET_USE_OPENCV=0
 endif
 
 ifeq ($(USE_OPENMP), 1)
diff --git a/make/config.mk b/make/config.mk
index 8a1aa2c..f9ac4cf 100644
--- a/make/config.mk
+++ b/make/config.mk
@@ -89,6 +89,10 @@ USE_NCCL_PATH = NONE
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 #whether use libjpeg-turbo for image decode without OpenCV wrapper
 USE_LIBJPEG_TURBO = 0
diff --git a/make/crosscompile.jetson.mk b/make/crosscompile.jetson.mk
index 171f846..3db2b98 100644
--- a/make/crosscompile.jetson.mk
+++ b/make/crosscompile.jetson.mk
@@ -89,6 +89,10 @@ USE_NCCL_PATH = NONE
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 0
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 #whether use libjpeg-turbo for image decode without OpenCV wrapper
 USE_LIBJPEG_TURBO = 0
diff --git a/make/maven/maven_darwin_mkl.mk b/make/maven/maven_darwin_mkl.mk
index f5b77ae..f68f156 100644
--- a/make/maven/maven_darwin_mkl.mk
+++ b/make/maven/maven_darwin_mkl.mk
@@ -58,6 +58,10 @@ USE_BLAS=apple
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 0
diff --git a/make/maven/maven_linux_cu90mkl.mk b/make/maven/maven_linux_cu90mkl.mk
index 661f444..3d3b2c4 100644
--- a/make/maven/maven_linux_cu90mkl.mk
+++ b/make/maven/maven_linux_cu90mkl.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 1
diff --git a/make/maven/maven_linux_cu92mkl.mk b/make/maven/maven_linux_cu92mkl.mk
index ecd2527..ab93801 100644
--- a/make/maven/maven_linux_cu92mkl.mk
+++ b/make/maven/maven_linux_cu92mkl.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 1
diff --git a/make/maven/maven_linux_mkl.mk b/make/maven/maven_linux_mkl.mk
index 6cb9f32..dfe5065 100644
--- a/make/maven/maven_linux_mkl.mk
+++ b/make/maven/maven_linux_mkl.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 0
diff --git a/make/osx.mk b/make/osx.mk
index 3e2e592..7e32d81 100644
--- a/make/osx.mk
+++ b/make/osx.mk
@@ -75,6 +75,10 @@ USE_CUDNN = 0
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # use openmp for parallelization
 # apple-clang by default does not have openmp built-in
diff --git a/make/pip/pip_darwin_cpu.mk b/make/pip/pip_darwin_cpu.mk
index 87a9679..2c80c42 100644
--- a/make/pip/pip_darwin_cpu.mk
+++ b/make/pip/pip_darwin_cpu.mk
@@ -58,6 +58,10 @@ USE_BLAS=apple
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 0
diff --git a/make/pip/pip_darwin_mkl.mk b/make/pip/pip_darwin_mkl.mk
index 339c95f..2fac512 100644
--- a/make/pip/pip_darwin_mkl.mk
+++ b/make/pip/pip_darwin_mkl.mk
@@ -58,6 +58,10 @@ USE_BLAS=apple
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 0
diff --git a/make/pip/pip_linux_cpu.mk b/make/pip/pip_linux_cpu.mk
index d680f6d..9ed88bb 100644
--- a/make/pip/pip_linux_cpu.mk
+++ b/make/pip/pip_linux_cpu.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 0
diff --git a/make/pip/pip_linux_cu100.mk b/make/pip/pip_linux_cu100.mk
index 0f3f84a..f3bea65 100644
--- a/make/pip/pip_linux_cu100.mk
+++ b/make/pip/pip_linux_cu100.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 1
diff --git a/make/pip/pip_linux_cu100mkl.mk b/make/pip/pip_linux_cu100mkl.mk
index b4792e3..4dfcb21 100644
--- a/make/pip/pip_linux_cu100mkl.mk
+++ b/make/pip/pip_linux_cu100mkl.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 1
diff --git a/make/pip/pip_linux_cu75.mk b/make/pip/pip_linux_cu75.mk
index ff05ec8..c686c20 100644
--- a/make/pip/pip_linux_cu75.mk
+++ b/make/pip/pip_linux_cu75.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 1
diff --git a/make/pip/pip_linux_cu75mkl.mk b/make/pip/pip_linux_cu75mkl.mk
index a825920..ec87676 100644
--- a/make/pip/pip_linux_cu75mkl.mk
+++ b/make/pip/pip_linux_cu75mkl.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 1
diff --git a/make/pip/pip_linux_cu80.mk b/make/pip/pip_linux_cu80.mk
index 7abf09f..72a04de 100644
--- a/make/pip/pip_linux_cu80.mk
+++ b/make/pip/pip_linux_cu80.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 1
diff --git a/make/pip/pip_linux_cu80mkl.mk b/make/pip/pip_linux_cu80mkl.mk
index be42cca..1bd2951 100644
--- a/make/pip/pip_linux_cu80mkl.mk
+++ b/make/pip/pip_linux_cu80mkl.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 1
diff --git a/make/pip/pip_linux_cu90.mk b/make/pip/pip_linux_cu90.mk
index 12bc774..b831d65 100644
--- a/make/pip/pip_linux_cu90.mk
+++ b/make/pip/pip_linux_cu90.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 1
diff --git a/make/pip/pip_linux_cu90mkl.mk b/make/pip/pip_linux_cu90mkl.mk
index 770f292..8ab1307 100644
--- a/make/pip/pip_linux_cu90mkl.mk
+++ b/make/pip/pip_linux_cu90mkl.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 1
diff --git a/make/pip/pip_linux_cu91.mk b/make/pip/pip_linux_cu91.mk
index 9e399f9..cb66394 100644
--- a/make/pip/pip_linux_cu91.mk
+++ b/make/pip/pip_linux_cu91.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 1
diff --git a/make/pip/pip_linux_cu91mkl.mk b/make/pip/pip_linux_cu91mkl.mk
index 80ef690..a948745 100644
--- a/make/pip/pip_linux_cu91mkl.mk
+++ b/make/pip/pip_linux_cu91mkl.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 1
diff --git a/make/pip/pip_linux_cu92.mk b/make/pip/pip_linux_cu92.mk
index b0731ad..8796a3b 100644
--- a/make/pip/pip_linux_cu92.mk
+++ b/make/pip/pip_linux_cu92.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 1
diff --git a/make/pip/pip_linux_cu92mkl.mk b/make/pip/pip_linux_cu92mkl.mk
index 768fcc9..b9766ec 100644
--- a/make/pip/pip_linux_cu92mkl.mk
+++ b/make/pip/pip_linux_cu92mkl.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 1
diff --git a/make/pip/pip_linux_mkl.mk b/make/pip/pip_linux_mkl.mk
index ea11061..95146ee 100644
--- a/make/pip/pip_linux_mkl.mk
+++ b/make/pip/pip_linux_mkl.mk
@@ -58,6 +58,10 @@ USE_BLAS=openblas
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 1
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDA during compile
 USE_CUDA = 0
diff --git a/make/readthedocs.mk b/make/readthedocs.mk
index 0a45e6f..b33dd3c 100644
--- a/make/readthedocs.mk
+++ b/make/readthedocs.mk
@@ -36,6 +36,10 @@ USE_CUDA_PATH = NONE
 # you can disable it, however, you will not able to use
 # imbin iterator
 USE_OPENCV = 0
+# Add OpenCV include path, in which the directory `opencv2` exists
+USE_OPENCV_INC_PATH = NONE
+# Add OpenCV shared library path, in which the shared library exists
+USE_OPENCV_LIB_PATH = NONE
 
 # whether use CUDNN R3 library
 USE_CUDNN = 0
diff --git a/src/io/image_aug_default.cc b/src/io/image_aug_default.cc
index 5fb0b0f..01c0a7a 100644
--- a/src/io/image_aug_default.cc
+++ b/src/io/image_aug_default.cc
@@ -32,6 +32,7 @@
 #include "../common/utils.h"
 
 #if MXNET_USE_OPENCV
+#include "./opencv_compatibility.h"
 // Registers
 namespace dmlc {
 DMLC_REGISTRY_ENABLE(::mxnet::io::ImageAugmenterReg);
diff --git a/src/io/image_det_aug_default.cc b/src/io/image_det_aug_default.cc
index 91711bf..74e51b5 100644
--- a/src/io/image_det_aug_default.cc
+++ b/src/io/image_det_aug_default.cc
@@ -203,6 +203,7 @@ std::vector<dmlc::ParamFieldInfo> ListDefaultDetAugParams() {
 }
 
 #if MXNET_USE_OPENCV
+#include "./opencv_compatibility.h"
 using Rect = cv::Rect_<float>;
 
 #ifdef _MSC_VER
diff --git a/src/io/image_io.cc b/src/io/image_io.cc
index ab55c1a..2196983 100644
--- a/src/io/image_io.cc
+++ b/src/io/image_io.cc
@@ -41,6 +41,7 @@
 
 #if MXNET_USE_OPENCV
   #include <opencv2/opencv.hpp>
+  #include "./opencv_compatibility.h"
 #endif  // MXNET_USE_OPENCV
 
 namespace mxnet {
diff --git a/src/io/opencv_compatibility.h b/src/io/opencv_compatibility.h
new file mode 100644
index 0000000..7f42328
--- /dev/null
+++ b/src/io/opencv_compatibility.h
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ *  Copyright (c) 2019 by Contributors
+ * \file opencv_compatibility.h
+ * \brief To be compatible with multiple versions of opencv
+ */
+#ifndef MXNET_IO_OPENCV_COMPATIBILITY_H_
+#define MXNET_IO_OPENCV_COMPATIBILITY_H_
+
+#if MXNET_USE_OPENCV
+#include <opencv2/core/version.hpp>
+
+#if CV_VERSION_MAJOR >= 4
+#include <opencv2/opencv.hpp>
+#define CV_RGB2GRAY cv::COLOR_RGB2GRAY
+#define CV_BGR2GRAY cv::COLOR_BGR2GRAY
+
+#define CV_GRAY2RGB cv::COLOR_GRAY2RGB
+#define CV_GRAY2BGR cv::COLOR_GRAY2BGR
+
+#define CV_RGB2HLS cv::COLOR_RGB2HLS
+#define CV_BGR2HLS cv::COLOR_BGR2HLS
+
+#define CV_HLS2RGB cv::COLOR_HLS2RGB
+#define CV_HLS2BGR cv::COLOR_HLS2BGR
+
+#define CV_RGB2BGR cv::COLOR_RGB2BGR
+#define CV_BGR2RGB cv::COLOR_BGR2RGB
+
+#define CV_INTER_LINEAR cv::INTER_LINEAR
+#define CV_INTER_NEAREST cv::INTER_NEAREST
+
+#define CV_LOAD_IMAGE_COLOR cv::IMREAD_COLOR
+#define CV_IMWRITE_PNG_COMPRESSION cv::IMWRITE_PNG_COMPRESSION
+#define CV_IMWRITE_JPEG_QUALITY cv::IMWRITE_JPEG_QUALITY
+
+#endif  // CV_VERSION_MAJOR >= 4
+
+#endif  // MXNET_USE_OPENCV
+
+#endif  // MXNET_IO_OPENCV_COMPATIBILITY_H_
diff --git a/tools/im2rec.cc b/tools/im2rec.cc
index 915b780..989b314 100644
--- a/tools/im2rec.cc
+++ b/tools/im2rec.cc
@@ -39,6 +39,7 @@
 #include <dmlc/logging.h>
 #include <dmlc/recordio.h>
 #include <opencv2/opencv.hpp>
+#include "../src/io/opencv_compatibility.h"
 #include "../src/io/image_recordio.h"
 #include <random>
 /*!