diff --git a/src/algorithms/IBGS.h b/src/algorithms/IBGS.h
index d9ba04126b00d703683e11d3a056d3fd760e7fc0..bbf835fa24b9289738c76ff6b957cfbf20cedd75 100644
--- a/src/algorithms/IBGS.h
+++ b/src/algorithms/IBGS.h
@@ -13,7 +13,9 @@
 // opencv legacy includes
 #include <opencv2/imgproc/types_c.h>
 #include <opencv2/imgproc/imgproc_c.h>
+#ifndef MEX_COMPILE_FLAG
 #include <opencv2/highgui/highgui_c.h>
+#endif
 
 #include "../utils/ILoadSaveConfig.h"
 
diff --git a/src/algorithms/IMBS/IMBS.hpp b/src/algorithms/IMBS/IMBS.hpp
index 9ddc66fac1b27bdf7ec92b9ad73b8cbb677419a6..d17f05164b88d36148c04cd7a8df77f736d0a8fb 100644
--- a/src/algorithms/IMBS/IMBS.hpp
+++ b/src/algorithms/IMBS/IMBS.hpp
@@ -4,13 +4,15 @@
 #include <vector>
 
 #include <opencv2/core/core.hpp>
-#include <opencv2/highgui/highgui.hpp>
 #include <opencv2/imgproc/imgproc.hpp>
 #include <opencv2/features2d/features2d.hpp>
 // opencv legacy includes
+#ifndef MEX_COMPILE_FLAG
+#include <opencv2/highgui/highgui.hpp>
+#include <opencv2/highgui/highgui_c.h>
+#endif
 #include <opencv2/imgproc/types_c.h>
 #include <opencv2/imgproc/imgproc_c.h>
-#include <opencv2/highgui/highgui_c.h>
 
 namespace bgslibrary
 {
diff --git a/src/algorithms/LBSP/BackgroundSubtractorLBSP.cpp b/src/algorithms/LBSP/BackgroundSubtractorLBSP.cpp
index 53dc0bedfd3bd3f446a0ed37334d087f51c34976..d8444703ec30563d87a01a5bb7e786328865cc0e 100644
--- a/src/algorithms/LBSP/BackgroundSubtractorLBSP.cpp
+++ b/src/algorithms/LBSP/BackgroundSubtractorLBSP.cpp
@@ -3,7 +3,9 @@
 #include <exception>
 
 #include <opencv2/imgproc/imgproc.hpp>
+#ifndef MEX_COMPILE_FLAG
 #include <opencv2/highgui/highgui.hpp>
+#endif
 
 #include "BackgroundSubtractorLBSP.h"
 #include "DistanceUtils.h"
diff --git a/src/algorithms/LBSP/BackgroundSubtractorLBSP_.cpp b/src/algorithms/LBSP/BackgroundSubtractorLBSP_.cpp
index 4bcdfb1a85f78b334b8e66bfa4316ec089449f20..3a343d53bea68878df053a706cbdb0dc22369a6d 100644
--- a/src/algorithms/LBSP/BackgroundSubtractorLBSP_.cpp
+++ b/src/algorithms/LBSP/BackgroundSubtractorLBSP_.cpp
@@ -3,7 +3,9 @@
 #include <exception>
 
 #include <opencv2/imgproc/imgproc.hpp>
+#ifndef MEX_COMPILE_FLAG
 #include <opencv2/highgui/highgui.hpp>
+#endif
 
 #include "BackgroundSubtractorLBSP_.h"
 #include "DistanceUtils.h"
diff --git a/src/algorithms/LBSP/BackgroundSubtractorLOBSTER.cpp b/src/algorithms/LBSP/BackgroundSubtractorLOBSTER.cpp
index 330a1ac50889d908942ce98d35085b37b45e6ac6..58c81c5fae0d0c83e78d99e88ebeb88b804afce5 100644
--- a/src/algorithms/LBSP/BackgroundSubtractorLOBSTER.cpp
+++ b/src/algorithms/LBSP/BackgroundSubtractorLOBSTER.cpp
@@ -2,7 +2,9 @@
 #include <iomanip>
 
 #include <opencv2/imgproc/imgproc.hpp>
+#ifndef MEX_COMPILE_FLAG
 #include <opencv2/highgui/highgui.hpp>
+#endif
 
 #include "BackgroundSubtractorLOBSTER.h"
 #include "RandUtils.h"
diff --git a/src/algorithms/LBSP/BackgroundSubtractorPAWCS.cpp b/src/algorithms/LBSP/BackgroundSubtractorPAWCS.cpp
index acb1a8f4b522fda388fcfe4d51873dc9c989d705..19bc1e7039cbe566ab5f618a3bc02a5c419dcc30 100644
--- a/src/algorithms/LBSP/BackgroundSubtractorPAWCS.cpp
+++ b/src/algorithms/LBSP/BackgroundSubtractorPAWCS.cpp
@@ -2,7 +2,9 @@
 #include <iomanip>
 
 #include <opencv2/imgproc/imgproc.hpp>
+#ifndef MEX_COMPILE_FLAG
 #include <opencv2/highgui/highgui.hpp>
+#endif
 
 #include "BackgroundSubtractorPAWCS.h"
 #include "RandUtils.h"
diff --git a/src/algorithms/LBSP/BackgroundSubtractorSuBSENSE.cpp b/src/algorithms/LBSP/BackgroundSubtractorSuBSENSE.cpp
index 9278354473de81339c23aa4c9d5259c0850fba39..31cd8dfdeb0fdbc309f9d0c3b7f2aa76cb3fb18e 100644
--- a/src/algorithms/LBSP/BackgroundSubtractorSuBSENSE.cpp
+++ b/src/algorithms/LBSP/BackgroundSubtractorSuBSENSE.cpp
@@ -2,7 +2,9 @@
 #include <iomanip>
 
 #include <opencv2/imgproc/imgproc.hpp>
+#ifndef MEX_COMPILE_FLAG
 #include <opencv2/highgui/highgui.hpp>
+#endif
 
 #include "BackgroundSubtractorSuBSENSE.h"
 #include "RandUtils.h"
diff --git a/src/tools/PixelUtils.h b/src/tools/PixelUtils.h
index fd0842015b4da05292fe243749f49164466e4ee0..1522562de236c3bcbf2060aea1b81fcf4c09a2d7 100644
--- a/src/tools/PixelUtils.h
+++ b/src/tools/PixelUtils.h
@@ -7,7 +7,9 @@
 //#include <opencv2/highgui/highgui_c.h>
 #include <opencv2/imgproc/types_c.h>
 #include <opencv2/imgproc/imgproc_c.h>
+#ifndef MEX_COMPILE_FLAG
 #include <opencv2/highgui/highgui_c.h>
+#endif
 
 namespace bgslibrary
 {