WPILibC++  2020.3.2
cscore_raw_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_RAW_CV_H_
9 #define CSCORE_CSCORE_RAW_CV_H_
10 
11 #ifdef CSCORE_CSCORE_CV_H_
12 #error "Cannot include both cscore_cv.h and cscore_raw_cv.h in the same file"
13 #endif
14 
15 #include "cscore_raw.h"
16 
17 namespace cs {
28 class RawCvSource : public RawSource {
29  public:
30  RawCvSource() = default;
31 
38  RawCvSource(const wpi::Twine& name, const VideoMode& mode);
39 
49  RawCvSource(const wpi::Twine& name, VideoMode::PixelFormat pixelFormat,
50  int width, int height, int fps);
51 
61  void PutFrame(cv::Mat& image);
62 
63  private:
64  RawFrame rawFrame;
65 };
66 
77 class RawCvSink : public RawSink {
78  public:
79  RawCvSink() = default;
80 
89  explicit RawCvSink(const wpi::Twine& name);
90 
103  RawCvSink(const wpi::Twine& name,
104  std::function<void(uint64_t time)> processFrame);
105 
115  uint64_t GrabFrame(cv::Mat& image, double timeout = 0.225);
116 
125  uint64_t GrabFrameNoTimeout(cv::Mat& image);
126 
136  uint64_t GrabFrameDirect(cv::Mat& image, double timeout = 0.225);
137 
146  uint64_t GrabFrameNoTimeoutDirect(cv::Mat& image);
147 
148  private:
149  RawFrame rawFrame;
150 };
151 
152 inline RawCvSource::RawCvSource(const wpi::Twine& name, const VideoMode& mode)
153  : RawSource{name, mode} {}
154 
155 inline RawCvSource::RawCvSource(const wpi::Twine& name,
156  VideoMode::PixelFormat format, int width,
157  int height, int fps)
158  : RawSource{name, format, width, height, fps} {}
159 
160 inline void RawCvSource::PutFrame(cv::Mat& image) {
161  m_status = 0;
162  rawFrame.data = reinterpret_cast<char*>(image.data);
163  rawFrame.width = image.cols;
164  rawFrame.height = image.rows;
165  rawFrame.totalData = image.total() * image.channels;
166  rawFrame.pixelFormat = image.channels == 3 ? CS_PIXFMT_BGR : CS_PIXFMT_GRAY;
167  PutSourceFrame(m_handle, rawFrame, &m_status);
168 }
169 
170 inline RawCvSink::RawCvSink(const wpi::Twine& name) : RawSink{name} {}
171 
172 inline RawCvSink::RawCvSink(const wpi::Twine& name,
173  std::function<void(uint64_t time)> processFrame)
174  : RawSink{name, processFrame} {}
175 
176 inline uint64_t RawCvSink::GrabFrame(cv::Mat& image, double timeout) {
177  cv::Mat tmpMat;
178  auto retVal = GrabFrameDirect(tmpMat);
179  if (retVal <= 0) {
180  return retVal;
181  }
182  tmpMat.copyTo(image);
183  return retVal;
184 }
185 
186 inline uint64_t RawCvSink::GrabFrameNoTimeout(cv::Mat& image) {
187  cv::Mat tmpMat;
188  auto retVal = GrabFrameNoTimeoutDirect(tmpMat);
189  if (retVal <= 0) {
190  return retVal;
191  }
192  tmpMat.copyTo(image);
193  return retVal;
194 }
195 
196 inline uint64_t RawCvSink::GrabFrameDirect(cv::Mat& image, double timeout) {
197  rawFrame.height = 0;
198  rawFrame.width = 0;
199  rawFrame.pixelFormat = CS_PixelFormat::CS_PIXFMT_BGR;
200  m_status = RawSink::GrabFrame(rawFrame, timeout);
201  if (m_status <= 0) return m_status;
202  image = cv::Mat{rawFrame.height, rawFrame.width, CV_8UC3, rawFrame.data};
203  return m_status;
204 }
205 
206 inline uint64_t RawCvSink::GrabFrameNoTimeoutDirect(cv::Mat& image) {
207  rawFrame.height = 0;
208  rawFrame.width = 0;
209  rawFrame.pixelFormat = CS_PixelFormat::CS_PIXFMT_BGR;
210  m_status = RawSink::GrabFrameNoTimeout(rawFrame);
211  if (m_status <= 0) return m_status;
212  image = cv::Mat{rawFrame.height, rawFrame.width, CV_8UC3, rawFrame.data};
213  return m_status;
214 }
215 
216 } // namespace cs
217 
218 #endif // CSCORE_CSCORE_RAW_CV_H_
cs::RawCvSink::GrabFrameDirect
uint64_t GrabFrameDirect(cv::Mat &image, double timeout=0.225)
Wait for the next frame and get the image.
Definition: cscore_raw_cv.h:196
cs::RawCvSink::GrabFrame
uint64_t GrabFrame(cv::Mat &image, double timeout=0.225)
Wait for the next frame and get the image.
Definition: cscore_raw_cv.h:176
cs::VideoMode
Video mode.
Definition: cscore_cpp.h:68
cs::RawCvSink::GrabFrameNoTimeout
uint64_t GrabFrameNoTimeout(cv::Mat &image)
Wait for the next frame and get the image.
Definition: cscore_raw_cv.h:186
cs
CameraServer (cscore) namespace.
Definition: cscore_cpp.h:35
cs::RawCvSink
A sink for user code to accept raw video frames as OpenCV images.
Definition: cscore_raw_cv.h:77
cs::RawCvSource::PutFrame
void PutFrame(cv::Mat &image)
Put an OpenCV image and notify sinks.
Definition: cscore_raw_cv.h:160
cs::RawCvSink::GrabFrameNoTimeoutDirect
uint64_t GrabFrameNoTimeoutDirect(cv::Mat &image)
Wait for the next frame and get the image.
Definition: cscore_raw_cv.h:206
cs::RawCvSource
A source for using the raw frame API to provide opencv images.
Definition: cscore_raw_cv.h:28
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85