WPILibC++ 2023.4.3-108-ge5452e3
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 "cscore_c.h"
9
10#ifdef CSCORE_CSCORE_RAW_CV_H_
11#error "Cannot include both cscore_cv.h and cscore_raw_cv.h in the same file"
12#endif
13
14#ifdef __cplusplus
15#include "cscore_oo.h" // NOLINT(build/include_order)
16
17#endif
18
19#if CV_VERSION_MAJOR < 4
20
21#ifdef __cplusplus
22extern "C" { // NOLINT(build/include_order)
23#endif
24
25struct CvMat;
26
27void CS_PutSourceFrame(CS_Source source, struct CvMat* image,
28 CS_Status* status);
29
30uint64_t CS_GrabSinkFrame(CS_Sink sink, struct CvMat* image, CS_Status* status);
31uint64_t CS_GrabSinkFrameTimeout(CS_Sink sink, struct CvMat* image,
32 double timeout, CS_Status* status);
33
34#ifdef __cplusplus
35} // extern "C"
36#endif
37
38#endif // CV_VERSION_MAJOR < 4
39
40#ifdef __cplusplus
41
42#include "cscore_oo.h"
43
44namespace cv {
45class Mat;
46} // namespace cv
47
48namespace cs {
49
50/**
51 * @defgroup cscore_cpp_opencv_special cscore C functions taking a cv::Mat*
52 *
53 * These are needed for specific interop implementations.
54 * @{
55 */
56extern "C" {
57uint64_t CS_GrabSinkFrameCpp(CS_Sink sink, cv::Mat* image, CS_Status* status);
59 double timeout, CS_Status* status);
60void CS_PutSourceFrameCpp(CS_Source source, cv::Mat* image, CS_Status* status);
61} // extern "C"
62/** @} */
63
64void PutSourceFrame(CS_Source source, cv::Mat& image, CS_Status* status);
65uint64_t GrabSinkFrame(CS_Sink sink, cv::Mat& image, CS_Status* status);
66uint64_t GrabSinkFrameTimeout(CS_Sink sink, cv::Mat& image, double timeout,
67 CS_Status* status);
68
69/**
70 * A source for user code to provide OpenCV images as video frames.
71 * These sources require the WPILib OpenCV builds.
72 * For an alternate OpenCV, include "cscore_raw_cv.h" instead, and
73 * include your Mat header before that header.
74 */
75class CvSource : public ImageSource {
76 public:
77 CvSource() = default;
78
79 /**
80 * Create an OpenCV source.
81 *
82 * @param name Source name (arbitrary unique identifier)
83 * @param mode Video mode being generated
84 */
85 CvSource(std::string_view name, const VideoMode& mode);
86
87 /**
88 * Create an OpenCV source.
89 *
90 * @param name Source name (arbitrary unique identifier)
91 * @param pixelFormat Pixel format
92 * @param width width
93 * @param height height
94 * @param fps fps
95 */
96 CvSource(std::string_view name, VideoMode::PixelFormat pixelFormat, int width,
97 int height, int fps);
98
99 /**
100 * Put an OpenCV image and notify sinks.
101 *
102 * <p>Only 8-bit single-channel or 3-channel (with BGR channel order) images
103 * are supported. If the format, depth or channel order is different, use
104 * cv::Mat::convertTo() and/or cv::cvtColor() to convert it first.
105 *
106 * @param image OpenCV image
107 */
108 void PutFrame(cv::Mat& image);
109};
110
111/**
112 * A sink for user code to accept video frames as OpenCV images.
113 * These sinks require the WPILib OpenCV builds.
114 * For an alternate OpenCV, include "cscore_raw_cv.h" instead, and
115 * include your Mat header before that header.
116 */
117class CvSink : public ImageSink {
118 public:
119 CvSink() = default;
120
121 /**
122 * Create a sink for accepting OpenCV images.
123 *
124 * <p>WaitForFrame() must be called on the created sink to get each new
125 * image.
126 *
127 * @param name Source name (arbitrary unique identifier)
128 */
129 explicit CvSink(std::string_view name);
130
131 /**
132 * Create a sink for accepting OpenCV images in a separate thread.
133 *
134 * <p>A thread will be created that calls WaitForFrame() and calls the
135 * processFrame() callback each time a new frame arrives.
136 *
137 * @param name Source name (arbitrary unique identifier)
138 * @param processFrame Frame processing function; will be called with a
139 * time=0 if an error occurred. processFrame should call GetImage()
140 * or GetError() as needed, but should not call (except in very
141 * unusual circumstances) WaitForImage().
142 */
144 std::function<void(uint64_t time)> processFrame);
145
146 /**
147 * Wait for the next frame and get the image.
148 * Times out (returning 0) after timeout seconds.
149 * The provided image will have three 8-bit channels stored in BGR order.
150 *
151 * @return Frame time, or 0 on error (call GetError() to obtain the error
152 * message); the frame time is in the same time base as wpi::Now(),
153 * and is in 1 us increments.
154 */
155 [[nodiscard]]
156 uint64_t GrabFrame(cv::Mat& image, double timeout = 0.225) const;
157
158 /**
159 * Wait for the next frame and get the image. May block forever.
160 * The provided image will have three 8-bit channels stored in BGR order.
161 *
162 * @return Frame time, or 0 on error (call GetError() to obtain the error
163 * message); the frame time is in the same time base as wpi::Now(),
164 * and is in 1 us increments.
165 */
166 [[nodiscard]]
167 uint64_t GrabFrameNoTimeout(cv::Mat& image) const;
168};
169
171 m_handle = CreateCvSource(name, mode, &m_status);
172}
173
175 int width, int height, int fps) {
176 m_handle =
177 CreateCvSource(name, VideoMode{format, width, height, fps}, &m_status);
178}
179
180inline void CvSource::PutFrame(cv::Mat& image) {
181 m_status = 0;
183}
184
187}
188
190 std::function<void(uint64_t time)> processFrame) {
191 m_handle = CreateCvSinkCallback(name, processFrame, &m_status);
192}
193
194inline uint64_t CvSink::GrabFrame(cv::Mat& image, double timeout) const {
195 m_status = 0;
196 return GrabSinkFrameTimeout(m_handle, image, timeout, &m_status);
197}
198
199inline uint64_t CvSink::GrabFrameNoTimeout(cv::Mat& image) const {
200 m_status = 0;
201 return GrabSinkFrame(m_handle, image, &m_status);
202}
203
204} // namespace cs
205
206#endif
207
208#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:117
CvSink()=default
uint64_t GrabFrameNoTimeout(cv::Mat &image) const
Wait for the next frame and get the image.
Definition: cscore_cv.h:199
uint64_t GrabFrame(cv::Mat &image, double timeout=0.225) const
Wait for the next frame and get the image.
Definition: cscore_cv.h:194
A source for user code to provide OpenCV images as video frames.
Definition: cscore_cv.h:75
void PutFrame(cv::Mat &image)
Put an OpenCV image and notify sinks.
Definition: cscore_cv.h:180
CvSource()=default
A base class for single image reading sinks.
Definition: cscore_oo.h:940
A base class for single image providing sources.
Definition: cscore_oo.h:610
CS_Status m_status
Definition: cscore_oo.h:853
CS_Sink m_handle
Definition: cscore_oo.h:854
CS_Source m_handle
Definition: cscore_oo.h:352
CS_Status m_status
Definition: cscore_oo.h:351
basic_string_view< char > string_view
Definition: core.h:520
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)
CS_Sink CreateCvSinkCallback(std::string_view name, std::function< void(uint64_t time)> processFrame, CS_Status *status)
CS_Sink CreateCvSink(std::string_view name, CS_Status *status)
CS_Source CreateCvSource(std::string_view name, const VideoMode &mode, CS_Status *status)
CS_Handle CS_Source
Definition: cscore_c.h:51
int CS_Status
Definition: cscore_c.h:44
CS_Handle CS_Sink
Definition: cscore_c.h:50
::uint64_t uint64_t
Definition: Meta.h:58
CameraServer (cscore) namespace.
Definition: cscore_oo.inc:15
void PutSourceFrame(CS_Source source, cv::Mat &image, CS_Status *status)
uint64_t GrabSinkFrame(CS_Sink sink, cv::Mat &image, CS_Status *status)
uint64_t GrabSinkFrameTimeout(CS_Sink sink, cv::Mat &image, double timeout, CS_Status *status)
Definition: VisionPipeline.h:7
fps
Definition: velocity.h:46
Video mode.
Definition: cscore_cpp.h:64
PixelFormat
Definition: cscore_cpp.h:65
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition: xchar.h:87