問(wèn)題描述
我正在使用帶有 OpenCV 2.4.7
的 Visual Studio Express 2013,遵循此 教程.
I'm using Visual Studio Express 2013 with OpenCV 2.4.7
, following this tutorial.
我花了幾個(gè)小時(shí)在網(wǎng)上搜索解決方案,包括所有相關(guān)的 SO 問(wèn)題.我試過(guò)了:
I have spent hours searching the web for solutions, including all of the relevant SO questions. I have tried:
VideoCapture::open
的返回值為 1
將 waitKey() 延遲延長(zhǎng)至 50 毫秒,然后延長(zhǎng)至 500 毫秒
extending the waitKey() delay to 50ms and later 500ms
設(shè)置窗口的尺寸
在 Visual C++ 上創(chuàng)建另一個(gè)項(xiàng)目
creating another project on Visual C++
打開(kāi)現(xiàn)有圖像而不是從相機(jī)讀取(同樣的錯(cuò)誤)
opening an existing image instead of reading from camera (same error)
但沒(méi)有運(yùn)氣,請(qǐng)幫忙!
這是我的代碼:
#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);
}
}
當(dāng)我編譯并運(yùn)行它時(shí),出現(xiàn)以下錯(cuò)誤:
As I compiled and ran it, I got the following error:
OpenCV 錯(cuò)誤:斷言失敗 (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
錯(cuò)誤發(fā)生在 imshow("window", image);
行.當(dāng)我評(píng)論出來(lái)時(shí),沒(méi)有人抱怨.
Error occurs at the line imshow("window", image);
. When I commented it out, there are no complaints.
更新:
為什么會(huì)發(fā)生此錯(cuò)誤的合理解釋是我的網(wǎng)絡(luò)攝像頭需要時(shí)間才能啟動(dò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);
}
我們可以等待相機(jī)啟動(dòng)
推薦答案
我試過(guò)你的代碼,對(duì)我來(lái)說(shuō)它有效(它可視化當(dāng)前的網(wǎng)絡(luò)攝像頭輸入)!
我在帶有 OpenCV 2.4.7 的 Visual Studio 2012 Ultimate 上運(yùn)行它.
...
出現(xiàn)錯(cuò)誤是因?yàn)閳D片為空,所以試試這個(gè):
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)絡(luò)攝像頭收到的第一張圖片是空的.在這種情況下 imshow 不會(huì)拋出錯(cuò)誤.所以希望接下來(lái)的輸入圖像不是空的.
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.
這篇關(guān)于斷言失敗(size.width>0 && size.height>0)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!