WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
CvSource.hpp
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#pragma once
6
7#include <string_view>
8
9#include <opencv2/core/mat.hpp>
10
12#include "wpi/cs/VideoMode.hpp"
13#include "wpi/cs/cscore_raw.hpp"
14
15namespace wpi::cs {
16
17/**
18 * A source for user code to provide OpenCV images as video frames.
19 *
20 * This is not dependent on any opencv binary ABI, and can be used
21 * with versions of most versions of OpenCV.
22 */
23class CvSource : public ImageSource {
24 public:
25 CvSource() = default;
26
27 /**
28 * Create an OpenCV source.
29 *
30 * @param name Source name (arbitrary unique identifier)
31 * @param mode Video mode being generated
32 */
33 CvSource(std::string_view name, const VideoMode& mode) {
34 m_handle = CreateRawSource(name, true, mode, &m_status);
35 }
36
37 /**
38 * Create an OpenCV source.
39 *
40 * @param name Source name (arbitrary unique identifier)
41 * @param pixelFormat Pixel format
42 * @param width width
43 * @param height height
44 * @param fps fps
45 */
46 CvSource(std::string_view name, wpi::util::PixelFormat pixelFormat, int width,
47 int height, int fps) {
49 name, true, VideoMode{pixelFormat, width, height, fps}, &m_status);
50 }
51
52 /**
53 * Put an OpenCV image and notify sinks
54 *
55 * <p>
56 * The image format is guessed from the number of channels. The channel
57 * mapping is as follows. 1: kGray 2: kYUYV 3: BGR 4: BGRA Any other channel
58 * numbers will throw an error. If your image is an in alternate format, use
59 * the overload that takes a PixelFormat.
60 *
61 * @param image OpenCV Image
62 */
63 void PutFrame(cv::Mat& image) {
64 // We only support 8-bit images; convert if necessary.
65 cv::Mat finalImage;
66 if (image.depth() == CV_8U) {
67 finalImage = image;
68 } else {
69 image.convertTo(finalImage, CV_8U);
70 }
71
72 int channels = finalImage.channels();
74 if (channels == 1) {
75 // 1 channel is assumed Grayscale
77 } else if (channels == 2) {
78 // 2 channels is assumed YUYV
80 } else if (channels == 3) {
81 // 3 channels is assumed BGR
83 } else if (channels == 4) {
84 // 4 channels is assumed BGRA
86 } else {
87 // TODO Error
88 return;
89 }
90
91 PutFrame(finalImage, format, true);
92 }
93
94 /**
95 * Put an OpenCV image and notify sinks.
96 *
97 * <p>
98 * The format of the Mat must match the PixelFormat. You will corrupt memory
99 * if they dont. With skipVerification false, we will verify the number of
100 * channels matches the pixel format. If skipVerification is true, this step
101 * is skipped and is passed straight through.
102 *
103 * @param image OpenCV image
104 * @param pixelFormat The pixel format of the image
105 * @param skipVerification skip verifying pixel format
106 */
107 void PutFrame(cv::Mat& image, wpi::util::PixelFormat pixelFormat,
108 bool skipVerification) {
109 // We only support 8-bit images; convert if necessary.
110 cv::Mat finalImage;
111 if (image.depth() == CV_8U) {
112 finalImage = image;
113 } else {
114 image.convertTo(finalImage, CV_8U);
115 }
116
117 if (!skipVerification) {
118 if (!VerifyFormat(finalImage, pixelFormat)) {
119 // TODO Error
120 return;
121 }
122 }
123
124 WPI_RawFrame frame; // use WPI_Frame because we don't want the destructor
125 frame.data = finalImage.data;
126 frame.freeFunc = nullptr;
127 frame.freeCbData = nullptr;
128 frame.size = finalImage.total() * finalImage.channels();
129 frame.width = finalImage.cols;
130 frame.height = finalImage.rows;
131 frame.stride = finalImage.step;
132 frame.pixelFormat = static_cast<int>(pixelFormat);
133 m_status = 0;
135 }
136
137 private:
138 static bool VerifyFormat(cv::Mat& image, wpi::util::PixelFormat pixelFormat) {
139 int channels = image.channels();
140 switch (pixelFormat) {
142 if (channels == 3) {
143 return true;
144 }
145 break;
147 if (channels == 4) {
148 return true;
149 }
150 break;
152 if (channels == 1) {
153 return true;
154 }
155 break;
157 if (channels == 2) {
158 return true;
159 }
160 break;
162 if (channels == 2) {
163 return true;
164 }
165 break;
167 if (channels == 2) {
168 return true;
169 }
170 break;
172 if (channels == 2) {
173 return true;
174 }
175 break;
177 if (channels == 1) {
178 return true;
179 }
180 break;
181 default:
182 break;
183 }
184 return false;
185 }
186};
187
188} // namespace wpi::cs
@ name
Definition base.h:690
CvSource(std::string_view name, const VideoMode &mode)
Create an OpenCV source.
Definition CvSource.hpp:33
void PutFrame(cv::Mat &image)
Put an OpenCV image and notify sinks.
Definition CvSource.hpp:63
void PutFrame(cv::Mat &image, wpi::util::PixelFormat pixelFormat, bool skipVerification)
Put an OpenCV image and notify sinks.
Definition CvSource.hpp:107
CvSource()=default
CvSource(std::string_view name, wpi::util::PixelFormat pixelFormat, int width, int height, int fps)
Create an OpenCV source.
Definition CvSource.hpp:46
CS_Status m_status
Definition VideoSource.hpp:372
CS_Source m_handle
Video source handle.
Definition VideoSource.hpp:375
FMT_INLINE auto format(locale_ref loc, format_string< T... > fmt, T &&... args) -> std::string
Definition format.h:4305
void PutSourceFrame(CS_Source source, const WPI_RawFrame &image, CS_Status *status)
CS_Source CreateRawSource(std::string_view name, bool isCv, const VideoMode &mode, CS_Status *status)
CameraServer (cscore) namespace.
Definition CvSource.hpp:15
PixelFormat
Pixel formats.
Definition PixelFormat.hpp:14
@ kRGB565
Definition PixelFormat.hpp:18
@ kBGRA
Definition PixelFormat.hpp:23
@ kGray
Definition PixelFormat.hpp:20
@ kYUYV
Definition PixelFormat.hpp:17
@ kY16
Definition PixelFormat.hpp:21
@ kMJPEG
Definition PixelFormat.hpp:16
@ kBGR
Definition PixelFormat.hpp:19
@ kUYVY
Definition PixelFormat.hpp:22
Raw Frame.
Definition RawFrame.h:17
int pixelFormat
Definition RawFrame.h:25
void * freeCbData
Definition RawFrame.h:22
void(* freeFunc)(void *cbdata, void *data, size_t capacity)
Definition RawFrame.h:21
uint8_t * data
Definition RawFrame.h:19
size_t size
Definition RawFrame.h:24
int height
Definition RawFrame.h:27
int stride
Definition RawFrame.h:28
int width
Definition RawFrame.h:26
Video mode.
Definition VideoMode.hpp:15