【发布时间】:2018-01-04 18:00:06
【问题描述】:
我正在尝试检测眼睛,但我遇到了另一个问题。我无法显示相机画面。问题可能很明显,但我是新手。下面是我的部分代码:
这是我的 EyeDetection.h
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
using namespace cv;
class EyeDetection {
private:
CascadeClassifier eye_cascade, eyepair_cascade;
public:
EyeDetection();
void detect();
};
这是我的 EyeDetection.cpp
#include "EyeDetection.h"
EyeDetection::EyeDetection() {
eye_cascade.load("haarcascade_eye.xml");
eyepair_cascade.load("haarcascade_mcs_eyepair_big.xml");
}
void EyeDetection::detect()
{
VideoCapture webcam(1); //Webcam number is 1
if (eyepair_cascade.empty() || eye_cascade.empty() || !(webcam).isOpened())
return;
webcam.set(CV_CAP_PROP_FRAME_WIDTH, 800);
webcam.set(CV_CAP_PROP_FRAME_HEIGHT, 600);
Mat frame;
while (1) {
webcam >> frame;
if (frame.empty()) continue;
imshow("asad", frame);
}
}
这是我的 Source.cpp(main):
#include "EyeDetection.h"
using namespace cv;
int main(int argc, char** argv)
{
EyeDetection e = EyeDetection();
e.detect();
return 0;
}
它不显示相机框架,它只显示一个空白的灰色窗口。 有什么问题?
【问题讨论】:
-
imshow没有waitKey。阅读the documentation! "这个函数后面要跟waitKey函数,显示指定毫秒的图像,否则不显示图像。" -
感谢您的回答。你是对的,我忘记了 waitKey() 函数。但我还有其他问题。因为,我有多余的台词,我没有提出我的问题。这些行是
webcam.set(CV_CAP_PROP_FRAME_WIDTH, 800);和webcam.set(CV_CAP_PROP_FRAME_HEIGHT, 600);我编辑了这个问题。当我删除这些行并将 waitKey() 函数放在我的代码上时,它就可以工作了。此外,这很有趣,但如果 (width, height)=(320,240),这些行没有问题并且代码有效。实际上,我不明白为什么。 -
你是说当你将帧大小设置为800x600时,你只会不断得到空帧吗?什么样的相机?
-
是的,我将帧大小设置为 800x600。我尝试了自己笔记本电脑的摄像头和另一个网络摄像头(8 mp,不知道它的品牌)。我想,他们的决心是不够的。但是当我在 main 方法中完成所有工作时,它就起作用了。
标签: c++ opencv visual-c++