問題描述
我正在使用帶有 OpenCV 2.4.7
的 Visual Studio Express 2013,遵循此 教程.
I'm using Visual Studio Express 2013 with OpenCV 2.4.7
, following this tutorial.
我花了幾個小時在網(wǎng)上搜索解決方案,包括所有相關的 SO 問題.我試過了:
I have spent hours searching the web for solutions, including all of the relevant SO questions. I have tried:
VideoCapture::open
的返回值為 1
將 waitKey() 延遲延長至 50 毫秒,然后延長至 500 毫秒
extending the waitKey() delay to 50ms and later 500ms
設置窗口的尺寸
在 Visual C++ 上創(chuàng)建另一個項目
creating another project on Visual C++
打開現(xiàn)有圖像而不是從相機讀取(同樣的錯誤)
opening an existing image instead of reading from camera (same error)
但沒有運氣,請幫忙!
這是我的代碼:
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
Mat image;
VideoCapture cap;
int camOpen = cap.open(CV_CAP_ANY);
namedWindow("window", CV_WINDOW_AUTOSIZE);
while (true) {
cap >> image;
imshow("window", image);
// delay 33ms
waitKey(33);
}
}
當我編譯并運行它時,出現(xiàn)以下錯誤:
As I compiled and ran it, I got the following error:
OpenCV 錯誤:斷言失敗 (size.width>0 && size.height>0) in cv::imshow, file ........opencvmoduleshighguisrcwindow.cpp,第 261 行
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file ........opencvmoduleshighguisrcwindow.cpp, line 261
錯誤發(fā)生在 imshow("window", image);
行.當我評論出來時,沒有人抱怨.
Error occurs at the line imshow("window", image);
. When I commented it out, there are no complaints.
更新:
為什么會發(fā)生此錯誤的合理解釋是我的網(wǎng)絡攝像頭需要時間才能啟動,這就是為什么 image.empty() 最初為 true,因此調(diào)用 abort() 函數(shù)退出程序.
A plausible explanation of why this error occured was that my webcam takes time to start, which is why image.empty() is true initially, hence the abort() function was called to exit the program.
用代碼
if (!image.empty()) {
imshow("window", image);
}
我們可以等待相機啟動
推薦答案
我試過你的代碼,對我來說它有效(它可視化當前的網(wǎng)絡攝像頭輸入)!
我在帶有 OpenCV 2.4.7 的 Visual Studio 2012 Ultimate 上運行它.
...
出現(xiàn)錯誤是因為圖片為空,所以試試這個:
I tried your code and for me it works (it visualizes the current webcam input)!
I ran it on Visual Studio 2012 Ultimate with OpenCV 2.4.7.
...
The error occurs because the image is empty, so try this:
while (true) {
cap >> image;
if(!image.empty()){
imshow("window", image);
}
// delay 33ms
waitKey(33);
}
也許您從網(wǎng)絡攝像頭收到的第一張圖片是空的.在這種情況下 imshow 不會拋出錯誤.所以希望接下來的輸入圖像不是空的.
Maybe the first image you receive from your webcam is empty. In this case imshow will not throw an error. So hopefully the next input images are not empty.
這篇關于斷言失敗(size.width>0 && size.height>0)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!