cvQueryFrame issue in multithreaded application on Windows

View: New views
4 Messages — Rating Filter:   Alert me  

cvQueryFrame issue in multithreaded application on Windows

by visionofarun :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

When using a single thread, I do not face any problem. I can initialize the camera and query for the frame. Here is the code for that.

/*
 * No multithread
 */
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
CvCapture *inputSource;


int main()
{
        //CvCapture *inputSource;
        IplImage* inputImage = NULL;
        inputSource = cvCaptureFromCAM( 0, cvSize(320,240) );
        if (NULL == inputSource) {
                printf("ERROR: Camera not detected or connected.\n");
        return 0;
        }
        else {
                printf("inputSource: %u\n", inputSource);
        }
        cvNamedWindow( "Testing", 0 );
        printf("Before Query Frame\n");
        inputImage = cvQueryFrame( inputSource );
        while(1) {
                if (!inputImage) {
                        printf ("I have a NULL pointer\n");
                        exit(1);
                }
                else {
                        printf("Success to capture frame\n");
                        cvShowImage ("Testing", inputImage);
                        cvWaitKey(0);
                        //Sleep(1);
                }
        }
        cvDestroyWindow("Testing");
        return 1;
}

/*********************/

But when I use multithreads by initializing the camera with cvCaptureFromCam() in one thread and then use that inputSource to cvQueryFrame in other thread, the cvQueryFrame function blocks and doesn't return at all. I can see that the SendMessage() call is blocking and the callback is not delivered. I am not able to get further on this. Has someone faced the same issue before? Here is the code for that.

/*
 * With multithread
 */
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <windows.h>
CvCapture *inputSource;

int AccessFrame(void)
{
        cvNamedWindow( "Testing", 0 );
        IplImage* inputImage = NULL;
        printf("Before Query Frame\n");
        inputImage = cvQueryFrame( inputSource );
        while(1) {
                if (!inputImage) {
                        printf ("I have a NULL pointer\n");
                        exit(1);
                }
                else {
                        printf("Success to capture frame\n");
                        cvShowImage ("Testing", inputImage);
                        cvWaitKey(0);
                        //Sleep(1);
                }
        }
}


int main()
{
        //CvCapture *inputSource;
       
        inputSource = cvCaptureFromCAM( 0, cvSize(320,240) );
        if (NULL == inputSource) {
                printf("ERROR: Camera not detected or connected.\n");
        return 0;
        }
        else {
                printf("inputSource: %u\n", inputSource);
        }
        CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)AccessFrame, NULL, 0, 0);
        while (1)
                Sleep(100000);
        cvWaitKey(0);
        cvDestroyWindow("Testing");
        return 1;
}
/*********************/

Re: cvQueryFrame issue in multithreaded application on Windows

by Nils Hasler-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

depending on the backend you are using it may or may not be possible
to reuse the CvCapture from a different thread. In general, you cannot
assume that it is possible to access a camera from a thread that did
not open it.

Try opening the camera in the thread you want to use it in.

cheers,
nils

2009/4/8 visionofarun <visionofarun@...>:

> But when I use multithreads by initializing the camera with
> cvCaptureFromCam() in one thread and then use that inputSource to
> cvQueryFrame in other thread, the cvQueryFrame function blocks and doesn't
> return at all. I can see that the SendMessage() call is blocking and the
> callback is not delivered. I am not able to get further on this. Has someone
> faced the same issue before? Here is the code for that.
>
> /*
>  * With multithread
>  */
> #include <cv.h>
> #include <highgui.h>
> #include <stdio.h>
> #include <windows.h>
> CvCapture *inputSource;
>
> int AccessFrame(void)
> {
>        cvNamedWindow( "Testing", 0 );
>        IplImage* inputImage = NULL;
>        printf("Before Query Frame\n");
>        inputImage = cvQueryFrame( inputSource );
>        while(1) {
>                if (!inputImage) {
>                        printf ("I have a NULL pointer\n");
>                        exit(1);
>                }
>                else {
>                        printf("Success to capture frame\n");
>                        cvShowImage ("Testing", inputImage);
>                        cvWaitKey(0);
>                        //Sleep(1);
>                }
>        }
> }
>
>
> int main()
> {
>        //CvCapture *inputSource;
>
>        inputSource = cvCaptureFromCAM( 0, cvSize(320,240) );
>        if (NULL == inputSource) {
>                printf("ERROR: Camera not detected or connected.\n");
>        return 0;
>        }
>        else {
>                printf("inputSource: %u\n", inputSource);
>        }
>        CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)AccessFrame, NULL, 0, 0);
>        while (1)
>                Sleep(100000);
>        cvWaitKey(0);
>        cvDestroyWindow("Testing");
>        return 1;
> }
> /*********************/
> --
> View this message in context: http://www.nabble.com/cvQueryFrame-issue-in-multithreaded-application-on-Windows-tp22941729p22941729.html
> Sent from the opencvlibrary-devel mailing list archive at Nabble.com.
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by:
> High Quality Requirements in a Collaborative Environment.
> Download a free trial of Rational Requirements Composer Now!
> http://p.sf.net/sfu/www-ibm-com
> _______________________________________________
> Opencvlibrary-devel mailing list
> Opencvlibrary-devel@...
> https://lists.sourceforge.net/lists/listinfo/opencvlibrary-devel
>



