I have an issue when reusing libcamera by only releasing it and then aquire it again, the control options does not work well(its way off) and the image gets blurry and noisy. But if I stop the camera manager and destroy it with the destructor and then start it all up again the control options are working perfectly fine and the images are way clearer and sharper. The downside to this is that it increases the time per shot and takes way to long compared to only releasing the libcamera and aquiring it.
I suspect I either are deleting/nulling/resetting something in the wrong order or its something wrong with libcamera api.
LIBCAMERA VERSION: libcamera v0.1.0+118-563cd78e
This is my code:
I suspect I either are deleting/nulling/resetting something in the wrong order or its something wrong with libcamera api.
LIBCAMERA VERSION: libcamera v0.1.0+118-563cd78e
This is my code:
Code:
#include <iostream>#include "mapped_framebuffer.h"#include <libcamera/libcamera.h>#include "event_loop.h"#include <thread>#include <opencv2/core.hpp>#include <opencv2/imgproc.hpp>#include <opencv2/highgui.hpp>#include <string>#include <stdlib.h>#include <fstream>#include <memory>#include <chrono>#include <iomanip>using namespace std;using namespace libcamera;static EventLoop loop;static void cameraFunction();int LoopCount = 0;int l = 0; std::shared_ptr<Camera> libbcamera;std::unique_ptr<std::thread> aThread;std::vector<std::unique_ptr<Request>> requests;std::unique_ptr<CameraManager> camera_manager;Stream *stream;FrameBufferAllocator *frameBufferAllocator;std::unique_ptr<CameraConfiguration> config; //const Request::BufferMap *buffers;float brightness = 0;float saturation = 0;float contrast = 0;float lens_position = 0;int exposure_time = 0; int d = 0;float cg1 = 0;float cg2 = 0;int main(int argc, char *argv[]){camera_manager = std::make_unique<CameraManager>();camera_manager->start();std::string cameraId = camera_manager->cameras()[0]->id();libbcamera = camera_manager->get(cameraId);auto start = std::chrono::steady_clock::now();while (LoopCount < 3) {cout << LoopCount << "\n";cameraFunction();LoopCount++;}//libbcamera->release();auto end = std::chrono::steady_clock::now();auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);std::cout << "Time taken: " << duration.count() << " milliseconds" << std::endl;libbcamera.reset();libbcamera = nullptr;camera_manager->stop();camera_manager->~CameraManager();return 0;}static void processRequest(Request *request){std::cout << "Completed " << (void *)request << std::endl;const Request::BufferMap &buffers = request->buffers();for (auto bufferPair : buffers){const Stream *stream = bufferPair.first;FrameBuffer *buffer = bufferPair.second;auto cfg = stream->configuration();unsigned int width = cfg.size.width;unsigned int height = cfg.size.height;int* random = new int;*random = 1;int count;libcamera::MappedFrameBuffer mappedBuffer(buffer, MappedFrameBuffer::MapFlag::Read);const std::vector<libcamera::Span<uint8_t>> mem = mappedBuffer.planes();cv::Mat image(height, width, CV_8UC4, (uint8_t*)(mem[0].data()));cv::Mat image_3_channels;cv::cvtColor(image, image_3_channels, cv::COLOR_RGBA2RGB);std::string filename = "/root/WebservTest/image_" + std::to_string(LoopCount) + ".jpg";cv::imwrite(filename, image_3_channels);delete random;request = nullptr;stream = nullptr;buffer = nullptr;}delete request;loop.exit();}static void cameraFunction(){//camera_manager = std::make_unique<CameraManager>();//camera_manager->start();//std::string cameraId = camera_manager->cameras()[0]->id();//libbcamera = camera_manager->get(cameraId);libbcamera->acquire();switch (LoopCount) {case 0://1STbrightness = 0.01;saturation = 0.5;contrast = 1.1;lens_position = 7.55;exposure_time = 17500;cg1 = 0.95f;cg2 = 0.9f;break;case 1://2NDbrightness = 0.038; // 0.038saturation = 0.5;contrast = 1.1;lens_position = 7.55;exposure_time = 1800; //1800cg1 = 1.12f; //1.02cg2 = 1.22f; //1.22break;case 2://3RDbrightness = 0.05;saturation = 0.8;contrast = 1.1;lens_position = 7.55;exposure_time = 20000;cg1 = 1.45f;cg2 = 1.9f;break;}config = libbcamera->generateConfiguration({ StreamRole::Viewfinder });StreamConfiguration &streamConfig = config->at(0);streamConfig.size.width = 2592; streamConfig.size.height = 1944;streamConfig.bufferCount = 1;config->validate();libbcamera->configure(config.get());frameBufferAllocator = new FrameBufferAllocator(libbcamera);for (StreamConfiguration &cfg : *config) {int ret = frameBufferAllocator->allocate(cfg.stream());size_t allocated = frameBufferAllocator->buffers(cfg.stream()).size();std::cout << "Allocated " << allocated << " buffers for stream" << std::endl;}Stream *stream = streamConfig.stream();const std::vector<std::unique_ptr<FrameBuffer>> &buffers = frameBufferAllocator->buffers(stream); std::unique_ptr<Request> request = libbcamera->createRequest();const std::unique_ptr<FrameBuffer> &buffer = buffers[0];int ret = request->addBuffer(stream, buffer.get());ControlList &controls = request->controls();controls.set(controls::Brightness, brightness);controls.set(controls::Saturation, saturation);controls.set(controls::Contrast, contrast);controls.set(controls::AfMode, controls::AfModeManual);controls.set(controls::AwbMode, 0);controls.set(controls::ColourGains, Span<const float, 2>({ cg1, cg2 }));controls.set(controls::LensPosition, lens_position);controls.set(controls::ExposureTime, exposure_time);libbcamera->requestCompleted.connect(processRequest);libbcamera->start();aThread = std::make_unique<std::thread>([&]() { loop.exec(); });libbcamera->queueRequest(request.get());aThread->join();libbcamera->requestCompleted.disconnect(processRequest);request = nullptr;int* random = new int;*random = 2;frameBufferAllocator->free(stream);delete frameBufferAllocator;ret = 0;stream = nullptr;aThread = 0;config.release();config.reset();libbcamera->stop();libbcamera->release();//libbcamera.reset();//libbcamera = nullptr;//camera_manager->stop();//camera_manager->~CameraManager();}
Statistics: Posted by catlord34 — Mon Apr 29, 2024 9:41 am