cv::VideoCapture stopped working in separate threads

Hello dear community! I am porting some old opencv code from v.2.4.1 to v.4.8.1 and i have stucked with cv::VideoCapture get frames from two separate threads and two different cameras (in v.2.4.1 it works like a charm), i didn’t change any code, just changed version of openCV in my project (VS 2022, Windows).

Here is my code, i run it in two separate threads:

void CsdkDemoDlg::GETFRAME_LEFT_CAM()
{
	cv::VideoCapture videoCaptureLeftCam;
	videoCaptureLeftCam.open(0);
	while (true)
	{
		try
		{
			cv::Mat processfrm;
			//Get frame from webcam
			videoCaptureLeftCam >> processfrm;
			DrawImg(processfrm);
			
			// Sleep one milisecond

		}
		catch (const boost::thread_interrupted&)
		{
	
		}
	}
}
void CsdkDemoDlg::GETFRAME_RIGHT_CAM()
{
	cv::VideoCapture videoCaptureRightCam;
	videoCaptureRightCam.open(0);
	while (true)
	{
		try
		{
			cv::Mat processfrm;
			//Get frame from webcam
			videoCaptureRightCam >> processfrm;
		        DrawImg(processfrm);
			
			// Sleep one milisecond

		}
		catch (const boost::thread_interrupted&)
		{
		
		}
	}
}

Is there anything changed in cv::VideoCapture threading work? Or maybe i should change any code? Help me please to find a solution.

one of them needs a different id

1 Like

For test purpose i use the same camera from my laptop, it has index = 0, in v.2.4.1 it work without problems i can see a videocapture in two different windows from one camera., but in v.4.8.1 i can see a videocapture only in one window, besides if i start and stop the program it can be “window1” or “window2” any time it different, but never in two windows simultaneosly

again, it does not matter, what it did, 10 years ago.

1 Like

I completely agree, could you please suggest any solution ? You mean that i cannot use anymore two different instance of cv::Videocapture with the same camera ID?

yes, exactly . . . . . .

1 Like

Ok, thank you for a quick reply!

What if i need to process two or more Videocaptures from the same camera in separate threads? What is the best approach for it?

not possible with opencv, afaik. separate thread or not.
maybe something like obs works for you

1 Like

sure it’s possible, but opencv won’t help, because that is not opencv’s purpose. you should use multithreading but sensibly.

you should capture in one thread, then distribute every frame to each consumer.

if the consumers can’t keep up with production, you’ll have to know some computer science to coordinate that stuff. this would necessarily require dropping frames.

1 Like

using multiple cameras in multiple threads, but not the same multiple times

1 Like

I just described how to read from one camera and then distribute that same frame to multiple consumers, in custom code. you should see that nothing prevents this.

2 Likes

I have a class for that purpose, i think i will stop on this approach…

OBS is really powerfull stuff for video processing, I’ll keep it in mind as an option…

Thank you for replies and valuable advices!!!