Compile Opencv with GStreamer for Visual Studio 2019 on windows 10 with and contribution modules

The goal of this tutorial is a simple step-by-step compilation of Opencv 4.2 with the contribution of extra modules with GStreamer as a bonus. The environment is Windows 10, Visual Studio 2019 C++ application. This took me almost one day to correct of CMake setting. The goal of this tutorial is: to compile a set of OpenCV libraries with GStreamer and FFmpeg on Windows. I focus mainly on GStreamer. It is a little bit more tricky. You will find the following information about your Opencv environment by compiling and running this simple code. The Opencv GStreamer is turned as YES. GStreamer gives you a great opportunity to stream OpenCV output video outside of your program, for example, a web application.

I recently compiled with OpenCV 4.4. The update at the end of the post. 
It is working!! wow, The working app and configuration in future tutorials.
opencv on web video stream

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
int main()
{
    std::cout << "Hello World!\n";
    std::cout << cv::getBuildInformation() << std::endl; 
    std::getchar();
}
Visual studio opencv gstreamer

Environment for opencv under Windows compiled with extra modules and GStreamer

1) Get Opencv source. I would like to recommend taking the released version. I took the Opencv 4.2 source code zip on the left and released opencv_contrib 4.2 zip on the right. 


2) Get Cmake, I will recommend downloading the 3.17 release candidate 3 for 64-bit Windows. I failed with 3.16.5.
3) Extract OpenCV source into opencv-4.2.0
4) Extract OpenCV source into opencv_contrib-4.2.0
5) Extract Cmake for example into the same root folder https://cmake.org/download/
6) Create the empty build folder as you can see on the image with directories. 
7) Download and install GStreamer from https://gstreamer.freedesktop.org/download/
MSVC 64-bit (VS 2019), You can download and install both, but the1.16.2 development installer for MSVC 64-bit (VS 2019) is important.
The result of the installation is this directory structure.  It will be linked with our Cmake configuration later on.  
8) Make sure you have Visual Studio 2019 with all C++ functionality

Configure Cmake to compile Opencv with GStreamer on Windows

I have a problem with this for a long time. I installed OpenCV with FFmpeg and GStreamer on Linux Debian and on Ubuntu many times. I installed OpenCV with contribution and FFmpeg libraries on Windows as well many times. The Opencv GStreamer took me almost a whole weekend to let this work. It is not so difficult at all. 

9)  Open cmake/bin folder and start cmake-gui.exe
10)Add "where is the source code" as main /opencv-4.2.0 folder
11)Add "where to build the binaries" as new created /build folder
12) Hit configure button and select Visual Studio 2019
13) Wait for the end of configuration and ADD OENCV_EXTRA_MODULES_PATH
to point into /opencv_contrib-master/modules
14) Lesson learned Correct the path of extra modules with this /
If you use path c:\opencv_contrib-master\modules the error message when parsing string problem will occur. 
 
15) Set all GStreamer paths as follows:
Note: Some points directly to xx.lib which is important!! Some points in the header include the directory.

16) Hit Configure
17) Hit Generate if the configuration is OK
18) Open project if 16- 17 are DONE

Build a Visual Studio Opencv project with GStreamer on Windows

19) Change project to Release and x64
20) Right-click on whole solution and select build solution
21) After the solution is successfully built, install the solution by build just INSTALL in the solution  
  

22) installation is done, The ready to use libraries and headers are located under build/install

Visual Studio 2019 Opencv with GStreamer configuration

24) Create a new C++ console application in Visual Studio 2019
25) Change the project to x64, I am using Release configuration. 
26) Open property of your project and set Additional include directory. This variable points into the include directory under install folder. (picture above)
 
27) Under Linker- General- Additional Library Directories set- path to your install lib directory example c:\xxx\xxx\build3\install\x64\vc16\lib

28) Under Linker-Input -Set Additional dependencies variable- Set names of OpenCV libraries as opencv_core420.lib

29) Copy opencv_core420.dll into your project next to testOpencvGstreamer.exe or set environment variables to find your OpenCV dlls.
30) Compile the project and with the expected result
 

The next tutorial will be about the OpenCV GStreamer program example.

UPDATE OPENCV GStreamer

To use GStreamer you need to add path system environment variables to access GStreamer runtime as follows. 
gstreamer windows 10 opencv

