【问题标题】:Initialising OpenCV-VideoCapture-class in another multithreading class在另一个多线程类中初始化 OpenCV-VideoCapture 类
【发布时间】:2014-10-14 11:44:03
【问题描述】:

我试图在我的多线程程序中初始化一次 VideoCapture 捕获设备。一旦初始化,它应该在acquireImage线程中作为图像缓冲区服务,由网络摄像头填充。 以下代码在运行时显示“OpenCV 错误:在 cv::imshow 中断言失败 (func != 0)”-错误,这意味着 VideoCapture 捕获设备从未真正初始化,因此 imshow 没有必要的数据,但为什么呢?

代码是基于这个问题构建的:Correctly using mutex in OpenCL-OpenCV-Realtime-Threads? 基本上它使用两个线程,一个处理来自 USB 网络摄像头的图像,第二个将在这些图像上找到面孔......但目前什么都不做代码-简单。

#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <vector>
#include <cmath>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/ocl/ocl.hpp"
#include "opencv2/opencv.hpp"
#include <functional>

using namespace std;
using namespace cv;

typedef unsigned char uchar;
typedef unsigned int uint;

class FaceDetector
{
mutex imageLock, facesLock;
condition_variable imageAqcuired;
bool newImageAvailable;

Mat _img;
Mat _imgToWorkOn;
Mat _faceImages;
bool quit;

VideoCapture captureDevice;
int device_id;
FaceDetector (int _device_id);

void acquireImage()
{
while (!quit)
{

    unique_lock<mutex> ulock(imageLock);
    imageAqcuired.wait(ulock,[&](){return !newImageAvailable;}); //only take new image after current one was consumed

    Mat captureFrame;
    captureDevice>>captureFrame;
    transpose(captureFrame,captureFrame);
    flip(captureFrame,captureFrame,1);
    _img = captureFrame.clone();

    ulock.unlock();
    newImageAvailable = true;
    imageAqcuired.notify_one(); //notify that a new image is available
}
}
void processImage()
{
while (!quit)
{
    unique_lock<mutex> ulock(imageLock);
    imageAqcuired.wait(ulock,[&](){return newImageAvailable;}); //wait untill a new image is available
    _imgToWorkOn = _img.clone();
    ulock.unlock();
    newImageAvailable = false;
    imageAqcuired.notify_one(); //notify the current image can be replaced by a newer one
    unique_lock<mutex> lockFace(facesLock);
    //arbeit
    lockFace.unlock();
}
}

public:
FaceDetector() : newImageAvailable(false) {}
void start() {
quit = false;
thread t1(&FaceDetector::acquireImage,this);
t1.detach();
thread t2(&FaceDetector::processImage,this);
t2.detach();
}
void stop() {
quit = true;
}
Mat getImage() {
if (quit)
    return Mat();
lock_guard<mutex> lock(imageLock);
return _img;
}

Mat getProcessedImage() {
if (quit)
    return Mat();
lock_guard<mutex> lock(facesLock);
return _faceImages;
}

};

FaceDetector::FaceDetector(int _device_id)
{

device_id = _device_id;
captureDevice.open(device_id);
captureDevice.set(CV_CAP_PROP_FRAME_WIDTH,620);             //erst jetzt cam.set weil sonst system-pause nicht funzt
captureDevice.set(CV_CAP_PROP_FRAME_HEIGHT,480);

}

int main()
{
bool quit(false);
FaceDetector faceDet;
faceDet.start();
thread input([](bool &quitFlag) { getchar(); quitFlag = true; },ref(quit)); //stop on user press Enter
input.detach();
while (!quit) {


Mat img = faceDet.getImage();
Mat imgc = img.clone();

imshow("links", imgc);

/*
imgs = faceDet.getProcessedImage();
Mat imgsc = imgs.clone();

imshow("gsichter", imgsc);
*/
waitKey(30);
this_thread::sleep_for(chrono::milliseconds(33)); //no need to show more than 30 fps...
}
faceDet.stop();
return 0;
}

编辑:我试图包含您的答案,但仍然收到“imshow 中的 Opencv 断言错误”。

#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <vector>
#include <cmath>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/ocl/ocl.hpp"
#include "opencv2/opencv.hpp"
#include <functional>

using namespace std;
using namespace cv;

typedef unsigned char uchar;
typedef unsigned int uint;

