C++ 用于定期网络摄像头帧捕获的中断计时器c++;
我需要每秒大约30次从网络摄像头捕获一帧。基本上,我正在寻找一种实现以下功能的方法:C++ 用于定期网络摄像头帧捕获的中断计时器c++;,c++,timer,webcam,interrupt,periodic-task,C++,Timer,Webcam,Interrupt,Periodic Task,我需要每秒大约30次从网络摄像头捕获一帧。基本上,我正在寻找一种实现以下功能的方法: mainloop: while( // scan is in process ) { // process current image // discard current image and dequeue next image (pop it off the front of the queue) } interrupt routine: // execute 30 times per s
mainloop:
while( // scan is in process )
{
// process current image
// discard current image and dequeue next image (pop it off the front of the queue)
}
interrupt routine: // execute 30 times per second
{
// capture newImage with webcam
// enqueue newImage (push it onto the back of the queue)
// reset timer and return from interrupt
}
恐怕我没有足够的经验确切地知道我在找什么。如果有人对如何每30秒在背景中拍照有更好的建议,我会很高兴听到。我是新的OpenCV,虽然我有相当数量的C++经验在课堂上。我的项目的最终目标是使用特征检测和匹配来提取每两帧之间的帧到帧变换矩阵(换句话说,跟踪相机在曲面上的运动)
目标操作系统:OSX Yosemite 10.10.4,运行XCode 6.3.1
*最终,这个解决方案将转移到windows平台,因此我希望找到一个不特定于平台(或计算机)的解决方案 大多数相机都是在自己的时钟上拍摄图像的。然后,你是一个奴隶,而不是主人:你不会触发图像捕获。相反,只要有新映像可用,就会通知您。任何摄像头API(OpenCV、Qt多媒体等)都允许您在新的摄像头数据可用时收到通知。如果API没有异步通知,则可以旋转线程并同步执行捕获。比如说,使用OpenCV:
void process(const cv::Mat & frame) { ... }
int main() {
bool quit = false;
std::condition_variable queue_cv;
std::mutex queue_mutex;
std::deque<cv::Mat> queue;
auto capture = cv::VideoCapture(0);
// Worker thread - source of frames
auto thread = std::thread([&]{
int frame_count = 0;
while (! quit) {
cv::Mat frame;
if (! capture.read(frame)) break;
frame_count ++;
if (frame_count >= 30) {
std::unique_lock<std::mutex> lock(queue_mutex);
queue.push_back(frame);
lock.unlock();
queue_cv.notify_one();
frame_count = 0;
}
}
quit = true;
});
// Main thread - consumer of frames
while (!quit) {
std::unique_lock<std::mutex> lock(queue_mutex);
queue_cv.wait(queue_lock, []{ return queue.size() > 0; });
// we own the lock here
auto frame = queue.pop_front();
lock.unlock();
// lock is released, process the frame
process(frame);
}
thread.join();
}
void进程(const cv::Mat&frame){…}
int main(){
bool-quit=false;
std::条件变量队列cv;
std::mutex queue\u mutex;
std::deque队列;
自动捕获=cv::视频捕获(0);
//工作线程-帧的源
自动线程=标准::线程([&]{
整数帧计数=0;
而(!退出){
cv::垫架;
如果(!capture.read(frame))中断;
帧计数++;
如果(帧计数>=30){
std::唯一的锁(队列互斥);
队列。推回(帧);
lock.unlock();
排队通知一个人();
帧计数=0;
}
}
退出=真;
});
//主线程-机架的耗电元件
而(!退出){
std::唯一的锁(队列互斥);
queue_cv.wait(queue_lock,[{return queue.size()>0;});
//这里的锁是我们的
自动帧=queue.pop_front();
lock.unlock();
//释放锁后,处理框架
过程(框架);
}
thread.join();
}
太好了,谢谢!我看看能不能想出那样做的办法。