Issue in Installing pre-compiled driver for pivariety camera B0323 imx298

I have changed CMakeList.txt file to specify Opencv Path and it successfully installed .

Dear @Wong Thank for your support.

I have successfully integrated libcamera-apps opencv with my application and started to grab image continuously in cv::Mat but grab image with button click it grabs buffer image .
how to reduce buffer size of an image garbing in libcamera-app opencv .

You can try to modify this:
libcamera_app.cpp:
image

By the way, the current program is just a simple modification on libcamera_app, and there are many redundant things we did not delete. We plan to make a simpler and clearer example, but it will take some time.

Sir , I have also changed bufferCount in libcamera_apps as mentioned above , but that does not give expected result .it still gives buffer Image(for First 8 trigger ,it gives past Image and for 9th trigger gives current Image).Is that I need modify BufferCount in libcamera-apps opencv before CMake. Modified ConfigureViewfinder() function as below

void LibcameraApp::ConfigureViewfinder()
{
	if (options_->verbose)
		std::cerr << "Configuring viewfinder..." << std::endl;

	bool have_lores_stream = options_->lores_width && options_->lores_height;
	StreamRoles stream_roles = { StreamRole::Viewfinder };
	if (have_lores_stream)
		stream_roles.push_back(StreamRole::Viewfinder);
	configuration_ = camera_->generateConfiguration(stream_roles);
	if (!configuration_)
		throw std::runtime_error("failed to generate viewfinder configuration");

	Size size(1280, 960);
	if (options_->viewfinder_width && options_->viewfinder_height)
		size = Size(options_->viewfinder_width, options_->viewfinder_height);
	else if (camera_->properties().contains(properties::PixelArrayActiveAreas))
	{
		// The idea here is that most sensors will have a 2x2 binned mode that
		// we can pick up. If it doesn't, well, you can always specify the size
		// you want exactly with the viewfinder_width/height options_->
		size = camera_->properties().get(properties::PixelArrayActiveAreas)[0].size() / 2;
		// If width and height were given, we might be switching to capture
		// afterwards - so try to match the field of view.
		if (options_->width && options_->height)
			size = size.boundedToAspectRatio(Size(options_->width, options_->height));
		size.alignDownTo(2, 2); // YUV420 will want to be even
		if (options_->verbose)
			std::cerr << "Viewfinder size chosen is " << size.toString() << std::endl;
	}

	// Finally trim the image size to the largest that the preview can handle.
	Size max_size;
	preview_->MaxImageSize(max_size.width, max_size.height);
	if (max_size.width && max_size.height)
	{
		size.boundTo(max_size.boundedToAspectRatio(size)).alignDownTo(2, 2);
		if (options_->verbose)
			std::cerr << "Final viewfinder size is " << size.toString() << std::endl;
	}

	// Now we get to override any of the default settings from the options_->
	// configuration_->at(0).pixelFormat = libcamera::formats::YUV420;
	configuration_->at(0).pixelFormat = libcamera::formats::RGB888;
	configuration_->at(0).size = size;
	configuration_->at(0).bufferCount = 1; //buffer is modifed by balaji 
	if (have_lores_stream)
	{
		Size lores_size(options_->lores_width, options_->lores_height);
		lores_size.alignDownTo(2, 2);
		if (lores_size.width > size.width || lores_size.height > size.height)
			throw std::runtime_error("Low res image larger than viewfinder");
		configuration_->at(1).pixelFormat = libcamera::formats::YUV420;
		configuration_->at(1).size = lores_size;
		configuration_->at(1).bufferCount = configuration_->at(0).bufferCount;
	}

	configuration_->transform = options_->transform;

	post_processor_.AdjustConfig("viewfinder", &configuration_->at(0));

	configureDenoise(options_->denoise == "auto" ? "cdn_off" : options_->denoise);
	setupCapture();

	streams_["viewfinder"] = configuration_->at(0).stream();
	if (have_lores_stream)
		streams_["lores"] = configuration_->at(1).stream();

	post_processor_.Configure();

	if (options_->verbose)
		std::cerr << "Viewfinder setup complete" << std::endl;
}

If I am Wrong please guide me to get expected output.