class FaceDetector
{
mutex imageLock, facesLock;
condition_variable imageAqcuired;
bool newImageAvailable;

Mat _img;
Mat _imgToWorkOn;
Mat _faceImages;
bool quit;

VideoCapture captureDevice;
int device_id;

void acquireImage()
{
while (!quit)
{

    unique_lock<mutex> ulock(imageLock);
    imageAqcuired.wait(ulock,[&](){return !newImageAvailable;}); //only take new image after current one was consumed

    Mat captureFrame;
    captureDevice>>captureFrame;
    //transpose(captureFrame,captureFrame);
    //flip(captureFrame,captureFrame,1);
    _img = captureFrame.clone();

    ulock.unlock();
    newImageAvailable = true;
    imageAqcuired.notify_one(); //notify that a new image is available
}
}
void processImage()
{
while (!quit)
{
    unique_lock<mutex> ulock(imageLock);
    imageAqcuired.wait(ulock,[&](){return newImageAvailable;}); //wait untill a new image is available
    _imgToWorkOn = _img.clone();
    ulock.unlock();
    newImageAvailable = false;
    imageAqcuired.notify_one(); //notify the current image can be replaced by a newer one
    unique_lock<mutex> lockFace(facesLock);
    //arbeit
    lockFace.unlock();
}
}

public:

FaceDetector() : newImageAvailable(false) {}
void start() {
quit = false;
thread t1(&FaceDetector::acquireImage,this);
t1.detach();
thread t2(&FaceDetector::processImage,this);
t2.detach();
}
void stop() {
quit = true;
}
Mat getImage() {
if (quit)
    return Mat();
lock_guard<mutex> lock(imageLock);
return _img;
}

Mat getProcessedImage() {
if (quit)
    return Mat();
lock_guard<mutex> lock(facesLock);
return _faceImages;
}

FaceDetector::FaceDetector(int _device_id)
{
VideoCapture captureDevice;
device_id = _device_id;
captureDevice.open(device_id);
captureDevice.set(CV_CAP_PROP_FRAME_WIDTH,620);             //erst jetzt cam.set weil sonst system-pause nicht funzt
captureDevice.set(CV_CAP_PROP_FRAME_HEIGHT,480);
}

};

int main()
{
bool quit(false);
FaceDetector faceDet(0);
faceDet.start();
thread input([](bool &quitFlag) { getchar(); quitFlag = true; },ref(quit)); //stop on     user press Enter
input.detach();
while (!quit) {


Mat img = faceDet.getImage();
Mat imgc = img.clone();

imshow("links", imgc);

/*
imgs = faceDet.getProcessedImage();
Mat imgsc = imgs.clone();

imshow("gsichter", imgsc);
*/
waitKey(30);
this_thread::sleep_for(chrono::milliseconds(33)); //no need to show more than 30 fps...
}
faceDet.stop();
return 0;
}

我也尝试了以下代码,但仍然出现断言错误。

public:
FaceDetector(int device_id) : newImageAvailable(false) {}
....
void init() {
VideoCapture captureDevice;
captureDevice.open(device_id);
captureDevice.set(CV_CAP_PROP_FRAME_WIDTH,620);             //erst jetzt cam.set weil sonst system-pause nicht funzt
captureDevice.set(CV_CAP_PROP_FRAME_HEIGHT,480);
}

int main()
{
FaceDetector faceDet(0);
faceDet.init();
faceDet.start();
}

【问题讨论】:

  • VideoCapture is 已经在它自己的线程中运行。你不会得到任何东西,围绕它缠绕另一个线程。如果您正在进行级联检测,是一个候选者。

标签: c++ multithreading opencv video-capture


【解决方案1】:

main() 函数中,您不是使用构造函数FaceDetector::FaceDetector(int _device_id) 创建对象faceDet,而是使用默认构造函数。这意味着您根本没有打开captureDevice

编辑以进行更正

在声明中,公开FaceDetector::FaceDetector(int _device_id)。 现在在main(),使用这个构造函数创建对象faceDet,你需要这样调用:

FaceDetector faceDet(0); // I have taken 0 as as default camera ID, you can add other value like 1 or 2 etc depending the choice of camera too.

这应该现在可以工作了,如果您遇到任何问题,请告诉我。

【讨论】:

  • 能否请您在代码中包含您的建议或给我一个代码 sn-p?我认为一个类的构造函数必须与该类具有相同的名称,因此不能使用多个私有/公共构造函数......我真的不知道如何将你的答案作为代码包含在内,因为我认为那时我将不得不在默认构造函数中包含我的初始化视频捕获类?我以前从未真正使用过自制课程(功能总是足以满足我的需求),因此在这种情况下我依赖于你的帮助。网上的例子总是只显示简单的东西,比如改变整数..
  • 我尝试包含您的建议,但我仍然收到断言错误。摄像头一直是连着的,所以代码还是有问题的。
  • 当然,你的代码可能有更多的错误。我现在正在旅行。您应该在代码中逐步进行。首先实现单线程并验证,然后再进行多线程。另外,发布您要了解问题的确切错误。
  • 嗨@Echo88,我回来工作了:) 请分享您看到的确切错误,另外,首先让它在单线程中工作。
猜你喜欢
  • 2022-06-10
  • 2023-04-02
  • 2015-06-02
  • 1970-01-01
  • 2022-11-20
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
相关资源
最近更新 更多