arduino-audio-tools
Loading...
Searching...
No Matches
OutputOpenCV.h
Go to the documentation of this file.
1#pragma once
2
4#include <opencv2/core/core.hpp>
5#include <opencv2/highgui/highgui.hpp>
6#include <opencv2/imgproc/imgproc.hpp>
7
8namespace audio_tools {
9
23class OutputOpenCV : public VideoOutput {
24public:
25 OutputOpenCV() = default;
26
28 void setVideoFormat(VideoFormat format) { video_format = format; }
29
32 void setSize(uint16_t width, uint16_t height) {
33 this->width = width;
34 this->height = height;
35 }
36
37 size_t write(const uint8_t *data, size_t len) override {
40 displayRaw(data, len);
41 return len;
42 }
43 // Accumulates jpeg bytes for the frame currently being assembled
44 if (pos == 0) start = millis();
45 // prevent memory fragmentation, change size only if more memory is needed
46 if (img_vector.size() < pos + len) {
47 img_vector.resize(pos + len);
48 }
49 memcpy(&img_vector[pos], data, len);
50 pos += len;
51 return len;
52 }
53
58 void flush() override {
59 if (pos == 0) return;
61 pos = 0;
62 }
63
64protected:
66 uint16_t width = 0;
67 uint16_t height = 0;
68 bool create_window = true;
69 std::vector<uint8_t> img_vector;
70 const char *window = "Movie";
71 size_t pos = 0;
72 uint64_t start = 0;
73
74 void ensureWindow() {
75 if (create_window) {
76 create_window = false;
77 // create image window named "My Image"
78 cv::namedWindow(window);
79 }
80 }
81
82 void displayJpeg() {
83 cv::Mat data(1, pos, CV_8UC1, (void *)&img_vector[0]);
84 // cv::InputArray input_array(img_vector);
85 cv::Mat mat = cv::imdecode(data, 0);
86 cv::imshow(window, mat);
87 // cv::pollKey();
88 cv::waitKey(1);
89 }
90
95 void displayRaw(const uint8_t *data, size_t len) {
96 if (width == 0 || height == 0) {
97 LOGE("OutputOpenCV: setSize() was not called");
98 return;
99 }
100 cv::Mat bgr;
101 switch (video_format) {
102 case VideoFormat::RGB565: {
103 cv::Mat raw(height, width, CV_8UC2, (void *)data);
104 cv::cvtColor(raw, bgr, cv::COLOR_BGR5652BGR);
105 break;
106 }
108 case VideoFormat::RGB666: {
109 // RGB666's low 2 bits/channel are unused (left-justified in each
110 // byte, see VideoFormat) - same 3-bytes/pixel memory layout as
111 // RGB888 otherwise, so displays fine at slightly reduced precision
112 cv::Mat raw(height, width, CV_8UC3, (void *)data);
113 cv::cvtColor(raw, bgr, cv::COLOR_RGB2BGR);
114 break;
115 }
116 case VideoFormat::YUV422: {
117 cv::Mat raw(height, width, CV_8UC2, (void *)data);
118 cv::cvtColor(raw, bgr, cv::COLOR_YUV2BGR_YUYV);
119 break;
120 }
121 case VideoFormat::I420: {
122 cv::Mat raw(height + height / 2, width, CV_8UC1, (void *)data);
123 cv::cvtColor(raw, bgr, cv::COLOR_YUV2BGR_I420);
124 break;
125 }
126 default:
127 LOGE("OutputOpenCV: unsupported VideoFormat %d", (int)video_format);
128 return;
129 }
130 cv::imshow(window, bgr);
131 cv::waitKey(1);
132 }
133};
134
135} // namespace audio_tools
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Display a video frame with OpenCV, to be used on the desktop - VideoFormat::MJPEG (the default) expec...
Definition OutputOpenCV.h:23
bool create_window
Definition OutputOpenCV.h:68
void flush() override
Definition OutputOpenCV.h:58
void ensureWindow()
Definition OutputOpenCV.h:74
const char * window
Definition OutputOpenCV.h:70
size_t pos
Definition OutputOpenCV.h:71
void displayJpeg()
Definition OutputOpenCV.h:82
void setSize(uint16_t width, uint16_t height)
Definition OutputOpenCV.h:32
uint64_t start
Definition OutputOpenCV.h:72
size_t write(const uint8_t *data, size_t len) override
Definition OutputOpenCV.h:37
VideoFormat video_format
Definition OutputOpenCV.h:65
uint16_t height
Definition OutputOpenCV.h:67
std::vector< uint8_t > img_vector
Definition OutputOpenCV.h:69
void setVideoFormat(VideoFormat format)
Selects the format of the data passed to write() - see class comment.
Definition OutputOpenCV.h:28
uint16_t width
Definition OutputOpenCV.h:66
void displayRaw(const uint8_t *data, size_t len)
Definition OutputOpenCV.h:95
Abstract class for video playback. This class is used to assemble a complete video frame in memory....
Definition Video.h:192
VideoFormat
Video codec/pixel-format identifier, shared by two unrelated uses: the (single) video stream of a con...
Definition Video.h:43
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
uint32_t millis()
Returns the milliseconds since the start.
Definition Arduino.h:260