IRSOL
C++ code implementing socket server for interacting with Baumer camera.
main-opencv.cpp File Reference

Example application using the irsol library to initialize logging, connect to a camera, capturing a sequence of images and displaying them. More...

#include "irsol/irsol.hpp"
#include <chrono>
#include <opencv2/opencv.hpp>
#include <unordered_map>

Go to the source code of this file.

Classes

struct  Params
 Command-line parameters for the program. More...
 

Macros

#define PROGRAM_NAME   "loading-images-opencv-demo"
 

Functions

const std::string getProgramName ()
 Returns the program name, typically used for logging. If PROGRAM_NAME is not defined at compile time, returns "loading-images-opencv-demo".
 
Params getParams (int argc, char **argv)
 Parses command-line arguments using args library.
 
void runCapture (irsol::camera::Interface &cam)
 Captures a single image using the provided camera interface, logs metadata and performance stats.
 
int main (int argc, char **argv)
 Program entrypoint.
 

Detailed Description

Example application using the irsol library to initialize logging, connect to a camera, capturing a sequence of images and displaying them.

This example is only build when opencv is found by the build toolchain.

This program serves as a minimal working example for new developers to understand how to interact with the core components of the irsol library. It demonstrates:

  • Command-line argument parsing
  • Logging setup and log level configuration
  • Camera interface initialization
  • Repeated image acquisition with basic performance metrics

Build system integration is expected to define PROGRAM_NAME for logging.

Definition in file main-opencv.cpp.

Macro Definition Documentation

◆ PROGRAM_NAME

#define PROGRAM_NAME   "loading-images-opencv-demo"

Function Documentation

◆ getParams()

Params getParams ( int  argc,
char **  argv 
)

Parses command-line arguments using args library.

Supported arguments:

  • --log-level, -l: Set logging level (trace, debug, info, warn, error)
  • --frames-count, -f: Set number of frames to capture
  • --help, -h: Show help message and exit
Parameters
argcArgument count
argvArgument vector
Returns
Filled Params struct

Definition at line 57 of file main-opencv.cpp.

58{
59 args::ArgumentParser parser(getProgramName());
60 args::HelpFlag help(parser, "help", "Display this help menu", {'h', "help"});
61
62 args::MapFlag<std::string, spdlog::level::level_enum> logLevelArg(
63 parser, "level", "Log level", {'l', "log-level"}, irsol::levelNameToLevelMap);
64
65 args::ValueFlag<uint64_t> numFramesArg(
66 parser, "#frames", "Number of frames to iterate over", {'n', "frames-count"});
67
68 args::ValueFlag<double> fpsArg(parser, "fps", "Desired FPS for capturing image", {'f', "fps"});
69
70 try {
71 parser.ParseCLI(argc, argv);
72 } catch(args::Help) {
73 std::cerr << parser.Help() << std::endl;
74 std::exit(0);
75 } catch(args::ParseError e) {
76 std::cerr << "Error parsing command-line arguments:\n" << e.what() << "\n\n" << parser.Help();
77 std::exit(1);
78 }
79
80 Params params{};
81 if(logLevelArg) {
82 params.logLevel = args::get(logLevelArg);
83 }
84 if(numFramesArg) {
85 params.numFrames = args::get(numFramesArg);
86 }
87 if(fpsArg) {
88 params.fps = args::get(fpsArg);
89 }
90 return params;
91}
const std::unordered_map< std::string, spdlog::level::level_enum > levelNameToLevelMap
Runtime map from string names to spdlog log levels.
Definition logging.hpp:254
const std::string getProgramName()
Returns the program name, typically used for logging. If PROGRAM_NAME is not defined at compile time,...
Command-line parameters for the program.
spdlog::level::level_enum logLevel
Logging verbosity level (default: info)

◆ getProgramName()

const std::string getProgramName ( )

Returns the program name, typically used for logging. If PROGRAM_NAME is not defined at compile time, returns "loading-images-opencv-demo".

Definition at line 25 of file main-opencv.cpp.

26{
27#ifndef PROGRAM_NAME
28#define PROGRAM_NAME "loading-images-opencv-demo"
29#endif
30 return PROGRAM_NAME;
31}
#define PROGRAM_NAME

◆ main()

int main ( int  argc,
char **  argv 
)

Program entrypoint.

  • Initializes logging and assertions
  • Selects camera resolution based on build mode
  • Captures a user-defined number of frames
  • Logs image statistics and performance

