https://patch-diff.githubusercontent.com/raw/BVLC/caffe/pull/6693.diff
--- a/src/caffe/layers/window_data_layer.cpp
+++ b/src/caffe/layers/window_data_layer.cpp
@@ -10,6 +10,9 @@
 
 #include "opencv2/core/core.hpp"
 #include "opencv2/highgui/highgui.hpp"
+#if CV_MAJOR_VERSION >= 4
+#include "opencv2/imgcodecs/legacy/constants_c.h"
+#endif
 #include "opencv2/imgproc/imgproc.hpp"
 
 #include "caffe/data_transformer.hpp"
--- a/src/caffe/test/test_io.cpp
+++ b/src/caffe/test/test_io.cpp
@@ -2,6 +2,9 @@
 #include <opencv2/core/core.hpp>
 #include <opencv2/highgui/highgui.hpp>
 #include <opencv2/highgui/highgui_c.h>
+#if CV_MAJOR_VERSION >= 4
+#include <opencv2/imgcodecs/legacy/constants_c.h>
+#endif
 #include <opencv2/imgproc/imgproc.hpp>
 
 #include <string>
--- a/src/caffe/util/io.cpp
+++ b/src/caffe/util/io.cpp
@@ -6,6 +6,9 @@
 #include <opencv2/core/core.hpp>
 #include <opencv2/highgui/highgui.hpp>
 #include <opencv2/highgui/highgui_c.h>
+#if CV_MAJOR_VERSION >= 4
+#include <opencv2/imgcodecs/legacy/constants_c.h>
+#endif
 #include <opencv2/imgproc/imgproc.hpp>
 #endif  // USE_OPENCV
 #include <stdint.h>
