Compile OpenCV with GStreamer support in Windows

cropped-ocv_gst2.png

If you are trying to build an OpenCV application which uses GStreamer for video-processing, then you need to compile OpenCV from source with GStreamer support. This is easier in Linux, where you can install GStreamer libraries and plugins, and compile OpenCV with with_gstreamer option enabled to get your job done. But in windows, this becomes trickier as the with_gstreamer option is permanently off in OpenCV CMakeFile.

So, to successfully compile OpenCV with GStreamer support on Windows platform, you need to edit some cmakefiles so that CMake can find the GStreamer libraries necessary to generate the proper solution for compilation. Here it goes……

  • Download pkg-config-lite-0.28-1_bin-win32 (Link)
  • Download GStreamer Developer package (Link)

Add the following system paths

  • C:\path-to-pkg-config\bin
  • C:\gstreamer\1.0\x86\lib\gstreamer-1.0 (for gstreamer 1.0)
  • C:\gstreamer\1.0\x86\lib (for gstreamer 1.0)

Add Environment Variable

  • PKG_CONFIG_PATH C:\Gstreamer\1.0\x86\lib\pkgconfig

Edit opencv\sources\CMakeLists.txt

1. Find this line and add WIN32 in gstreamer support as shown below:

OCV_OPTION(WITH_GSTREAMER "Include Gstreamer support" ON IF (WIN32 OR UNIX AND NOT ANDROID) )

2. Find this line:

...
CHECK FOR SYSTEM LIBRARIES, OPTIONS, ETC..
...

and add this code so that cmake uses pkgconfig to find gstreamer libraries:

if(WIN32)
  find_package(PKGConfig QUIET)
  include(CheckFunctionExists)
  include(CheckIncludeFile)
endif()

Edit opencv\sources\modules\highgui\CMakeLists.txt

1. Find these lines:

if(UNIX)
#these variables are set by CHECK_MODULE macro
  foreach(P ${HIGHGUI_INCLUDE_DIRS})
    ocv_include_directories(${P})
  endforeach()

  foreach(P ${HIGHGUI_LIBRARY_DIRS})
    link_directories(${P})
  endforeach()
endif()

and add this code after that:

if(WIN32 AND HAVE_GSTREAMER)
#these variables are set by CHECK_MODULE macro
  foreach(P ${HIGHGUI_INCLUDE_DIRS})
    ocv_include_directories(${P})
  endforeach()

  foreach(P ${HIGHGUI_LIBRARY_DIRS})
    link_directories(${P})
  endforeach()
endif()

Edit opencv\sources\modules\videoio\CMakeLists.txt

In a similar manner add this code:

if(WIN32 AND HAVE_GSTREAMER)
#these variables are set by CHECK_MODULE macro
  foreach(P ${VIDEOIO_INCLUDE_DIRS})
    ocv_include_directories(${P})
  endforeach()

  foreach(P ${VIDEOIO_LIBRARY_DIRS})
    link_directories(${P})
  endforeach()
endif()

At last edit cap_gstreamer.cpp in modules/videoio

Add this code (red color) to make it compatible with windows.

#if defined(WIN32)
#include <stdlib.h>
#else
#include <unistd.h>
#endif
#if defined(WIN32)
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif
#if defined(WIN32)
_fullpath(uri, filename, _MAX_PATH);
#else
uri = realpath(filename, NULL);

Then prepare the solution file using cmake-gui and build the solution in Visual studio.

“Voila”. Now you are good to go to build your application.

Advertisements

4 thoughts on “Compile OpenCV with GStreamer support in Windows

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s