[英]Capturing camera frames once a while
我的系統通常運行100 HZ或10 ms的掃描時間並執行時間關鍵任務。 我試圖用opencv添加一次相機(取決於用戶與系統交互的時間,因此它可以是從10秒暫停到分鍾的任何地方)捕獲圖像以進行質量控制。 這是我的代碼正在做的事情:
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
UMat frame;
for(;;){
if (timing_variable_100Hz){
cap >> frame; // get a new frame from camera
*Do something time critical*
if(some_criteria_is_met){
if(!frame.empty()) imwrite( "Image.jpg", frame);
}
}
}
return 0;
}
現在我遇到的問題是cap >> frame
需要花費很多時間。
我的掃描時間通常在3毫秒左右,現在是40毫秒。 現在我的問題是,無論如何打開相機,捕捉,然后不必捕捉每一幀后,直到我必須? 我試圖在if(some_criteria_is_met)
移動cap >> frame
,這允許我正確地捕獲第一個圖像,但幾分鍾后拍攝的第二個圖像是第一個捕獲圖像之后的一幀(我希望這是有道理的) )。
謝謝
問題是您的相機的幀速率小於100fps,可能是30fps(根據您測量的32ms),因此請等待新幀可用。
由於無法在opencv中執行非阻塞讀取,我認為您最好的選擇是在另一個線程中執行視頻抓取。
像這樣的東西,如果你使用c ++ 11(這是一個例子,不確定它是完全正確的):
void camera_loop(std::atomic<bool> &capture, std::atomic<bool> &stop)
{
VideoCapture cap(0);
Mat frame;
while(!stop)
{
cap.grab();
if(capture)
{
cap.retrieve(frame);
// do whatever you do with the frame
capture=false;
}
}
}
int main()
{
std::atomic<bool> capture=false, stop=false;
std::thread camera_thread(camera_loop, std::ref(capture), std::ref(stop));
for(;;)
{
// do something time critical
if(some_criteria_is_met)
{
capture=true;
}
}
stop=true;
camera_thread.join();
}
它沒有回答你的問題, are there anyway to open the camera, capture, then not have to capture every frame after until I have to?
,但一個建議
您可以嘗試在后台線程中使用cap >> frame
,后者僅負責捕獲幀。
一旦幀在內存中,將其推送到某種共享循環隊列,以便從主線程訪問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.