Performance issue with cv::VideoCapture.set

Hi everybody,

setting camera properties like height, width, frameRate etc. with VideoCapture.set takes a lot of time compared to the other operations like connecting to a camera. According to my tests, this is happening only for USB cameras. IP cameras are fine.

Here my test result:

**Integrated USB Camera (HP 5MP Camera)**
PERFORMANCE TEST CAMERA 0
Connected!
Time taken: 9.92867 seconds
Read camera width: 640
Time taken: 9.75e-05 seconds
**Set camera width on: 800**
**Time taken: 6.41921 seconds**
Read 100 frames.
Time taken: 3.658 seconds

**IP Camera (AXIS M1013)**
PERFORMANCE TEST CAMERA 1
Connected!
Time taken: 1.01986 seconds
Read camera width: 640
Time taken: 0.0002403 seconds
**Set camera width on: 800**
**Time taken: 0.0014786 seconds**
Read 100 frames.
Time taken: 4.93484 seconds

Here the test program:

#include <opencv2/opencv.hpp>
#include <iostream>
#include <chrono>

void measureCameraPerformance(int cameraIndex)
{
	std::cout << "PERFORMANCE TEST CAMERA " << cameraIndex << std::endl;

	auto start = std::chrono::high_resolution_clock::now();

	cv::VideoCapture cap(cameraIndex); // Open the specified camera
	if (!cap.isOpened()) {
		std::cerr << "Error: Unable to connect to camera!" << std::endl;
		return;
	}
	auto end = std::chrono::high_resolution_clock::now();

	std::chrono::duration<double> elapsed_seconds = end - start;

	std::cout << "Connected!" << std::endl;
	std::cout << "Time taken: " << elapsed_seconds.count() << " seconds" << std::endl;

	//Reading camera settings
	start = std::chrono::high_resolution_clock::now();

	int width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
	std::cout << "Read camera width: " << width << std::endl;

	end = std::chrono::high_resolution_clock::now();
	elapsed_seconds = end - start;
	std::cout << "Time taken: " << elapsed_seconds.count() << " seconds" << std::endl;

	//Setting new values for camera settings
	start = std::chrono::high_resolution_clock::now();

	width = 800;
	cap.set(cv::CAP_PROP_FRAME_WIDTH, width);
	std::cout << "Set camera width on: " << width << std::endl;

	end = std::chrono::high_resolution_clock::now();
	elapsed_seconds = end - start;
	std::cout << "Time taken: " << elapsed_seconds.count() << " seconds" << std::endl;

	start = std::chrono::high_resolution_clock::now();

	cv::Mat frame;
	for (int i = 0; i < 100; i++) {
		cap >> frame;
	}
	std::cout << "Read 100 frames." << std::endl;

	end = std::chrono::high_resolution_clock::now();
	elapsed_seconds = end - start;
	std::cout << "Time taken: " << elapsed_seconds.count() << " seconds" << std::endl;

	cap.release();
}

int main()
{
	measureCameraPerformance(0);
	measureCameraPerformance(1);

	return 0;
}

Has anybody an idea why this setter takes so long? Is there another way to change the camera settings?

Any helping idea is appreciated :-)!

Thanks in advance!

Short update
Setting the video capture API explicitly when opening the capture solved my performance problem.
E.g.
capture.open(0, cv::CAP_DSHOW);