Definition at line 120 of file main-opencv.cpp.

121{
122 // Parse command-line parameters
123 auto params = getParams(argc, argv);
124
125 // Construct log file path based on program name
126 std::string logPath = "logs/" + getProgramName() + ".log";
127 irsol::initLogging(logPath.c_str(), params.logLevel);
128
129 // Enable custom assertion handler
131
132 // Choose camera resolution based on build type
133#ifdef DEBUG
134 auto cameraInterface = irsol::camera::Interface::FullResolution();
135#else
136 auto cameraInterface = irsol::camera::Interface::HalfResolution();
137#endif
138
139 // Ensure the camera is connected before continuing
140 IRSOL_ASSERT_FATAL(cameraInterface.isConnected(), "Camera is not connected");
141
142 // Log camera metadata for debugging purposes
143 IRSOL_LOG_INFO("\n{}", cameraInterface.cameraInfoAsString());
144 IRSOL_LOG_INFO("\n{}", cameraInterface.cameraStatusAsString());
145
146 // Start a monitoring thread to log camera status
147 irsol::camera::StatusMonitor monitor(cameraInterface, std::chrono::milliseconds(500));
148 monitor.start();
149
150 // Capture and log a sequence of frames
151 auto lastTick = irsol::types::clock_t::now();
152 const auto desiredDt =
153 params.fps > 0.0 ? std::chrono::microseconds(static_cast<uint64_t>(1000000.0 / params.fps))
154 : std::chrono::microseconds(0);
155 for(size_t i = 0; i < params.numFrames; ++i) {
156 const auto t0 = irsol::types::clock_t::now();
157 runCapture(cameraInterface);
158 const auto nextTick = lastTick + desiredDt;
159 lastTick = nextTick;
160 std::this_thread::sleep_until(nextTick);
161 const auto loopDuration = irsol::types::clock_t::now() - t0;
162 const auto measuredFps =
163 1000000.0 / std::chrono::duration_cast<std::chrono::microseconds>(loopDuration).count();
164 IRSOL_LOG_INFO("Desired FPS: {:.4f}, measured FPS: {:.4f}", params.fps, measuredFps);
165 }
166
167 return 0;
168}
static Interface HalfResolution()
Factory method to create a camera interface using half sensor resolution.
Definition interface.cpp:63
static Interface FullResolution()
Factory method to create a camera interface using full sensor resolution.
Definition interface.cpp:49
Class that offers an easy way to run a background monitorin thread that periodically inspects and rep...
Definition monitor.hpp:44
void initAssertHandler()
Initializes the assertion handler system.
Definition assert.cpp:14
#define IRSOL_ASSERT_FATAL
Fatal-level assertion macro.
Definition assert.hpp:135
#define IRSOL_LOG_INFO(...)
Logs an info-level message using the default logger.
Definition logging.hpp:92
void initLogging(const char *fileSinkFilename="logs/irsol.log", std::optional< spdlog::level::level_enum > minLogLevel=std::nullopt)
Initializes the irsol logging system.
Definition logging.cpp:108
Params getParams(int argc, char **argv)
Parses command-line arguments using args library.
void runCapture(irsol::camera::Interface &cam)
Captures a single image using the provided camera interface, logs metadata and performance stats.

◆ runCapture()

void runCapture ( irsol::camera::Interface cam)

Captures a single image using the provided camera interface, logs metadata and performance stats.

Parameters
camReference to an initialized irsol::camera::Interface instance.

Definition at line 98 of file main-opencv.cpp.

99{
100 auto image = cam.captureImage();
101
102 if(image.IsEmpty()) {
103 IRSOL_LOG_WARN("Image is empty.");
104 }
105
106 auto cvMat = irsol::opencv::convertNeoImageToCvMat(image);
107
108 IRSOL_LOG_INFO("{}x{}, {} bytes", image.GetHeight(), image.GetWidth(), image.GetSize());
109 cv::imshow(getProgramName(), cvMat);
110 cv::waitKey(1);
111}
image_t captureImage(std::optional< irsol::types::duration_t > timeout=std::nullopt)
Capture a single image from the camera.
#define IRSOL_LOG_WARN(...)
Logs a warning-level message using the default logger.
Definition logging.hpp:93
cv::Mat convertNeoImageToCvMat(const NeoAPI::Image &image)
Converts a NeoAPI::Image (Mono12, non-packed) into an OpenCV cv::Mat.
Definition opencv.cpp:12