Capture problem in thread
Hi.
I'm try to capture a frame from webcam with parallel work in another thread.
When I capture a frame without another thread, capture function works at 50~60fps.
But, work with another thread even just while loop, capture function work at 20~30fps.
Below is a code.
using namespace std;
using namespace cv;
int k = 0;
IplImage* image = 0;
CvCapture* capture = cvCaptureFromCAM(0);
int Thrr()
{
while (!k)
{
;
}
}
int main(int argc, char** argv)
{
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 1280);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 720);
thread th1(Thrr);
while (1)
{
for (int i = 0; i < 300; i++)
{
clock_t aaa = clock();
cvGrabFrame(capture);
clock_t ss = clock();
image = cvRetrieveFrame(capture);
printf("%d ", i);
printf("a12 frame : %f\n", double(CLOCKS_PER_SEC) / double(clock() - aaa));
}
k = 1;
}
return 0;
}
Like above image you can see, when it capture a frame with another thread in first for loop, it works at 25fps.
But after first for loop, Thrr() thread end(k = 1, it means Thrr() while(!k) loop end), capture function works at 55fps.
Didn't any of you know why it is and how can I do for this problem.
Thanks.
please avoid using opencv's deprecated, no more maintained c-api.
Thanks for reply. But it is same problem even if I use a VideoCapture class. Do you know why is it?
please also note, that opencv is not threadsafe by design.
do you have more than one core ? if not, you're just "stealing" cpu-ticks from the main thread this way. (which would pretty much explain, what you're seeing)
again, sorry for the downvote, but your code is a bad example, and might mislead other noobs seing this.
Yeah. My system has Octa-core(exynos 5422 / octa-core) cpu. So I can't understand this problem. Also I got it understand your mind. never mind. Thanks.
also, clock() measures CPU ticks, while retrieving frames from an usb webcam is mostly an IO problem (apart from the decoding)
1 avoid using opencv's deprecated IplImage and CvCapture
2 are you sure your webcam works 1280x720@60fps ?
3 You have to measure Grab and Retrieve