内置网络摄像头,opencv:有时有效,有时无效
built-in webcam, opencv: sometimes works, sometimes not
我正在开发visual studio 2010 C++和Opencv 2.3.1。使用HP笔记本电脑windows 7 32位。我确实试过很多次来解决这个问题,但它仍然存在。我的内置网络摄像头有一些代码(有时有效,有时无效),而有一些其他代码,它总是显示灰色窗口,而不是摄像头馈送
有人能帮忙吗?提前谢谢。
例如,第一个代码有时显示凸轮进给,第二个代码总是显示灰色窗口
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if (!cap.isOpened()) // check if we succeeded
return -1;
Ptr<BackgroundSubtractor> pMOG = new BackgroundSubtractorMOG2();
Mat fg_mask;
Mat frame;
int count = -1;
for (;;)
{
// Get frame
cap >> frame; // get a new frame from camera
// Update counter
++count;
// Background subtraction
pMOG->operator()(frame, fg_mask);
imshow("frame", frame);
imshow("fg_mask", fg_mask);
// Save foreground mask
string name = "mask_" + std::to_string(static_cast<long long>(count)) + ".png";
imwrite("D:\SO\temp\" + name, fg_mask);
if (waitKey(1) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
////////////////////////第二个代码:
// WriteVideo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "ERROR: Cannot open the video file" << endl;
return -1;
}
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame Size = " << dWidth << "x" << dHeight << endl;
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
VideoWriter oVideoWriter ("D:/visual outs/mix/WriteVideo.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true); //initialize the VideoWriter object
if ( !oVideoWriter.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
{
cout << "ERROR: Failed to write the video" << endl;
return -1;
}
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "ERROR: Cannot read a frame from video file" << endl;
break;
}
oVideoWriter.write(frame); //writer the frame into the file
imshow("MyVideo", frame); //show the frame in "MyVideo" window
if (waitKey(10) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
如果没有实际代码,我们就无法真正找出问题所在。你能重新表述一下你的问题吗?无论如何,为了在OpenCV中实例化VideoCapture对象,C++代码将非常类似于这个
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break; // Wait 30 ms for a key feed, then break out of the code if a keypress is found in the message queue
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
至于标准C版本,您必须利用旧的但仍然可靠的CvCapture结构,它包含在旧的OpenCV API中。在这种情况下,您将根据cvCreateCameraCapture(0)执行操作,该操作只需返回一个指向新创建的相机实例(CvCapture)的指针。在后一种情况下,代码将评估为下面的snipper。
# include "highgui.h"
# include "cv.h"
int main( int argc, char** argv )
{
CvCapture* capture;
capture = cvCreateCameraCapture(0);
if (!capture)
return -1;
IplImage* bgr_frame = cvQueryFrame(capture); // Query the next frame
CvSize size = cvSize(
(int)cvGetCaptureProperty(capture,
CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(capture,
CV_CAP_PROP_FRAME_HEIGHT)
);
cvNamedWindow("OpenCV via CvCapture", CV_WINDOW_AUTOSIZE); // Create a window and assign it a title
/* Open a CvVideoWriter to save the video to a file. Think of it as a stream */
CvVideoWriter *writer = cvCreateVideoWriter(argv[1],
CV_FOURCC('D','I','V','X'),
30,
size
);
while((bgr_frame = cvQueryFrame(capture)) != NULL)
{
cvWriteFrame(writer, bgr_frame);
cvShowImage("OpenCV via CvCapture", bgr_frame);
char c = cvWaitKey(30); // Same as the snippet above. Wait for 30 ms
if(c == 27) break; // If the key code is 0x27 (ESC), break
}
cvReleaseVideoWriter(&writer); // Dispose the CvVideoWriter instance
cvReleaseCapture(&capture); // Dispose the CvCapture instance
cvDestroyWindow("OpenCV via CvCapture"); // Destroy the window
return 0;
}
相关文章:
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 枚举进程模块在有效句柄上返回无效句柄
- 为什么在引用指针时将 const 放在 & 符号的左侧有效,而在右侧则无效?
- C++14 : 2 个随机生成器 - 一个有效,另一个无效
- 使用 CMake 与其他静态库链接的静态库 - 一个有效,一个无效。为什么?
- 为什么构造函数的虚拟函数调用有时有效,但其他调用却无效
- 类外的前向声明有效,但在嵌套时无效
- 为什么第二个代码有效而第一个代码无效?
- 将文本文件读取为 const char* 有时有效,有时无效
- reinterpret_cast可以将无效的指针值转换为有效的指针值
- 为什么C++隐式转换有效,而显式转换无效?
- (C++)比较两段代码,一段有效,一段无效,无法找出区别
- 如何说一个有效或无效,即使一个是,另一个不是
- 正则表达式有效和无效的切换
- 将流定义为私有类变量似乎在Linux下有效,但在WindowsVisualStudio下无效
- 验证用户输入是否有效 [从 'char' 到 'char*' 的转换无效]
- 在C++中嵌入Python:在Python脚本中导入模块在一个函数调用过程中有效,但在另一个调用过程中无效
- 当从一个应用程序调用时,在DLL方法中创建COM接口指针是有效的,但当从另一个应用软件调用时则无效
- 为什么这个取消注释的宏在 clang 中无效,但在 msvc 中有效
- Tensorflow模型在Python中有效,但在C++中无效