1

我有一个从某个地方获取图像的主要功能,它应该显示在一个新线程中。这就是我正在做的事情:

int main(int argc, char** argv)
{
    Mat frame = imread("img.jpg");

    auto_ptr<SomeClass> thread1(new SomeClass(1, frame));
    auto_ptr<SomeClass> thread2(new SomeClass(2, frame));

    thread1->Start();
    thread2->Start();

    int result1 = reinterpret_cast<int>(thread1->Join());
    int result2 = reinterpret_cast<int>(thread2->Join());

    return 0;
}

我找到了许多源代码来实现一个简单的 pthread 包装类,只要我不尝试将“cv::Mat 框架”图像传递给派生线程(通过其构造函数),这些源代码运行良好。

pthread 包装类:

/* ********************* THREAD.H ********************* */
extern "C++" class __declspec(dllexport) Thread 
{
public:
    Thread();
    virtual ~Thread(void);

    void Start(void);
    void* Join(void);

private:
    virtual void* Run(void) = 0;

    static void* StartThread(void* pVoid);

    pthread_t threadID_;
    pthread_attr_t threadAttribute_;
    void* result_;
};

/* ********************* THREAD.CPP ********************* */
Thread::Thread() {}

Thread::~Thread() {}

void* Thread::StartThread(void* pVoid) 
{
    Thread* aThread = static_cast<Thread*>(pVoid);
    aThread->result_ = aThread->Run();
    return aThread->result_;
}

void Thread::Start() 
{
    pthread_attr_init(&threadAttribute_);
    pthread_attr_setscope(&threadAttribute_, PTHREAD_SCOPE_SYSTEM);

    pthread_create(&threadID_, &threadAttribute_, Thread::StartThread, (void*)this);
    pthread_attr_destroy(&threadAttribute_);
}


void* Thread::Join() 
{
    pthread_join(threadID_, NULL);
    return result_;
}

Thread 的子类如下所示:

/* ********************* SOMECLASS.H ********************* */
extern "C++" class __declspec(dllexport) SomeClass : public Thread
{
public:
    SomeClass(int id, cv::Mat& frame);
    ~SomeClass(void);

    virtual void* Run();
private:
    int id_;
    cv::Mat frame_;
};

/* ********************* SOMECLASS.CPP ********************* */
SomeClass::SomeClass(int id, cv::Mat& frame) : id_(id)
{
    frame_ = frame.clone();
}

SomeClass::~SomeClass(void) {}

void* SomeClass::Run() 
{
    cout << id_ << ", " << frame_.cols << ", " << frame_.rows << endl;

    imshow("frame " + id_, frame_);
    waitKey(1);

    return reinterpret_cast<void*>(id_);
}

执行在行 imshow() 处中断......知道什么会导致这个问题或者我该如何解决它?

4

1 回答 1

0

问题已解决。如果有人遇到类似的问题,那么:整个 OpenCV 项目需要在英特尔 TBB 支持下重建。(它适用于 OpenCV 2.3.1)

于 2012-04-22T09:24:51.047 回答