--
MPI Informatik
Campus E1 4, D-66123 Saarbrücken
Phone: +49 681 9325-420
www.mpi-inf.mpg.de/~hasler

------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Opencvlibrary-devel mailing list
Opencvlibrary-devel@...
https://lists.sourceforge.net/lists/listinfo/opencvlibrary-devel

Re: cvQueryFrame issue in multithreaded application on Windows

by visionofarun :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Nils,

Thanks for your reply. I missed to mention that accessing the device
from the thread that initialized it works absolutely fine. But not the
other way around. So, my initial thought was that, the threads on
Windows are not behaving like actual threads, sharing a common address
space, but they behave as independent processes. Later I thought in
terms of the device being accessed. If the device file descriptor (if
there is any such thing on Windows) is not being shared among threads.

The given multithreaded code works fine on Linux with POSIX Thread
calls. ie, icvGrabFrameCAM_V4L() works fine but icvGrabFrameCAM_VFW()
causes this problem!

So I was wondering if this could be a problem with Windows
implementation of the driver or something else. Because, the UVC
driver that we use on Linux doesn't cause any such problem. Since the
capGrabFrameNoStop() finally boils down to ((BOOL)AVICapSM(hwnd,
WM_CAP_GRAB_FRAME_NOSTOP, (WPARAM)0, (LPARAM)0L)), I am not able to
dig beyond that. I agree that this is the ultimate limitation with
Windows, not having its source.

So, for now, the only workaround is to initialize the device on the
same thread from which we access it.

Cheers,
Arun

On Wed, Apr 8, 2009 at 10:48 AM, Nils Hasler <nils.hasler@...> wrote:

> Hi,
>
> depending on the backend you are using it may or may not be possible
> to reuse the CvCapture from a different thread. In general, you cannot
> assume that it is possible to access a camera from a thread that did
> not open it.
>
> Try opening the camera in the thread you want to use it in.
>
> cheers,
> nils
>
> 2009/4/8 visionofarun <visionofarun@...>:
>> But when I use multithreads by initializing the camera with
>> cvCaptureFromCam() in one thread and then use that inputSource to
>> cvQueryFrame in other thread, the cvQueryFrame function blocks and doesn't
>> return at all. I can see that the SendMessage() call is blocking and the
>> callback is not delivered. I am not able to get further on this. Has someone
>> faced the same issue before? Here is the code for that.
>>
>> /*
>>  * With multithread
>>  */
>> #include <cv.h>
>> #include <highgui.h>
>> #include <stdio.h>
>> #include <windows.h>
>> CvCapture *inputSource;
>>
>> int AccessFrame(void)
>> {
>>        cvNamedWindow( "Testing", 0 );
>>        IplImage* inputImage = NULL;
>>        printf("Before Query Frame\n");
>>        inputImage = cvQueryFrame( inputSource );
>>        while(1) {
>>                if (!inputImage) {
>>                        printf ("I have a NULL pointer\n");
>>                        exit(1);
>>                }
>>                else {
>>                        printf("Success to capture frame\n");
>>                        cvShowImage ("Testing", inputImage);
>>                        cvWaitKey(0);
>>                        //Sleep(1);
>>                }
>>        }
>> }
>>
>>
>> int main()
>> {
>>        //CvCapture *inputSource;
>>
>>        inputSource = cvCaptureFromCAM( 0, cvSize(320,240) );
>>        if (NULL == inputSource) {
>>                printf("ERROR: Camera not detected or connected.\n");
>>        return 0;
>>        }
>>        else {
>>                printf("inputSource: %u\n", inputSource);
>>        }
>>        CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)AccessFrame, NULL, 0, 0);
>>        while (1)
>>                Sleep(100000);
>>        cvWaitKey(0);
>>        cvDestroyWindow("Testing");
>>        return 1;
>> }
>> /*********************/
>> --
>> View this message in context: http://www.nabble.com/cvQueryFrame-issue-in-multithreaded-application-on-Windows-tp22941729p22941729.html
>> Sent from the opencvlibrary-devel mailing list archive at Nabble.com.
>>
>>
>> ------------------------------------------------------------------------------
>> This SF.net email is sponsored by:
>> High Quality Requirements in a Collaborative Environment.
>> Download a free trial of Rational Requirements Composer Now!
>> http://p.sf.net/sfu/www-ibm-com
>> _______________________________________________
>> Opencvlibrary-devel mailing list
>> Opencvlibrary-devel@...
>> https://lists.sourceforge.net/lists/listinfo/opencvlibrary-devel
>>
>
>
>
> --
> MPI Informatik
> Campus E1 4, D-66123 Saarbrücken
> Phone: +49 681 9325-420
> www.mpi-inf.mpg.de/~hasler
>

------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Opencvlibrary-devel mailing list
Opencvlibrary-devel@...
https://lists.sourceforge.net/lists/listinfo/opencvlibrary-devel

Re: cvQueryFrame issue in multithreaded application on Windows

by Mario Luzeiro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I think I got a similar problem...
I'm doing it in linux and GTK

check it here:
https://sourceforge.net/projects/opencvlibrary/forums/forum/72228/topic/3442757

Have you solved it in any (elegant?) way?
Is there also any way to grab the very last frame from camera? or maybe to know that a new frame is ready..

thanks in advance,
MRL