-
Notifications
You must be signed in to change notification settings - Fork 35
OpenCV VideoCapture with tcambin pipeline
TIS-Stefan edited this page Apr 19, 2023
·
1 revision
This sample shows, how to use a GStreamer pipeline with our tcambin and set camera properties like exposure and gain. The pipeline string is passed to a cv::VideoCapture
object.
#include <iostream>
#include <string>
#include <stdexcept>
#include "opencv2/opencv.hpp"
/*
Helper function for formatted strings from
https://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf
*/
template<typename ... Args>
std::string string_format( const std::string& format, Args ... args )
{
int size_s = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
if( size_s <= 0 ){ throw std::runtime_error( "Error during formatting." ); }
auto size = static_cast<size_t>( size_s );
std::unique_ptr<char[]> buf( new char[ size ] );
std::snprintf( buf.get(), size, format.c_str(), args ... );
return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
}
int main(int argc, char **argv)
{
std::string serial = "serial=41910044"; //Use the serial number of acamera or leave this string empty ("").
std::string format = "BGRx";
int width = 640;
int height = 480;
int fps = 30;
// Create a string with some properties. Disable exposure and gain automatic and set a short
// exposure time. If no properties are to be set, leave the string empty ("")
std::string properties = "tcam-properties=tcam,ExposureAuto=Off,ExposureTime=3333,GainAuto=Off,Gain=0.0";
// Create the pipleine.
std::string pipeline = string_format("tcambin %s %s ! video/x-raw,format=%s,framerate=%d/1,width=%d,height=%d ! videoconvert ! appsink",
properties.c_str(), serial.c_str(), format.c_str(), fps, width,height);
auto cap = std::make_unique<cv::VideoCapture>(pipeline.c_str(), cv::CAP_GSTREAMER);
if (!cap->isOpened())
{
std::cout << "Error: Failed to open video capture device." << std::endl;
return -1;
}
std::cout << "Press Esc key to end program." << std::endl;
cv::namedWindow("Video Window",1);
int key = 0;
while(key != 27)
{
cv::Mat frame;
*cap >> frame; // get a new frame from camera
cv::imshow("Video Window", frame);
key = cv::waitKey(10);
}
cv::destroyWindow("Video Window");
std::cout << "End of Program." << std::endl;
return 0;
}
This sample is inherited from the standard OpenCV VideoCapture sample. It is the GStreamer Pipeline, which is new.