0

OpenCV 的哪些功能支持模拟摄像头进行直播?

请给我答案。

我尝试了 OpenCV 的 CvCapture 和 VideoCapture 功能。它适用于 USB 相机,但不适用于模拟相机。

我尝试了 VideoInput 功能。它适用于模拟摄像机,但我想要 opencv 内置功能。所以请帮助我。谢谢你的时间。

这是我使用的 cvcapture 函数的代码。

#include "stdafx.h"
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include "videoInput.h"
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

char key;
int main()
{
    cvNamedWindow("Camera_Output", 1);    //Create window
    CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);  //Capture using any camera              connected to your system
    while(1){ //Create infinte loop for live streaming

        IplImage* frame = cvQueryFrame(capture); //Create image frames from capture
        cvShowImage("Camera_Output", frame);   //Show image frames on created window
        key = cvWaitKey(10);     //Capture Keyboard stroke
        if (char(key) == 27){
            break;      //If you hit ESC key loop will break.
        }
    }
    cvReleaseCapture(&capture); //Release capture.
    cvDestroyWindow("Camera_Output"); //Destroy Window
    return 0;
}

代码结束

下一个代码是使用 VideoInput 函数。

代码是

#include "stdafx.h"
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include "videoInput.h"
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

int main(int, char**)
{
   videoInput VI;
    int numDevices = VI.listDevices();
    //int device1= 0;
    VI.setup(0);

    unsigned char* yourBuffer = new unsigned char[VI.getSize(0)];

    IplImage* colourImage = cvCreateImage(cvSize(320,240),8,3);

    while(1)
    {
        VI.grabFrame(0, yourBuffer);
        colourImage->imageData = (char*)yourBuffer;
        cvConvertImage(colourImage, colourImage, 3);

        cvShowImage("test",colourImage);

        if( cvWaitKey(10) == 27) 
            break;
    }
    return 0;
}

代码结束

最后一个代码是使用视频捕捉功能

代码是

#include "stdafx.h"
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include "videoInput.h"
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

int main() {
VideoCapture stream1(-1);   //0 is the id of video device.0 if you have only one camera.

    if (!stream1.isOpened()) { //check if video device has been initialised
        cout << "cannot open camera";
        return 0;
    }

//unconditional loop
    while (true) {
        Mat cameraFrame;
        stream1.read(cameraFrame);
        imshow("cam", cameraFrame);
        if (waitKey(30) >= 0)
        break;
    }
    return 0;
}

代码结束

请帮我。

4

0 回答 0