内置网络摄像头,opencv:有时有效,有时无效

built-in webcam, opencv: sometimes works, sometimes not

本文关键字:有效 无效 opencv 网络 摄像头 内置      更新时间:2023-10-16

我正在开发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;
}
相关文章: