libcudaisp  0.1.4
cudawhitebalancer_example.cpp

This illustrates the white balancer usage. To run the example, please, pass the following arguments:

1: the input image (raw bgr)

2: the output image (raw bgr)

3: width

4: height

/*
* Copyright (C) 2023 RidgeRun, LLC (http://www.ridgerun.com)
* All Rights Reserved.
*
* The contents of this software are proprietary and confidential to RidgeRun,
* LLC. No part of this program may be photocopied, reproduced or translated
* into another programming language without prior written consent of
* RidgeRun, LLC. The user is free to modify the source code after obtaining
* a software license from RidgeRun. All source code changes must be provided
* back to RidgeRun without any encumbrance.
*/
#include "framereader.hpp" /* NOLINT */
#include <iostream>
#include <libcudaisp/cudabufferparams.hpp>
#include <libcudaisp/ibackend.hpp>
#include <libcudaisp/whitebalanceparams.hpp>
#include <memory>
#include <string>
int main(int argc, char **argv) {
bool ret = true;
if (argc != 5) {
std::cerr << "Error: requires 4 arguments"
<< "\tinput_path output_path width height\n"
<< std::endl;
return -1;
}
/* Configuring the image */
int buffer_width = atoi(argv[3]);
int buffer_height = atoi(argv[4]);
rr::Format input_buffer_format = rr::Format::kBGR;
rr::Format output_buffer_format = rr::Format::kBGR;
const int num_bgr_channels = 3;
rr::RuntimeError runtimeerror;
int input_buffer_planes = 1;
std::array<int, rr::Buffer::kMaxPlanes> input_buffer_strides = {
num_bgr_channels * buffer_width, 0, 0, 0};
size_t input_buffer_size = num_bgr_channels * buffer_height * buffer_width;
int output_buffer_planes = 1;
std::array<int, rr::Buffer::kMaxPlanes> output_buffer_strides = {
num_bgr_channels * buffer_width, 0, 0, 0};
size_t output_buffer_size = num_bgr_channels * buffer_height * buffer_width;
/* Create backend, algorithms and params */
auto isp_backend = rr::IBackend::Build(rr::Backends::kCUDA, nullptr);
auto memory_params = std::make_shared<rr::CudaBufferParams>();
memory_params->type = rr::CudaType::kUnified;
/* Create buffers */
auto input_buffer =
isp_backend->CreateBuffer(input_buffer_size, memory_params);
auto output_buffer =
isp_backend->CreateBuffer(output_buffer_size, memory_params);
/* Format buffers */
rrerr = input_buffer->SetBufferInfo(input_buffer_format, input_buffer_planes,
input_buffer_strides, buffer_width,
buffer_height);
if (rrerr.IsError()) {
std::cerr << "Error allocating the input buffer: " << rrerr.GetDescription()
<< std::endl;
return -1;
}
rrerr = output_buffer->SetBufferInfo(
output_buffer_format, output_buffer_planes, output_buffer_strides,
buffer_width, buffer_height);
if (rrerr.IsError()) {
std::cerr << "Error allocating the output buffer: "
<< rrerr.GetDescription() << std::endl;
return -1;
}
ret = ReadFrame(argv[1], input_buffer->data(), input_buffer_size);
if (!ret) {
std::cerr << "Cannot read frame" << std::endl;
return -1;
}
/* Apply the white balancing algorithm */
auto whitebalancer =
isp_backend->CreateAlgorithm(rr::Algorithms::kWhiteBalancer, nullptr);
auto params = std::make_shared<rr::WhiteBalanceParams>();
params->quality_factor = 0.9;
rrerr = whitebalancer->Apply(input_buffer.get(), output_buffer.get(), params,
false);
if (rrerr.IsError()) {
std::cerr << "Error while applying white balancing: " +
<< std::endl;
return -1;
}
/* Synchronize the execution */
rrerr = isp_backend->Synchronize();
if (rrerr.IsError()) {
std::cerr << "Error while synchronizing: " + rrerr.GetDescription()
<< std::endl;
return -1;
}
/* Write result to file */
ret = WriteFrame(argv[2], output_buffer->data(), output_buffer_size);
if (!ret) {
std::cerr << "Cannot write frame" << std::endl;
return -1;
}
return 0;
}
rr::Backends::kCUDA
@ kCUDA
CUDA backend.
rr::RuntimeError
Implements the error handling class.
Definition: runtimeerror.hpp:27
rr::Algorithms::kWhiteBalancer
@ kWhiteBalancer
WhiteBalancer algorithm.
rr::Format::kBGR
@ kBGR
BGR image format.
rr::RuntimeError::GetDescription
const std::string GetDescription() const
Returns a human readable description of the error.
Definition: runtimeerror.cpp:31
rr::CudaType::kUnified
@ kUnified
Unified memory.
rr::WhiteBalanceParams::kHistogramStretch
@ kHistogramStretch
Histogram Stretch White Balancing algorithm.
Definition: whitebalanceparams.hpp:52
rr::IBackend::Build
static std::shared_ptr< IBackend > Build(const Backends backend, const std::shared_ptr< IParams > params)
This method construct the backend objects.
Definition: ibackend.cpp:24
rr::RuntimeError::IsError
bool IsError() const
Checks if the RuntimeError is in an error state.
Definition: runtimeerror.cpp:37
rr::Format
Format
This class contains different image formats.
Definition: format.hpp:24