gpt4 book ai didi

c++ - 在另一个多线程类中初始化 OpenCV-VideoCapture-class

转载 作者:行者123 更新时间:2023-11-28 06:43:50 25 4
gpt4 key购买 nike

我试图在我的多线程程序中初始化 VideoCapture 捕获设备一次。一旦初始化,它应该在 acquireImage 线程中用作图像缓冲区,由网络摄像头填充。以下代码向我展示了“OpenCV 错误:断言失败(函数!= 0)在 cv::imshow 中”- 运行时错误,这意味着 VideoCapture capturedevice 从未真正初始化,因此 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();
}

最佳答案

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.

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

关于c++ - 在另一个多线程类中初始化 OpenCV-VideoCapture-class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25416203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com