Ask Your Question

GStreamer unable to query pipeline state on Ubuntu18.04 with Opencv4

asked 2018-10-02 05:18:25 -0500

fra gravatar image

I have the same problem described here but with Ubuntu 18.04.

The program runs and does not stop but it does not catch the keyboard input and as soon as it starts I get the error

(process:11477): GStreamer-CRITICAL **: 18:44:25.567: gst_element_get_state: assertion 'GST_IS_ELEMENT (element)' failed
OpenCV | GStreamer warning: GStreamer: unable to query pipeline state (/home/fra/Documents/openCV/openCV/modules/videoio/src/cap_gstreamer.cpp:420)

I have installed the gstreamer libraries because they were required by opencv and I am including and linking them in the Makefile like this (see the complete Makefile later):

Have anyone had the same problem? What is generating the exception?

I am thinking the error is related to Opencv deendencies but I am not sure is not a problem related to SDL2.0.


Here is a minimal working example: clicking the left and right buttons of the mouse works (catched directly with Opencv callbacks) while pressing and releasing the keyboard buttons does not call the proper SDL callback. The script can be launched with ./executable camID anyNameYouWant where camID is an integer (typically 0 or 1) that identifies the cam you want to use.

#ifndef __OPENCV__
#define __OPENCV__
#include "opencv2/opencv.hpp"
//#include "utils.hpp"
//#include "constants.hpp"
#include <vector>
#include <SDL_events.h>

using namespace cv;
using namespace std;
static const int delay = 2;
#define WINDOW_SCALE 1.7

void onTrackbar_changed(int, void* data);
void onMouse(int evt, int x, int y, int flags, void* param);
void PrintKeyInfo( SDL_KeyboardEvent *key );
int keyboardCallback(SDL_KeyboardEvent ev);

//InputStateContext context;

int main(int argc, char* argv[])
    /* Initialise SDL */
    if( SDL_Init( SDL_INIT_VIDEO ) < 0)
        fprintf( stderr, "Could not initialise SDL: %s\n", SDL_GetError() );
        exit( -1 );

  string host;
  unsigned int port;
  const String sourceReference = argv[1];
  int camNum;
  string sensorName;
    camNum = stoi(sourceReference); // throws std::length_error
  catch (const std::exception& e)// reference to the base of a polymorphic object
    std::cout<<"Exception: " << e.what()<<endl; // information from length_error printed
    return -1;
  if (argc>4)
      host = argv[2];
      port = atoi(argv[3]);
      sensorName = argv[4];
    catch (const std::exception& e)
      cout<<"impossible to convert host or port"<<endl;
      return -1;
  else if(argc>2)
    cout<<"argumetns less than 4"<<endl;
    host = "http://localhost";
    port = 3456;

    sensorName = argv[2];
    cout<<"sensor name set from arguments: "<< sensorName<<endl;

    cout<<"stopping execution: too few arguments."<<endl;
    return -1;
  VideoCapture cam(camNum);
  /* or
  VideoCapture captUndTst;;*/
  if ( !cam.isOpened())
    cout << "Could not open reference " << sourceReference << endl;
   return -1;

  namedWindow("Camera", WINDOW_NORMAL);
  Mat frame;

  SDL_Event keyboardEvent;

  resize(frame, frame, cv::Size(frame.cols/WINDOW_SCALE, frame.rows/WINDOW_SCALE));
  resizeWindow("Camera", cv::Size(frame.cols/WINDOW_SCALE, frame.rows/WINDOW_SCALE));
  setMouseCallback("Camera", onMouse, &frame);
      while( SDL_PollEvent( &keyboardEvent) )
          switch( keyboardEvent.type )
              /* Keyboard event */
              /* Pass the event data onto PrintKeyInfo() */

              case SDL_KEYDOWN:
              case SDL_KEYUP:

              /* SDL_QUIT event (window close) */
              case SDL_QUIT:
              return 0;


     Mat frame_out;
     frame_out = frame.clone();
    cam>>frame ...
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-10-02 05:26:15 -0500

berak gravatar image

updated 2018-10-02 05:32:06 -0500

try to reduce it to:

    VideoCapture cam(camNum);
    while (cam.isOpened() {
        Mat frame;
        if (! break;
        int k = waitKey(10); // NO YOU DON'T NEED SDL !
        if (k == 27) break; // escape pressed

in other words, get rid of all that SDL debris first.

then try to avoid gstreamer, and use v4l instead:

   VideoCapture cam(0, CAP_V4L);
edit flag offensive delete link more


also, waitKey() returns the key, not the delay.

berak gravatar imageberak ( 2018-10-02 05:35:23 -0500 )edit

I am using SDL not related to waitkey nor to catch escape but to catch other keyboard events ('a', 'm', 'u' released) in the function keyboardCallback because openCV does not offer proper callbacks for keyboard events.

fra gravatar imagefra ( 2018-10-02 06:43:02 -0500 )edit

honestly, then you should go all the way and draw your image on an sdl window, and avoid all opencv gui.

mixing both is seriously asking for trouble.

berak gravatar imageberak ( 2018-10-02 06:45:28 -0500 )edit

Is it more reliable than openCV gui?

fra gravatar imagefra ( 2018-10-02 06:53:37 -0500 )edit

no idea, but you must not mix.

berak gravatar imageberak ( 2018-10-02 06:55:40 -0500 )edit

Sorry but I do not understand: for mixing you mean capturing the cam frames with open cv and using SDL for callbacks or using Opencv window with SDL callbacks or both?

fra gravatar imagefra ( 2018-10-02 07:07:29 -0500 )edit

callbacks, drawing, windows, anything gui related.

berak gravatar imageberak ( 2018-10-02 07:10:21 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-10-02 05:18:25 -0500

Seen: 685 times

Last updated: Oct 02 '18