[英]Error while finding differences in frames from camera
int main(int argc, char* argv[])
{
VideoCapture cap(0);
Mat current_frame;
Mat previous_frame;
Mat result;
Mat frame;
//cap.open(-1);
if (!cap.isOpened()) {
//cerr << "can not open camera or video file" << endl;
return -1;
}
while(1)
{
cap >> current_frame;
if (current_frame.empty())
break;
if (! previous_frame.empty()) {
// subtract frames
subtract(current_frame, previous_frame, result);
}
imshow("Window", result);
waitKey(10);
frame.copyTo(previous_frame);
}
}
當我運行該程序從上一幀中減去當前幀然后顯示結果幀時,它在開始執行時向我顯示此錯誤
WK01.exe中0x755d812f的未處理異常:Microsoft C ++異常:內存位置0x001fe848處的cv :: Exception。
我想在錄制的視頻上應用相同的內容
我認為問題出在previos_frame
。 您只能在循環的和處將值分配給previous_frame
。 我認為在while循環開始時它可能為空,因此
if (! previous_frame.empty()) {
// subtract frames
subtract(current_frame, previous_frame, result);
}
塊將不會執行。
減去時, previous_frame
的大小也必須與current_frame
相同。
此代碼(減法)應確定result
的大小,即您希望在下一行顯示的內容。
在第一幀中,結果為空!
imshow("Window", result); // this will crash
另外,您要將空frame
Mat復制到previous_frame
,應該是current_frame
,不是嗎?
嘗試像:
if (! previous_frame.empty()) {
// subtract frames
subtract(current_frame, previous_frame, result);
imshow("Window", result);
}
waitKey(10);
current_frame.copyTo(previous_frame);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.