MXVK Vulkan Framework 0.33.1
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
opencv_ex.cpp File Reference
#include <chrono>
#include <iostream>
#include <mxwrite.hpp>
#include <opencv2/opencv.hpp>
#include <thread>

Go to the source code of this file.

Functions

int main (int argc, char **argv)

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 7 of file opencv_ex.cpp.

7 {
8 if (argc != 3) {
9 std::cerr << "Usage: " << argv[0] << " camera index mode\n0 mode for normal mode, 1, for timestamp mode\n";
10 return 1;
11 }
12 int index = std::stoi(argv[1]);
13 int mode = std::stoi(argv[2]);
14
15 if (mode != 0 && mode != 1) {
16 std::cerr << "Invalid mode..\n";
17 return 1;
18 }
19
20 std::cout << "Initializing Camera: " << index << " mode: " << mode << "...\n";
21#ifdef _WIN32
22 cv::VideoCapture cap(index, cv::CAP_DSHOW);
23#else
24 cv::VideoCapture cap(index);
25#endif
26 if (!cap.isOpened()) {
27 std::cerr << "Failed to open camera\n";
28 return 1;
29 }
30 double fps = cap.get(cv::CAP_PROP_FPS);
31 if (fps <= 0)
32 fps = 30.0;
33
34 int width = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH));
35 int height = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_HEIGHT));
36
37 std::cout << "Camera settings: " << width << "x" << height << " @ " << fps << "fps\n";
38
39 Writer writer;
40 bool status;
41
42 if (mode == 0)
43 status = writer.open("output.mp4", width, height, fps, "24");
44 else if (mode == 1)
45 status = writer.open_ts("output.mp4", width, height, fps, "24");
46
47 if (!status) {
48 std::cerr << "Failed to open file for writing\n";
49 return 1;
50 }
51 cv::namedWindow("Webcam", cv::WINDOW_AUTOSIZE);
52 cv::Mat frame;
53 bool active = true;
54 while (active) {
55 if (cap.read(frame)) {
56 cv::Mat temp;
57 cv::cvtColor(frame, temp, cv::COLOR_BGR2RGBA);
58 if (mode == 0)
59 writer.write(temp.ptr());
60 else if (mode == 1)
61 writer.write_ts(temp.ptr());
62
63 cv::imshow("Webcam", frame);
64 }
65 if (cv::waitKey(1) == 27) {
66 active = false;
67 }
68 }
69 writer.close();
70 cap.release();
71 cv::destroyAllWindows();
72 return 0;
73}
FFmpeg-backed RGBA video writer.
Definition mxwrite.hpp:139
bool open(const std::string &filename, int width, int height, float fps, const char *crf)
Open an output file using the legacy CRF string interface.
Definition mxwrite.cpp:1039
void write(void *rgba_buffer)
Queue a host RGBA frame for immediate-mode encoding.
Definition mxwrite.cpp:1859
bool open_ts(const std::string &filename, int width, int height, float fps, const char *crf)
Open a timestamp-based output stream using the legacy CRF string interface.
Definition mxwrite.cpp:1060
void close()
Close the writer and flush pending packets.
Definition mxwrite.cpp:2425
void write_ts(void *rgba_buffer)
Queue a host RGBA frame using capture timestamps.
Definition mxwrite.cpp:2211
std::atomic< bool > active
Definition relay.cpp:12