WPILibC++ 2024.3.2
cs::CvSink Class Reference

A sink for user code to accept video frames as OpenCV images. More...

#include <cscore_cv.h>

Inheritance diagram for cs::CvSink:
cs::ImageSink cs::VideoSink

Public Member Functions

 CvSink ()=default
 
 CvSink (std::string_view name, VideoMode::PixelFormat pixelFormat=VideoMode::PixelFormat::kBGR)
 Create a sink for accepting OpenCV images. More...
 
 CvSink (std::string_view name, std::function< void(uint64_t time)> processFrame, VideoMode::PixelFormat pixelFormat=VideoMode::PixelFormat::kBGR)
 Create a sink for accepting OpenCV images in a separate thread. More...
 
uint64_t GrabFrame (cv::Mat &image, double timeout=0.225) const
 Wait for the next frame and get the image. More...
 
uint64_t GrabFrameNoTimeout (cv::Mat &image) const
 Wait for the next frame and get the image. More...
 
- Public Member Functions inherited from cs::ImageSink
void SetDescription (std::string_view description)
 Set sink description. More...
 
std::string GetError () const
 Get error string. More...
 
void SetEnabled (bool enabled)
 Enable or disable getting new frames. More...
 
- Public Member Functions inherited from cs::VideoSink
 VideoSink () noexcept=default
 
 VideoSink (const VideoSink &sink)
 
 VideoSink (VideoSink &&sink) noexcept
 
VideoSinkoperator= (VideoSink other) noexcept
 
 ~VideoSink ()
 
 operator bool () const
 Returns true if the VideoSink is valid. More...
 
int GetHandle () const
 Returns the VideoSink handle. More...
 
bool operator== (const VideoSink &other) const
 
Kind GetKind () const
 Get the kind of the sink. More...
 
std::string GetName () const
 Get the name of the sink. More...
 
std::string GetDescription () const
 Get the sink description. More...
 
VideoProperty GetProperty (std::string_view name)
 Get a property of the sink. More...
 
std::vector< VideoPropertyEnumerateProperties () const
 Enumerate all properties of this sink. More...
 
bool SetConfigJson (std::string_view config)
 Set properties from a JSON configuration string. More...
 
bool SetConfigJson (const wpi::json &config)
 Set properties from a JSON configuration object. More...
 
std::string GetConfigJson () const
 Get a JSON configuration string. More...
 
wpi::json GetConfigJsonObject () const
 Get a JSON configuration object. More...
 
void SetSource (VideoSource source)
 Configure which source should provide frames to this sink. More...
 
VideoSource GetSource () const
 Get the connected source. More...
 
VideoProperty GetSourceProperty (std::string_view name)
 Get a property of the associated source. More...
 
CS_Status GetLastStatus () const
 

Additional Inherited Members

- Public Types inherited from cs::VideoSink
enum  Kind { kUnknown = CS_SINK_UNKNOWN , kMjpeg = CS_SINK_MJPEG , kCv = CS_SINK_CV }
 
- Static Public Member Functions inherited from cs::VideoSink
static std::vector< VideoSinkEnumerateSinks ()
 Enumerate all existing sinks. More...
 
- Protected Member Functions inherited from cs::ImageSink
 ImageSink ()=default
 
- Protected Member Functions inherited from cs::VideoSink
 VideoSink (CS_Sink handle)
 
- Protected Attributes inherited from cs::VideoSink
CS_Status m_status = 0
 
CS_Sink m_handle {0}
 

Detailed Description

A sink for user code to accept video frames as OpenCV images.

These sinks require the WPILib OpenCV builds. For an alternate OpenCV, include "cscore_raw_cv.h" instead, and include your Mat header before that header.

Constructor & Destructor Documentation

◆ CvSink() [1/3]

cs::CvSink::CvSink ( )
default

◆ CvSink() [2/3]

cs::CvSink::CvSink ( std::string_view  name,
VideoMode::PixelFormat  pixelFormat = VideoMode::PixelFormat::kBGR 
)
inlineexplicit

Create a sink for accepting OpenCV images.

WaitForFrame() must be called on the created sink to get each new image.

Parameters
nameSource name (arbitrary unique identifier)
pixelFormatSource pixel format

◆ CvSink() [3/3]

cs::CvSink::CvSink ( std::string_view  name,
std::function< void(uint64_t time)>  processFrame,
VideoMode::PixelFormat  pixelFormat = VideoMode::PixelFormat::kBGR 
)
inline

Create a sink for accepting OpenCV images in a separate thread.

A thread will be created that calls WaitForFrame() and calls the processFrame() callback each time a new frame arrives.

Parameters
nameSource name (arbitrary unique identifier)
processFrameFrame processing function; will be called with a time=0 if an error occurred. processFrame should call GetImage() or GetError() as needed, but should not call (except in very unusual circumstances) WaitForImage().
pixelFormatSource pixel format

Member Function Documentation

◆ GrabFrame()

uint64_t cs::CvSink::GrabFrame ( cv::Mat &  image,
double  timeout = 0.225 
) const
inline

Wait for the next frame and get the image.

Times out (returning 0) after timeout seconds. The provided image will have three 8-bit channels stored in BGR order.

Returns
Frame time, or 0 on error (call GetError() to obtain the error message); the frame time is in the same time base as wpi::Now(), and is in 1 us increments.

◆ GrabFrameNoTimeout()

uint64_t cs::CvSink::GrabFrameNoTimeout ( cv::Mat &  image) const
inline

Wait for the next frame and get the image.

May block forever. The provided image will have three 8-bit channels stored in BGR order.

Returns
Frame time, or 0 on error (call GetError() to obtain the error message); the frame time is in the same time base as wpi::Now(), and is in 1 us increments.

The documentation for this class was generated from the following file: