Same picture being saved eventhough i move camera

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++;

looked into it a little and found this thread

“This is beyond the scope of our support. Regarding software implementation, you need to explore it yourself” This is from 8 years ago.

I could really use a little support here. Any hints what to look at. I mean anything.

@henri

I tested the program you wrote, I did not reproduce the problem you said, I can get 5 different images, can you record a video of your operation for me to see.

video is here

Whats your hardware/software where you got 5 different images?

In my setup i have RPI4, CM4. 32-bit raspberry os 6.1xx kernel. 2 ports for cameras. in one i currently have a quad hat connected with 2 cameras and in the second one i have a single 64mp camera. Could this somehow influence that i have 2 cameras connected? I did double check that im moving the camera i am making pictures with. you can see it in the video as it is first pointed to monitor.

@henri

In theory, the two camera interfaces do not interfere with each other

I used pi4, I will test it again and let you know the result.

@henri
Try this:

#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);
    time_t cap_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();
    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);
            if (time(0) - cap_time > 5) {
                imwrite(std::to_string(i)+".jpg", im);
                cap_time = time(0);
                printf("move camera now");
                i++;
            }
            // 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);
            }
            cam.returnFrameBuffer(frameData);
        }
        destroyAllWindows();
        cam.stopCamera();
    }
    cam.closeCamera();
    
    return 0;
}

yes this is fine. only way to resolve this is to constantly keep reading the frame data and then save the image when necessary.