I modified Edwards example to save 5 images in now. they all turn out to be the same first image altough i move camera in between. Can you tell me why this happens and how to fix it?
Example code is here GitHub - edward-ardu/libcamera-cpp-demo at c4179dc0c912870d63484464273ed87077a67395
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <unistd.h>
#include "LibCamera.h"
#include <string.h>
using namespace cv;
int main() {
time_t start_time = time(0);
int frame_count = 0;
float lens_position = 100;
float focus_step = 50;
LibCamera cam;
uint32_t width = 1280;
uint32_t height = 720;
uint32_t stride;
char key;
int window_width = 1920;
int window_height = 1080;
if (width > window_width)
{
// cv::namedWindow("libcamera-demo", cv::WINDOW_NORMAL);
// cv::resizeWindow("libcamera-demo", window_width, window_height);
}
int ret = cam.initCamera(0);
cam.configureStill(width, height, formats::RGB888, 1, 0);
ControlList controls_;
int64_t frame_time = 1000000 / 10;
// Set frame rate
controls_.set(controls::FrameDurationLimits, libcamera::Span<const int64_t, 2>({ frame_time, frame_time }));
// Adjust the brightness of the output images, in the range -1.0 to 1.0
controls_.set(controls::Brightness, 0.5);
// Adjust the contrast of the output image, where 1.0 = normal contrast
controls_.set(controls::Contrast, 1.5);
// Set the exposure time
controls_.set(controls::ExposureTime, 20000);
cam.set(controls_);
if (!ret) {
bool flag;
LibcameraOutData frameData;
cam.startCamera();
cam.VideoStream(&width, &height, &stride);
int i = 1;
int max =5;
while (i<=max) {
flag = cam.readFrame(&frameData);
if (!flag)
continue;
Mat im(height, width, CV_8UC3, frameData.imageData, stride);
imwrite(std::to_string(i)+".jpg", im);
//imshow("libcamera-demo", im);
key = waitKey(1);
if (key == 'q') {
break;
} else if (key == 'f') {
ControlList controls;
controls.set(controls::AfMode, controls::AfModeAuto);
controls.set(controls::AfTrigger, 0);
cam.set(controls);
} else if (key == 'a' || key == 'A') {
lens_position += focus_step;
} else if (key == 'd' || key == 'D') {
lens_position -= focus_step;
}
// To use the manual focus function, libcamera-dev needs to be updated to version 0.0.10 and above.
if (key == 'a' || key == 'A' || key == 'd' || key == 'D') {
ControlList controls;
controls.set(controls::AfMode, controls::AfModeManual);
controls.set(controls::LensPosition, lens_position);
cam.set(controls);
}
frame_count++;
if ((time(0) - start_time) >= 1){
printf("fps: %d\n", frame_count);
frame_count = 0;
start_time = time(0);
}
printf("move camera now");
sleep(5);
cam.returnFrameBuffer(frameData);
i++;
}
destroyAllWindows();
cam.stopCamera();
}
cam.closeCamera();
return 0;
}```
I stumbled onto this issue when integrated libcamera to my app based on this code. So for temporary fix i created such counter as i noticed after reading 4 frames i get proper image. this fixes the issue but i think adds extra latency especially with larger resolutions and i would like to get rid of it. if i jsut comment out the counter then what happens is that the second image i capture is copy of first image and third is copy of second image etc
cam.returnFrameBuffer(frameData);
if(counter>4){
QImage image(frameData.imageData, width, height, stride, QImage::Format_RGB888);
QPixmap pixmap = QPixmap::fromImage(image);
// Save the frame as a JPEG image
QString filename = target->getPath();
QFile file(filename);
if (file.open(QIODevice::WriteOnly)) {
QByteArray byteArray;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::WriteOnly);
pixmap.save(&buffer, "JPEG");
file.write(byteArray);
file.close();
}
break;
}
counter++;