Opencv GStreamer YouTube tutorial for Visual Studio

YouTube tutorial build OpenCV from source with Gstreamer on Windows

This will be a future tutorial on how to build the Opencv Gstreamer app on Windows.

Thanks for sharing: How to compile OpenCV  with GStreamer on Windows Visual Studio

Opencv 4.4 Gstreamer

gstreamer opencv 4.4



Next Post Previous Post
7 Comments
  • Chipi
    Chipi April 22, 2020 at 5:01 AM

    Hi, great tutorial!

    I'm trying to use GStreamer with OpenCV too and found your blog, but still I didn't achieve it to work fine in Windows (In Linux my code works perfect ). I got next error on execution:
    [ WARN:0] global C:\opencv\modules\videoio\src\cap_gstreamer.cpp (1274) cv::CvVideoWriter_GStreamer::close_ OpenCV | GStreamer warning: No source in GStreamer pipeline. Ignore
    [ERROR:0] global C:\opencv\modules\videoio\src\cap.cpp (415) cv::VideoWriter::open VIDEOIO(GSTREAMER): raised OpenCV exception:

    OpenCV(4.2.0) C:\opencv\modules\videoio\src\cap_gstreamer.cpp:144: error: (-215:Assertion failed) ptr in function 'cv::`anonymous-namespace'::GSafePtr::get'

    (I also got a similar issue when I'm trying to use VideoCapture).

    Have you see something like that? Do you have any idea?

    BTW, you have a minor mistake when you type "OPENCV_EXTRA_MODULES_PATH" in step 13.

    Thanks!

    • Unknown
      Unknown June 15, 2020 at 3:29 PM

      Hi, the procedure does not work unless the path to gstreamer dlls is added to the PATH.

    • Vl
      Vl August 13, 2020 at 10:03 AM

      Hi, I have seen this issue. I do not know the resolution. I have clean installation. I will try to reproduce it. I remember the problems with final pipeline that works in Linux and not worked in windows. It was somethink like extra space after the last character in pipeline: max=0 sync=true " It fixed my problem. Another issue can be problem with set up windows environmental variables of GStreamer for runtime. I will try to replicate your problem. Just try add extra space at the end of Pipeline and Fix windows environmental variables.

      VideoWriter writer(
      "appsrc ! videoconvert ! videoscale ! video/x-raw,width=320,height=240 ! theoraenc ! oggmux ! tcpserversink host=192.168.0.116 port=8080 recover-policy=keyframe sync-method=latest-keyframe unit-format=buffers units-max=1 buffers-max=0 sync=true ",
      0,
      5,
      Size(320, 240),
      true);

    • Vl
      Vl August 13, 2020 at 10:40 AM

      Update : I have a same problem with Opencv 4.4 . IT was tested on 4.2. Stil this can be a configuration problme.
      V

    • Chipi
      Chipi August 14, 2020 at 1:35 AM

      Hi, thanks for the reply. I tried the extra space in pipeline and nothing, even using the code you pasted to me. I'm still using OpenCV 4.2, and about the config...
      GStreamer appers to be correctly set in OpenCV compilation, and about the environment variables: I have set GSTREAMER_DIR, GSTREAMER_1_0_ROOT_X86_64 and in PATH I have ...\bin, ...\lib and ...\lib\gstreamer-1.0.

      But still, the problem it's weird because it's like GStreamer it's not able to read the pipeline given through OpenCV but I can use it directly from cmd without any problem.

    • Chipi
      Chipi August 14, 2020 at 1:41 AM

      Hi, thanks for the reply. I tried the extra space in pipeline and nothing, even using the code you pasted to me. I'm still using OpenCV 4.2, and about the config...
      GStreamer appers to be correctly set in OpenCV compilation, and about the environment variables: I have set GSTREAMER_DIR, GSTREAMER_1_0_ROOT_X86_64 and in PATH I have ...\bin, ...\lib and ...\lib\gstreamer-1.0.

      But still, the problem it's weird because it's like GStreamer it's not able to read the pipeline given through OpenCV but I can use it directly from cmd without any problem.

  • Ravi
    Ravi May 22, 2023 at 5:35 AM

    Excellent post, Thanks for sharing this.

    Learn more about our courses to get the best knowledge. Agile Coach Certification

Add Comment
comment url