OpenCV2 setting VideoBackend - c++

Referring to the opencv VideoCapture documentation here, there are a few properties that only work DC1394 v 2.x backend (specifically CV_CAP_PROP_BUFFERSIZE is what I want to change). Looking at the buildInformation for openCV on my machine I see it is compiled with support for DC1394 v 2.x, FFMPEG, Gstreamer. I cannot see to figure out how to set the video backend when initializing VideoCapture. Any ideas?

Looking at the VideoCapture source code there doesn't seem to be a way to open with a specific backend in opencv 2.4.
In fact it will just pick the first backend available it can find with a defined order in the code (for example lets suppose you have opencv compiled for ffmpeg and gstreamer, the order ffmpeg first and gstreamer second, if you distribute your binary to someone and they have gstreamer but not ffmpeg it will fail to open the video capture but continue to search for the next option, in this case gstreamer). So as far as I can see you have 3 choices to use the DC1394 backend:
Modify the source so it always tries to search for DC1394 first (or even further add a function to choose a backend)
Compile with just support for DC1394 as a video backend
Convert your code to opencv 3.x which has the option to set a backend when opening the video capturer.

Related

OpenCV encoding to H264

I want to encode images to H264 video in OpenCV.
As part of my software for image tracking, I'm using VideoWriter in OpenCV 3.4 (64-bit) with Visual Studio 2017 C++. I use ffmpeg for manual encoding, and as it comes with OpenCV I assume this is the best option. My version of OpenCV indicates it has been compiled with ffmpeg, and I can verify it loads the opencv_ffmpeg340_64.dll.
Decoding H264 (AVC1) is absolutely fine, including when specifically using the ffmpeg API. But encoding anything other than MJPG or raw images doesn't work:
VideoWriter.Open() mostly returns false, for some cases it only writes an empty or small header but won't write frames. I've tried not only the ffmpeg API, but also any available API. Redirecting console/debug output to intermediate window in VC doesn't give any messages from OpenCV or ffmpeg.
There are numerous posts on previous versions of OpenCV using FFmpeg, including (Cisco) OpenH264 library and difficulties using this. I've tried many options, installing codecs pack including H264, ffmpeg static version, Cisco openH264 library, setting paths etc and even tried the low level ffmpeg library functions such as cvCreateVideoWriter exposed in OpenCV, and using different video containers (AVI, MPG, MP4) as well as fourcc strings.
I can't believe in 2018 the great OpenCV with FFmpeg support is unable to encode anything but arcane MJPG or raw images. If it uses FFmpeg surely a significant set of functionality should be available?
Though I think this should work, my next best option would be using a separate ffmpeg library, which would ironically add another ffmpeg.dll to my solution I imagine. Any suggestion appreciated!
Code fragment (using any video encoding API):
VideoWriter writer;
int codec = VideoWriter::fourcc('X', '2', '6', '4');
writer.open("test.avi", codec, 30, colorImage.size(), true);
writer.write(colorImage);
writer.release();
To answer my own question - thank you for the previous suggestions though - I've managed to create and write frames to H264 video now, without any recompiling needed. This is what I've used:
Using OpenCV version 3.4.1
In the same folder as the exe: opencv_ffmpeg341_64.dll (provided with OpenCV)
In the same folder: openh264-1.7.0-win64.dll (available from: https://github.com/cisco/openh264/releases)
Use OpenCV VideoWriter class, omitting a specific encoding API
Container: MP4
Codec fourcc string: "H264"
I formatted my machine (Windows 10 64bit) so I can't exclude any issues with potential codec-clashing - also I have not fully tested for other combinations. As this appears to be such a common problem (with many misconceptions) I hope this is useful.

QT 5.8 WebEngine Html 5 Video player support

I have created a Qt 5.8 web engine based application but unfortunately, the video is not playing in the application.
After coming across many forums I found that I need to build QT with flag proprietary-codec
But this solution comes with the warning: When distributing proprietary codec libraries, you must acquire licenses for them.
Unfortunately, I can't use these proprietary codec libraries.
If I use open source codec libraries do I need to compile QT again with some other flag.
Is there any other solution to this approach.
Unfortunately for mp4 file format using proprietary codecs is the only option (i.e. this includes acquiring licenses to distribute and configuring/building Qt with the option you specified already in your question).
From Qt docs:
Qt WebEngine supports the MPEG-4 Part 14 (MP4) file format only if the required proprietary audio and video codecs, such as H.264 and MPEG layer-3 (MP3), have been enabled.

How can I display an IPLImage stream on Qt interface using OpenGL for rendering?

What is the best way to integrate and display an IplImage into a Qt GUI? Can this be done using OpenGL? If yes, how?
Well, I am answering my own question here.
I have been working with OpenCV since a year now and many times I have had to write code that opens a video file and streams it.
Lately, I have been using Qt to provide the high level gui and I came up with a solution to display IplImages on a Qt GUI using OpenGL(which I spent a lot of time struggling with but succeeded finally). Although, OpenCV can be built with Qt highgui support, I was keen in learning a bit of OpenGL and using it in my application.
I thought it would be helpful for people who need a basic framework for running videos for their applications.
This basic framework can be used in a wide range of applications like video content analysis, face recognition, pattern recognition and more. In the broader sense, this is basically a video player without audio support. So if you want you can add audio support and make this a video player. It will play almost all formats as opencv uses ffmpeg to play videos. I have created a C++ library for the same which I use quite extensively:
The main classes are:
QVideo : The main widget, providing functionality to open a video/rtsp stream and play it
QIplImage: The core display widget. It's a QGLWidget which renders an IplImage
BackgroundWorker: This is a thread which runs in the background to retrieve images
from the stream. It is mandatory to run the frame grabber in a background thread,
otherwise it interferes with the GUI thread. All the image processing can be done in
this thread
The source code is provided so you can modify it to your needs.
Once the library is installed, you can use it in your qt application like so:
//Create an object
QVideo* video=new QVideo("Name",);
//set geometry (optional)
setCentralWidget(video);
To build the library, you will need qt and opencv(with ffmpeg support) installed.
The library gets installed in /usr/lib or /usr/lib64 based on the architecture.
The include files get installed in /usr/local/include
Of course, all this can be changed in the project file if needed.
To install, just unzip the package, and run qmake, make and sudo make install.
Note: The project is configured for unix and would need to be changed for windows builds. Drop me a message for more information.

Decode mp4 to raw frames windows c++

I'm trying to decode an MP4 file i've created using mp4creator library. I can't find a ffmpeg or libav example that actually works with the libraries, because the code is incompatible with the examples.
I need a simple way of reading the mp4 file and decoding it into raw audio and video frames. In needs to work on windows too.
If someone can point me towards a working and compatible ffmpeg/libav library + example source file that could work too.
If i could get the library version which was used in this tutorial that would work, since this guy seems to be the only one who wrote a decent tutorial on FFMPEG (albeit on some ancient version)
http://dranger.com/ffmpeg/
edit:
In the latest version of ffmpeg i found the example named: filtering_video.c
using a patch found: http://ffmpeg.org/pipermail/ffmpeg-devel/2012-June/126587.html I have gotten the program to do "something" It looks to be working so I will investigate further.
As edited into the question: In the latest version of ffmpeg i found the example named: filtering_video.c
using a patch found: http://ffmpeg.org/pipermail/ffmpeg-devel/2012-June/126587.html I have gotten the program to work.
Using the function avtoipl described here: http://www.digipedia.pl/usenet/thread/16949/6806/
I've gotten the data into an opencv RGB image which i can display.

How to capture video in OpenCV 2.4 with Video4Linux2 (v4l2) in Qt 4.8 (C++)?

I have Xubuntu 12.04.
My web camera, as I can see, does not provided by linux version of OpenCV 2.4, because in Windows XP (on the same machine) code
CvCapture* capture = cvCaptureFromCAM(0)
does not return NULL, but in Linux it does. I experimented with many arguments (many negative, many positive numbers and zero).
But other applications can capture video from my web camera in Linux.
I've tried to compile and run this code and, I think, it runs good, because I can see dots, and I also can see some strange content of buffer (using -o key). I think, the content is frames, but I can't understand, how to use it (for example, create correct IplImage* from it).
How can I capture video from my web camera in Qt for further OpnenCV video processing?
And I also will need to capture video from several USB and IP cameras in one time.
The solution must be free for commercial use.
You have to build OpenCV with v4l support. Take a look a this guide. And make sure after configuring build you have this line:
V4L/V4L2: Using libv4l
Update: after some googling I found that this is a bug. See this thread and try fix from the third post (use LD_PRELOAD or add v4l1compat.so to linker).