c++ - 我们可以使用 pthread 库进行 opencv C++ 编程吗?

标签 c++ c linux multithreading opencv

我已经实现了一个 openCV 程序,它可以从视频文件中捕获帧并对其进行处理并创建一个新文件。那是为 single in file 做的。现在我想要多个文件。然后我对 POSIX 线程 pthread 库有了一个想法。这是好主意还是坏主意。实际上,当我在 opencv 程序中实现 pthreads 时,出现了如下错误:

OpenCV Error: Assertion failed (_src.sameSize(_dst) && dcn == scn) in accumulate, file /home/satinder/opencv_installation/OpenCV/opencv/modules/imgproc/src/accum.cpp, line 915

what(): /home/satinder/opencv_installation/OpenCV/opencv/modules/imgproc/src/accum.cpp:915: error: (-215) _src.sameSize(_dst) && dcn == scn in function accumulate Aborted (core dumped)

corrupted double-linked list: 0x00007fcd048f73d0 *** Aborted (core dumped)

seg fault 也有一段时间了。

有没有什么可能的方法可以实现多线程或等效的我的目标是制作一个程序,该程序可以为相同的处理获取多个输入文件。

以下是我的代码快照:

#include "opencv2/highgui/highgui.hpp"
#include <sys/types.h>
#include <pthread.h>
#include <iostream>

using namespace cv;
using namespace std;


void * VideoCap(void *);


void * VideoCap(void *arg)
{
         VideoCapture cap((char *)arg); // open the video file for reading

    if ( !cap.isOpened() )  // if not success, exit program
    {
         cout << "Cannot open the video file" << endl;
         exit(1);
    }

    //cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms

    double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video

     cout << "Frame per seconds : " << fps << endl;

    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

    while(1)
    {
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

         if (!bSuccess) //if not success, break loop
        {
                        cout << "Cannot read the frame from video file" << endl;
                       break;
        }

        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
       {
                cout << "esc key is pressed by user" << endl;
                break;
       }
    }

}

int main(int argc, char* argv[])
{

        int ret ;
        pthread_t th[2];

        ret = pthread_create(&th[0] , NULL , VideoCap , (void *)"cctv3.mp4");
        if(0 == ret)
        {
                cout << "Thread 1 is created successfull" << endl;
        }
        ret = pthread_create(&th[1] , NULL , VideoCap , (void *)"cctv10.mp4");
        if(0 == ret)
        {
                cout << "Thread 2 is created successfull" << endl;
        }
        pthread_join(th[0] , NULL);
        pthread_join(th[1] , NULL);

    return 0;
}

最佳答案

你的代码有一些问题

namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

您正在创建两个线程,因此窗口应该有不同的标识符。

namedWindow((char *)arg, CV_WINDOW_AUTOSIZE);
...
imshow((char *)arg, frame);

因为这个问题是用 linux 标签发布的,我猜 gtk 是相关的。插入指令后

#include <gdk/gdk.h>
#include <gtk/gtkmain.h>

在文件的开头,然后在 main()

pthread_t th[2];

gtk_disable_setlocale();
gtk_init(&argc, &argv); 
gdk_threads_init();

可能需要新的链接器/编译器标志,

g++ -O -Wall test.cpp -lopencv_highgui -lopencv_core `pkg-config --libs --cflags gdk-2.0 gtk+-2.0`

在这些更改之后仍然偶尔会发生崩溃,所以我添加了 gdk_threads_enter()gdk_threads_leave(); 调用来测试它们是否有帮助:

gdk_threads_enter();
namedWindow ((char *) arg, CV_WINDOW_AUTOSIZE);
gdk_threads_leave();

由于崩溃无法重现,因此很难判断这些行是否有任何影响。

关于c++ - 我们可以使用 pthread 库进行 opencv C++ 编程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35058463/

相关文章:

linux - linux更新环境变量后anaconda无法识别

c++ - 更改dll对windows的依赖

c++ - 在 C++ 中,std::cin 对象在哪里定义?

c++ - 禁用所有线程默认值的核心关联

linux - 有没有办法在 Amazon Linux 系统上安装 gnome、kde 或任何其他 X 界面?

linux - 错误:2006D002:BIO routines:BIO_new_file:system lib

c++ - 带有类C++的控制台应用程序菜单

c++ - Mac 上的 XCode C++ 源代码打印问题

c - 如何加速我的代码?

c - 如何测试Socket的速度?