简体   繁体   English

opencv videocapture c ++无法正常工作2次

[英]opencv videocapture c++ not working 2 times

I tried the following code for capturing a video from my webcam: 我尝试使用以下代码从网络摄像头捕获视频:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

int main()
{
    VideoCapture cap(0); // open the video camera no. 0

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video cam" << endl;
        return -1;
    }


    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;

    namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
    namedWindow("Changed", CV_WINDOW_AUTOSIZE);

    while (1)
    {
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }

        Mat imgH = frame + Scalar(75, 75, 75);
        imshow("MyVideo", frame); //show the frame in "MyVideo" window
        imshow("Changed", imgH);

        if (waitKey(30) == 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;

}

Now here's my problem: After debugging that program for the first time everything works as expected. 现在这是我的问题:首次调试该程序后,一切正常。 But when debugging for a second time (after changing some lines in the code) it cannot read from the camera. 但是,当第二次调试时(在代码中更改了一些行之后),它无法从相机读取。

Does anyone have a hint for me how to solve that problem? 有人对我有提示如何解决这个问题吗?

Thanks! 谢谢!

The code you posted seems to be working absolutely fine in my case, and the output is as intended. 在我的情况下,您发布的代码似乎运行得很好,并且输出符合预期。 However please make sure that your webcam is switched on before you run the program, this is important. 但是,在运行程序之前,请确保已打开网络摄像头,这一点很重要。 Since i have a YouCam client in my computer for the webcam, therefore it shows that i need to start youcam. 由于我的计算机中有一个用于网络摄像头的YouCam客户端,因此它表明我需要启动youcam。

Since i dont have enough reputation to post an image, so please see the following link in order to view the output i got when webcam not already switched on. 由于我没有足够的声誉来发布图像,因此请查看以下链接,以查看未打开网络摄像头时得到的输出。

http://i.imgur.com/h4bTZ7z.png http://i.imgur.com/h4bTZ7z.png

Hope this helps!! 希望这可以帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM