【问题标题】:built-in webcam, opencv: sometimes works, sometimes not内置网络摄像头,opencv:有时有效,有时无效
【发布时间】:2015-11-14 19:45:35
【问题描述】:

我正在开发 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;

}

【问题讨论】:

  • 哪些代码?您能否详细说明并添加一些代码?
  • 感谢@Miki,现在我添加了 2 个代码
  • 我工作正常。尝试删除与VideoWriter 相关的代码。问题可能就在于此。
  • 两者都正常工作?
  • 第二个没有 VideoWriter 的东西,可能我缺少一些编解码器。检查它是否也适用于没有 VideoWritere 的东西。如果是这样,您就知道问题出在哪里了。

标签: c++ opencv


【解决方案1】:

没有实际代码,我们无法真正查明问题所在。你能重新提出你的问题吗?无论如何,为了在 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 struct,它包含在旧的OpenCV API。在这种情况下,您将作用于 cvCreateCameraCapture(0),它只返回一个指向新创建的相机实例 (CvCapture) 的指针。在后一种情况下,代码将评估为 hereunder 的 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;
}

【讨论】:

  • 在您发布之前,代码已添加到问题中。这没有回答问题。
  • 太糟糕了,我正忙着写回复。你的观点也站不住脚,所以当你不确定时不要发表评论。
  • @Zakarya 你的电脑里只有一个摄像头吗?您确定 VideoWriter 访问的路径不受保护吗?这可能是由于权限错误,即使它仍然应该告诉您流未打开。我认为三木是对的
  • 感谢您的回复,但现在,调试甚至没有启动框架窗口。我猜是相机本身导致了问题,因为它现在挂了,而且很多时候我需要重新启动电脑才能关闭相机
  • 嗯,我一开始就是这么想的。很高兴随时提供帮助!
猜你喜欢
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-03
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
  • 2010-10-17
相关资源
最近更新 更多