C++ 在C+中线程化OpenCV VideoCapture对象+;
我正在使用Python中的以下代码,以便通过一个专用线程从OpenCV VideoCapture对象连续获取帧,并且工作正常C++ 在C+中线程化OpenCV VideoCapture对象+;,c++,multithreading,opencv,C++,Multithreading,Opencv,我正在使用Python中的以下代码,以便通过一个专用线程从OpenCV VideoCapture对象连续获取帧,并且工作正常 from threading import Thread import cv2 import imutils class VideoGet: def __init__(self, src=0): self.stream = cv2.VideoCapture(src) (self.grabbed, img) = self.stre
from threading import Thread
import cv2
import imutils
class VideoGet:
def __init__(self, src=0):
self.stream = cv2.VideoCapture(src)
(self.grabbed, img) = self.stream.read()
self.frame = imutils.resize(img, height=416, width=416)
self.stopped = False
def start(self):
Thread(target=self.get, args=()).start()
return self
def get(self):
while not self.stopped:
if not self.grabbed:
self.stop()
else:
(self.grabbed, img) = self.stream.read()
try:
self.frame = imutils.resize(img, height=416, width=416)
except:
self.stopped =True
def stop(self):
self.stopped = True
我现在尝试通过创建一个VIEGGET类来处理C++图像中类似的东西,但是我很难做到。以下是我迄今为止的尝试:
头文件
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <thread>
class VideoGet {
public:
VideoGet(int src);
cv::VideoCapture stream;
cv::Mat img, frame;
bool stopped;
std::thread start();
void get();
void stop();
}
你能帮我弄清楚怎么弄清楚吗
提前谢谢
#include "videoget.h"
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <thread>
using namespace cv;
using namespace std;
VideoGet::VideoGet(int src) {
Mat img;
this->stream = VideoCapture(src);
this->img = this->stream.read();
resize(this->img, this->frame, Size(416, 416));
this->stopped = false;
}
thread VideoGet::start() {
thread t(VideoGet::get());
return t;
}
void VideoGet::get() {
while (this->stopped = false)
{
if (this->frame.empty())
{
VideoGet::stop();
}
else
{
this->stream.read();
try
{
resize(this->img, this->frame, Size(416, 416));
}
catch(...)
{
this->stopped = true;
}
}
}
}
void VideoGet::stop() {
this->stopped = true;
}
1>c:\users\antho\documents\c++\thread_demo\thread_demo\thread_demo\videoget.cpp(6): error C2059: syntax error: 'using namespace'
1>c:\users\antho\documents\c++\thread_demo\thread_demo\thread_demo\videoget.cpp(11): error C2065: 'Mat': undeclared identifier
1>c:\users\antho\documents\c++\thread_demo\thread_demo\thread_demo\videoget.cpp(11): error C2146: syntax error: missing ';' before identifier 'img'
1>c:\users\antho\documents\c++\thread_demo\thread_demo\thread_demo\videoget.cpp(12): error C3861: 'VideoCapture': identifier not found
1>c:\users\antho\documents\c++\thread_demo\thread_demo\thread_demo\videoget.cpp(13): error C2660: 'cv::VideoCapture::read': function does not take 0 arguments
1>d:\opencv\build\install\include\opencv2\videoio.hpp(768): note: see declaration of 'cv::VideoCapture::read'
1>c:\users\antho\documents\c++\thread_demo\thread_demo\thread_demo\videoget.cpp(14): error C3861: 'Size': identifier not found
1>c:\users\antho\documents\c++\thread_demo\thread_demo\thread_demo\videoget.cpp(21): error C2665: 'std::thread::thread': none of the 3 overloads could convert all the argument types
1>d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\thread(70): note: could be 'std::thread::thread(const std::thread &)'
1>d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\thread(59): note: or 'std::thread::thread(std::thread &&) noexcept'
1>d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\thread(36): note: or 'std::thread::thread(void) noexcept'
1>c:\users\antho\documents\c++\thread_demo\thread_demo\thread_demo\videoget.cpp(21): note: while trying to match the argument list '(void)'
1>c:\users\antho\documents\c++\thread_demo\thread_demo\thread_demo\videoget.cpp(35): error C2660: 'cv::VideoCapture::read': function does not take 0 arguments
1>d:\opencv\build\install\include\opencv2\videoio.hpp(768): note: see declaration of 'cv::VideoCapture::read'
1>c:\users\antho\documents\c++\thread_demo\thread_demo\thread_demo\videoget.cpp(38): error C3861: 'Size': identifier not found
1>Done building project "thread_demo.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========