gpt4 book ai didi

c++ - 用于定期网络摄像头帧捕获的中断定时器 C++

转载 作者:行者123 更新时间:2023-11-30 05:40:32 25 4
gpt4 key购买 nike

我需要每秒大约 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 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();
}

关于c++ - 用于定期网络摄像头帧捕获的中断定时器 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31575782/

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