WPILibC++ 2024.3.2
cscore_cv.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef CSCORE_CSCORE_CV_H_
6#define CSCORE_CSCORE_CV_H_
7
8#include <functional>
9
10#include "cscore_c.h"
11
12#ifdef CSCORE_CSCORE_RAW_CV_H_
13#error "Cannot include both cscore_cv.h and cscore_raw_cv.h in the same file"
14#endif
15
16#ifdef __cplusplus
17#include "cscore_oo.h" // NOLINT(build/include_order)
18
19#endif
20
21#if CV_VERSION_MAJOR < 4
22
23#ifdef __cplusplus
24extern "C" { // NOLINT(build/include_order)
25#endif
26
27struct CvMat;
28
29void CS_PutSourceFrame(CS_Source source, struct CvMat* image,
30 CS_Status* status);
31
32uint64_t CS_GrabSinkFrame(CS_Sink sink, struct CvMat* image, CS_Status* status);
33uint64_t CS_GrabSinkFrameTimeout(CS_Sink sink, struct CvMat* image,
34 double timeout, CS_Status* status);
35
36#ifdef __cplusplus
37} // extern "C"
38#endif
39
40#endif // CV_VERSION_MAJOR < 4
41
42#ifdef __cplusplus
43
44#include "cscore_oo.h"
45
46namespace cv {
47class Mat;
48} // namespace cv
49
50namespace cs {
51
52/**
53 * @defgroup cscore_cpp_opencv_special cscore C functions taking a cv::Mat*
54 *
55 * These are needed for specific interop implementations.
56 * @{
57 */
58extern "C" {
59uint64_t CS_GrabSinkFrameCpp(CS_Sink sink, cv::Mat* image, CS_Status* status);
60uint64_t CS_GrabSinkFrameTimeoutCpp(CS_Sink sink, cv::Mat* image,
61 double timeout, CS_Status* status);
62void CS_PutSourceFrameCpp(CS_Source source, cv::Mat* image, CS_Status* status);
63} // extern "C"
64/** @} */
65
66void PutSourceFrame(CS_Source source, cv::Mat& image, CS_Status* status);
67uint64_t GrabSinkFrame(CS_Sink sink, cv::Mat& image, CS_Status* status);
68uint64_t GrabSinkFrameTimeout(CS_Sink sink, cv::Mat& image, double timeout,
69 CS_Status* status);
70
71/**
72 * A source for user code to provide OpenCV images as video frames.
73 * These sources require the WPILib OpenCV builds.
74 * For an alternate OpenCV, include "cscore_raw_cv.h" instead, and
75 * include your Mat header before that header.
76 */
77class CvSource : public ImageSource {
78 public:
79 CvSource() = default;
80
81 /**
82 * Create an OpenCV source.
83 *
84 * @param name Source name (arbitrary unique identifier)
85 * @param mode Video mode being generated
86 */
87 CvSource(std::string_view name, const VideoMode& mode);
88
89 /**
90 * Create an OpenCV source.
91 *
92 * @param name Source name (arbitrary unique identifier)
93 * @param pixelFormat Pixel format
94 * @param width width
95 * @param height height
96 * @param fps fps
97 */
98 CvSource(std::string_view name, VideoMode::PixelFormat pixelFormat, int width,
99 int height, int fps);
100
101 /**
102 * Put an OpenCV image and notify sinks.
103 *
104 * <p>Only 8-bit single-channel or 3-channel (with BGR channel order) images
105 * are supported. If the format, depth or channel order is different, use
106 * cv::Mat::convertTo() and/or cv::cvtColor() to convert it first.
107 *
108 * @param image OpenCV image
109 */
110 void PutFrame(cv::Mat& image);
111};
112
113/**
114 * A sink for user code to accept video frames as OpenCV images.
115 * These sinks require the WPILib OpenCV builds.
116 * For an alternate OpenCV, include "cscore_raw_cv.h" instead, and
117 * include your Mat header before that header.
118 */
119class CvSink : public ImageSink {
120 public:
121 CvSink() = default;
122
123 /**
124 * Create a sink for accepting OpenCV images.
125 *
126 * <p>WaitForFrame() must be called on the created sink to get each new
127 * image.
128 *
129 * @param name Source name (arbitrary unique identifier)
130 * @param pixelFormat Source pixel format
131 */
132 explicit CvSink(std::string_view name, VideoMode::PixelFormat pixelFormat =
133 VideoMode::PixelFormat::kBGR);
134
135 /**
136 * Create a sink for accepting OpenCV images in a separate thread.
137 *
138 * <p>A thread will be created that calls WaitForFrame() and calls the
139 * processFrame() callback each time a new frame arrives.
140 *
141 * @param name Source name (arbitrary unique identifier)
142 * @param processFrame Frame processing function; will be called with a
143 * time=0 if an error occurred. processFrame should call GetImage()
144 * or GetError() as needed, but should not call (except in very
145 * unusual circumstances) WaitForImage().
146 * @param pixelFormat Source pixel format
147 */
148 CvSink(std::string_view name, std::function<void(uint64_t time)> processFrame,
149 VideoMode::PixelFormat pixelFormat = VideoMode::PixelFormat::kBGR);
150
151 /**
152 * Wait for the next frame and get the image.
153 * Times out (returning 0) after timeout seconds.
154 * The provided image will have three 8-bit channels stored in BGR order.
155 *
156 * @return Frame time, or 0 on error (call GetError() to obtain the error
157 * message); the frame time is in the same time base as wpi::Now(),
158 * and is in 1 us increments.
159 */
160 [[nodiscard]]
161 uint64_t GrabFrame(cv::Mat& image, double timeout = 0.225) const;
162
163 /**
164 * Wait for the next frame and get the image. May block forever.
165 * The provided image will have three 8-bit channels stored in BGR order.
166 *
167 * @return Frame time, or 0 on error (call GetError() to obtain the error
168 * message); the frame time is in the same time base as wpi::Now(),
169 * and is in 1 us increments.
170 */
171 [[nodiscard]]
172 uint64_t GrabFrameNoTimeout(cv::Mat& image) const;
173};
174
176 m_handle = CreateCvSource(name, mode, &m_status);
177}
178
180 int width, int height, int fps) {
181 m_handle =
182 CreateCvSource(name, VideoMode{format, width, height, fps}, &m_status);
183}
184
185inline void CvSource::PutFrame(cv::Mat& image) {
186 m_status = 0;
188}
189
191 VideoMode::PixelFormat pixelFormat) {
192 m_handle = CreateCvSink(name, pixelFormat, &m_status);
193}
194
196 std::function<void(uint64_t time)> processFrame,
197 VideoMode::PixelFormat pixelFormat) {
198 m_handle = CreateCvSinkCallback(name, pixelFormat, processFrame, &m_status);
199}
200
201inline uint64_t CvSink::GrabFrame(cv::Mat& image, double timeout) const {
202 m_status = 0;
203 return GrabSinkFrameTimeout(m_handle, image, timeout, &m_status);
204}
205
206inline uint64_t CvSink::GrabFrameNoTimeout(cv::Mat& image) const {
207 m_status = 0;
208 return GrabSinkFrame(m_handle, image, &m_status);
209}
210
211} // namespace cs
212
213#endif
214
215#endif // CSCORE_CSCORE_CV_H_
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation source
Definition: ThirdPartyNotices.txt:111
A sink for user code to accept video frames as OpenCV images.
Definition: cscore_cv.h:119
CvSink()=default
uint64_t GrabFrameNoTimeout(cv::Mat &image) const
Wait for the next frame and get the image.
Definition: cscore_cv.h:206
uint64_t GrabFrame(cv::Mat &image, double timeout=0.225) const
Wait for the next frame and get the image.
Definition: cscore_cv.h:201
A source for user code to provide OpenCV images as video frames.
Definition: cscore_cv.h:77
void PutFrame(cv::Mat &image)
Put an OpenCV image and notify sinks.
Definition: cscore_cv.h:185
CvSource()=default
A base class for single image reading sinks.
Definition: cscore_oo.h:1089
A base class for single image providing sources.
Definition: cscore_oo.h:746
CS_Status m_status
Definition: cscore_oo.h:1002
CS_Sink m_handle
Definition: cscore_oo.h:1003
CS_Source m_handle
Video source handle.
Definition: cscore_oo.h:473
CS_Status m_status
Definition: cscore_oo.h:470
basic_string_view< char > string_view
Definition: core.h:501
void CS_PutSourceFrame(CS_Source source, struct CvMat *image, CS_Status *status)
uint64_t CS_GrabSinkFrame(CS_Sink sink, struct CvMat *image, CS_Status *status)
uint64_t CS_GrabSinkFrameTimeout(CS_Sink sink, struct CvMat *image, double timeout, CS_Status *status)
uint64_t CS_GrabSinkFrameTimeoutCpp(CS_Sink sink, cv::Mat *image, double timeout, CS_Status *status)
void CS_PutSourceFrameCpp(CS_Source source, cv::Mat *image, CS_Status *status)
uint64_t CS_GrabSinkFrameCpp(CS_Sink sink, cv::Mat *image, CS_Status *status)
uint64_t GrabSinkFrameTimeout(CS_Sink sink, WPI_RawFrame &image, double timeout, CS_Status *status)
uint64_t GrabSinkFrame(CS_Sink sink, WPI_RawFrame &image, CS_Status *status)
void PutSourceFrame(CS_Source source, const WPI_RawFrame &image, CS_Status *status)
CS_Sink CreateCvSinkCallback(std::string_view name, VideoMode::PixelFormat pixelFormat, std::function< void(uint64_t time)> processFrame, CS_Status *status)
CS_Sink CreateCvSink(std::string_view name, VideoMode::PixelFormat pixelFormat, CS_Status *status)
CS_Source CreateCvSource(std::string_view name, const VideoMode &mode, CS_Status *status)
CS_Handle CS_Source
Definition: cscore_c.h:53
int CS_Status
Definition: cscore_c.h:46
CS_Handle CS_Sink
Definition: cscore_c.h:52
CameraServer (cscore) namespace.
Definition: cscore_oo.inc:16
Definition: VisionPipeline.h:7
fps
Definition: velocity.h:46
Video mode.
Definition: cscore_cpp.h:62
PixelFormat
Definition: cscore_cpp.h:63
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition: xchar.h:108