WPILibC++  2020.3.2
cscore_cv.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef CSCORE_CSCORE_CV_H_
9 #define CSCORE_CSCORE_CV_H_
10 
11 #include "cscore_c.h"
12 
13 #ifdef CSCORE_CSCORE_RAW_CV_H_
14 #error "Cannot include both cscore_cv.h and cscore_raw_cv.h in the same file"
15 #endif
16 
17 #ifdef __cplusplus
18 #include "cscore_oo.h" // NOLINT(build/include_order)
19 
20 #endif
21 
22 #if CV_VERSION_MAJOR < 4
23 
24 #ifdef __cplusplus
25 extern "C" { // NOLINT(build/include_order)
26 #endif
27 
28 struct CvMat;
29 
30 void CS_PutSourceFrame(CS_Source source, struct CvMat* image,
31  CS_Status* status);
32 
33 uint64_t CS_GrabSinkFrame(CS_Sink sink, struct CvMat* image, CS_Status* status);
34 uint64_t CS_GrabSinkFrameTimeout(CS_Sink sink, struct CvMat* image,
35  double timeout, CS_Status* status);
36 
37 #ifdef __cplusplus
38 } // extern "C"
39 #endif
40 
41 #endif // CV_VERSION_MAJOR < 4
42 
43 #ifdef __cplusplus
44 
45 #include "cscore_oo.h"
46 
47 namespace cv {
48 class Mat;
49 } // namespace cv
50 
51 namespace cs {
52 
59 extern "C" {
60 uint64_t CS_GrabSinkFrameCpp(CS_Sink sink, cv::Mat* image, CS_Status* status);
61 uint64_t CS_GrabSinkFrameTimeoutCpp(CS_Sink sink, cv::Mat* image,
62  double timeout, CS_Status* status);
63 void CS_PutSourceFrameCpp(CS_Source source, cv::Mat* image, CS_Status* status);
64 } // extern "C"
67 void PutSourceFrame(CS_Source source, cv::Mat& image, CS_Status* status);
68 uint64_t GrabSinkFrame(CS_Sink sink, cv::Mat& image, CS_Status* status);
69 uint64_t GrabSinkFrameTimeout(CS_Sink sink, cv::Mat& image, double timeout,
70  CS_Status* status);
71 
78 class CvSource : public ImageSource {
79  public:
80  CvSource() = default;
81 
88  CvSource(const wpi::Twine& name, const VideoMode& mode);
89 
99  CvSource(const wpi::Twine& name, VideoMode::PixelFormat pixelFormat,
100  int width, int height, int fps);
101 
111  void PutFrame(cv::Mat& image);
112 };
113 
120 class CvSink : public ImageSink {
121  public:
122  CvSink() = default;
123 
132  explicit CvSink(const wpi::Twine& name);
133 
146  CvSink(const wpi::Twine& name,
147  std::function<void(uint64_t time)> processFrame);
148 
158  uint64_t GrabFrame(cv::Mat& image, double timeout = 0.225) const;
159 
168  uint64_t GrabFrameNoTimeout(cv::Mat& image) const;
169 };
170 
171 inline CvSource::CvSource(const wpi::Twine& name, const VideoMode& mode) {
172  m_handle = CreateCvSource(name, mode, &m_status);
173 }
174 
175 inline CvSource::CvSource(const wpi::Twine& name, VideoMode::PixelFormat format,
176  int width, int height, int fps) {
177  m_handle =
178  CreateCvSource(name, VideoMode{format, width, height, fps}, &m_status);
179 }
180 
181 inline void CvSource::PutFrame(cv::Mat& image) {
182  m_status = 0;
183  PutSourceFrame(m_handle, image, &m_status);
184 }
185 
186 inline CvSink::CvSink(const wpi::Twine& name) {
187  m_handle = CreateCvSink(name, &m_status);
188 }
189 
190 inline CvSink::CvSink(const wpi::Twine& name,
191  std::function<void(uint64_t time)> processFrame) {
192  m_handle = CreateCvSinkCallback(name, processFrame, &m_status);
193 }
194 
195 inline uint64_t CvSink::GrabFrame(cv::Mat& image, double timeout) const {
196  m_status = 0;
197  return GrabSinkFrameTimeout(m_handle, image, timeout, &m_status);
198 }
199 
200 inline uint64_t CvSink::GrabFrameNoTimeout(cv::Mat& image) const {
201  m_status = 0;
202  return GrabSinkFrame(m_handle, image, &m_status);
203 }
204 
205 } // namespace cs
206 
207 #endif
208 
209 #endif // CSCORE_CSCORE_CV_H_
wpi::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:132
cs
CameraServer (cscore) namespace.
Definition: cscore_cpp.h:35
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85