Sir , I Modified libcamera-apps opencv before cmake and then installed given data using cmake .now I am still getting buffer image but in reduced manner (for First 2 trigger gives past image and for 3rd trigger gives current image).if I tried to set BufferCount 0 (or) lessthen zero it ends up with error like “failed to allocate capture buffers”. the modified ConfigureViewfinder() function as same as previous reply.
I expected to get current image from camera by button click with minimal grabbing time .is that possible to achieve expected result with libcamera-apps opencv .

I think this can be achieved through software.You can make the acquisition thread work all the time, return the buffer without processing when it is not triggered, and process the data only when you trigger it.

Sir,
I am also tried to set width and height of viewfinder by following code

    Options *options = app.GetOptions();
    options->viewfinder_width = 640;
    options->viewfinder_height = 480;
    //by setting lower resolution to enhance speed  

it gives frame with size 640,480,but converted BGR to RGB image in open_cv is again converted from cv::Mat to Qimage for display image in PaintEvent which gives partial RGB and BGR colored one ,but without setting width and height for viewfinder (it gives 2328 X 1748 resolution)the conversion of BGR to RGB works fine and by saving both cv::Mat and QImage work fine but by display Qimage it show strange image like above said,
`

cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB);//conversion of BGR to RGB in cv::Mat
    cv::imwrite("/home/pi/sample.jpeg",frame); //save cv::Mat
const QImage image((const unsigned char*)frame.data, frame.cols, frame.rows, frame.step,
                       QImage::Format_RGB888, &matDeleter, new cv::Mat(frame));// convertion from cv::Mat to QImage
image.save("/home/pi/sample1.jpeg");//save QImage
emit image_signal(image);//emit Qimage sinal to paint event

`

I am using qt creator in raspberry
please refer image with resolution 640 X 480.


displaying QImage in PaintEvent.sample
saved opencv cv::Mat
sample1
saved QImage

please help me figure out what’s wrong with my code

Sir ,

is there some other way to reduce buffer image ,because by grabbing image continuously from camera in thread at background may slow down application speed and not accurate with triggered image at high speed operation.
If I am wrong with above ,please Guide me with an example code

It seems that it is not easy to achieve this goal.
There are many buffers here:
Camera driver buffer, isp input buffer, isp output buffer.
The minimum buffer can be set to 1, but the camera is always working, so there is always data in the buffer.

Sir , then how to get current image from camera if I triggered in high speed manner

Since the camera does not support external triggering, you can only use software to achieve it, as I mentioned before.

Sir ,I have tried some hacks to read Current Image From camera as follow :

1 . Grabbing of image in multiple time to get current Image when call grab function at once
Issue → while grabbing at multiple time it take more time then usual timing around 120-160ms
2 . Grabbing of image to global frame variable in separate timer event . when application read an external trigger ,application process the recently assigned frame variable .
Issue ->while the application in high speed manner it may miss external trigger frequently because
when externally not triggered the application continuously grab images from camera in timer event mean while externally triggered the application need to wait for reading input trigger until camera grabbing is over

if there some other hacks , please guide me to achieve required current image from camera

Unfortunately I don’t have a better way…

Thank @wong for Your support to read Pivariety camera image with Open-cv, if simpler sample code as you mentioned before with opencv is developed please inform me for further development of my application.
Thank you .

Okay, but we are a bit busy lately and the speed will be a little slower.

Sir ,

its okay ,if it is completed just inform me .
Thank you.

We have just completed a simple example, you can refer to it:
libcamera_cpp_demo-master.zip (1.7 MB)

Thanks Wong ,
by running given demo code I got an error at

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.5.0) /home/pi/install_lib/opencv/modules/highgui/src/window.cpp:651: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'

so I transfer code to Qt creator for display frame in ImageViewer,but by compiling demo code in Qt I got following error while build application

   `  :-1: error: /usr/local/lib/arm-linux-gnueabihf/libcamera.so.0: error adding symbols: DSO missing from command line`

Please provide support to overcome this issue

hi, Balaji

You can see from the error log that your opencv is compiled and installed by yourself, which may cause this problem.
You can install opencv via apt:

sudo apt-get install libopencv-dev

Regarding the QT error, it may be due to a problem with the system environment configuration. You need to find it yourself.We cannot help you find the error.

Thank yang , Let me try it Myself