thread.sleep()do do do do cv2.imshow,opencvSharp,c#

发布于 2025-02-10 15:36:45 字数 608 浏览 0 评论 0原文

非常简单的C#Winforms应用程序,OpenCvSharp4,thread.sleep()不起作用。 似乎(我猜)CV2无法在多个线程中使用,例如,一个线程创建名为Window,另一个线程Imshow。但是下面的这个简单代码仍然令人困惑。

    private void button1_Click(object sender, EventArgs e)
    {
        VideoCapture video = new VideoCapture("E:/RankingBack.mp4");
        Mat frame = new Mat();

        while (true)
        {
            video.Read(frame);
            Cv2.ImShow(" ", frame);
            //Cv2.WaitKey(33);  // WORKS
            Thread.Sleep(33);   // DOES NOT WORK
        }

        frame.Dispose();
        video.Release();
        Cv2.DestroyAllWindows();

    }

Very Simple C# Winforms App, OpenCVSharp4, Thread.Sleep() doesn't work.
It seems like (as I guess) Cv2 cannot be used in multiple threads, eg, one thread create NamedWindow, another thread ImShow. But this simple code below is still confusing.

    private void button1_Click(object sender, EventArgs e)
    {
        VideoCapture video = new VideoCapture("E:/RankingBack.mp4");
        Mat frame = new Mat();

        while (true)
        {
            video.Read(frame);
            Cv2.ImShow(" ", frame);
            //Cv2.WaitKey(33);  // WORKS
            Thread.Sleep(33);   // DOES NOT WORK
        }

        frame.Dispose();
        video.Release();
        Cv2.DestroyAllWindows();

    }

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

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

发布评论

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

评论(1

执笏见 2025-02-17 15:36:45

Waitkey的目的不是等待,而是 openCV使用的GUI事件循环 。如果您不称呼它,OpenCV的GUI不起作用。

您已经使用了C#和Winforms(通过标签来判断),因此您应该使用Winforms代替imshow。 OpenCV执行自己的GUI事件处理,可能与其他程序中使用的其他GUI工具包发生冲突。

OPENCV的GUI函数通常不是 可以安全地从线程调用,因为某些GUI后端不允许这样做。不过这是无关紧要的,因为...

在这里没有“线程”。您有一个按钮点击事件的处理程序。这是由Winforms应用程序的事件循环执行的。这发生在每个过程中始终存在的主线程中。那不是产卵的线。

waitKey's purpose is not waiting, it's to spin the GUI event loop that OpenCV uses. If you don't call it, OpenCV's GUI doesn't work.

You're using C# and WinForms already (judging by the tags), so you should use WinForms instead of imshow. OpenCV does its own GUI event handling, which may conflict with other GUI toolkits that are being used in the same program.

OpenCV's GUI functions are generally not safe to call from threads because some GUI backends don't allow that. That's irrelevant though because...

You do not have a "thread" here. You have the handler for a button click event. That's executed by the event loop of your WinForms application. That happens in the main thread that is always there in every process. That is not a spawned thread.

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