一起使用 cvQueryFrame 和 boost::thread

发布于 2025-01-07 21:52:26 字数 974 浏览 0 评论 0原文

我需要调用 cvQueryFrame (使用 opencv 从网络摄像头捕获帧),而不是使用 boost 创建的线程。这是一个小示例代码:

void testCVfunc(){
    IplImage* frame;
    CvCapture *capture;
    capture = cvCreateCameraCapture(CV_CAP_ANY);
    if(!capture){
        exit(1);
    }
    frame = cvQueryFrame(capture);
    cvNamedWindow("testCV", 1);

    while(frame = cvQueryFrame(capture)){
        if(!frame){
            exit(2);
        }
        cvShowImage("testCV", frame);
        cvWaitKey(1);
    }
    cvReleaseImage(&frame);
    cvReleaseCapture(&capture);
}

int main(){
    //Method 1: without boost::thread, works fine
    testCVfunc();

    //Method 2: with boost::thread, show black screen
    char entree;
    boost::thread threadTestCV = boost::thread(&testCVfunc);
    std::cin >> entree;
}

正如注释所说,如果我不从 boost::thread 调用它,testCVfunc 就会完成它的工作,但我会得到黑屏如果我使用boost::thread。 我不明白这个问题,也许有人明白?

感谢您的帮助。

I need to call cvQueryFrame (to capture a frame from a webcam with opencv) instead a thread created with boost. Here is a little example code:

void testCVfunc(){
    IplImage* frame;
    CvCapture *capture;
    capture = cvCreateCameraCapture(CV_CAP_ANY);
    if(!capture){
        exit(1);
    }
    frame = cvQueryFrame(capture);
    cvNamedWindow("testCV", 1);

    while(frame = cvQueryFrame(capture)){
        if(!frame){
            exit(2);
        }
        cvShowImage("testCV", frame);
        cvWaitKey(1);
    }
    cvReleaseImage(&frame);
    cvReleaseCapture(&capture);
}

int main(){
    //Method 1: without boost::thread, works fine
    testCVfunc();

    //Method 2: with boost::thread, show black screen
    char entree;
    boost::thread threadTestCV = boost::thread(&testCVfunc);
    std::cin >> entree;
}

As the comments say, testCVfunc does its job if I don't call it from a boost::thread, but I get a black screen if I use boost::thread.
I don't get the problem, maybe someone does?

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘香 2025-01-14 21:52:26

当从辅助线程执行 OpenCV 时,我看到了一些问题,并且当所有平台上的行为不一致时,很难查明问题的根源。

例如,您的源代码可以与 Mac OS X 10.7.2 上的 OpenCV 2.3.0 完美配合。我不知道您使用的是什么平台,但它在我的计算机上运行的事实表明 OpenCV 在您使用的平台上存在一些实现问题。

现在,如果您无法将 OpenCV 的代码移至主线程,那么您可能需要开始考虑创建第二个程序来处理所有 OpenCV 相关任务,并使用某种 IPC 机制来允许该程序与您的主线程进行通信应用。

I've seen some problems when OpenCV is being executed from a secondary thread and it's difficult to pinpoint the origin of the problem when the behavior is not consistent on all platforms.

For instance, your source code worked perfectly with OpenCV 2.3.0 on Mac OS X 10.7.2. I don't know what platform you are using, but the fact that it worked on my computer indicates that OpenCV has some implementation issues with the platform you are using.

Now, if you can't move OpenCV's code to the primary thread, then you might want to start thinking about creating a 2nd program to handle all OpenCV related tasks, and use some sort of IPC mechanism to allow this program to communicate with your main application.

初懵 2025-01-14 21:52:26

我通过调用主线程解决了问题

cvCreateCameraCapture(CV_CAP_ANY);

,即使它并没有真正回答问题:

为什么这不起作用?问题。

希望这可以帮助别人。

I solved the problem by calling

cvCreateCameraCapture(CV_CAP_ANY);

in the main thread, even if it doesn't really answer the question:

why is this not working? question.

Hope this can help someone else.

小瓶盖 2025-01-14 21:52:26

尝试调用 cv::startWindowThread();在主应用程序中,然后在线程中创建一个窗口。这对我有用。

Try calling cv::startWindowThread(); in the main app and then creating a window within your thread. This worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文