我如何使用OpenCV访问外部网络摄像头?

How do I access an external webcam with OpenCV?

本文关键字:网络 摄像头 外部 访问 何使用 OpenCV      更新时间:2023-10-16

最大的问题在于代码,无论我在VideoCapture cap()中输入哪个数字,它总是会找到我笔记本电脑的内部webcap -或者尝试失败。我试过进入设备关闭内部的那个,但是这并没有起到什么作用。(顺便说一下,我是这个论坛的新手,所以要温柔。)

下面是代码(不包括旋转函数)

int main()
{   
    // NOW WITH WEBCAM INPUT!!
    VideoCapture cap(0);//capture image from webcam
    cap.open(true); 
    Mat image;
    cap>>image; //applying the captured image to Mat image
    //Mat image = imread("Tulips.jpg"); // reading the image
    double degrees; // Number of degrees we want to rotate the image
    double oregoX = image.cols / 2; //X-center of the image
    double oregoY = image.rows / 2; //Y-center of the image
    //user inputs
    cout << "please enter the number of degrees you wish to turn the image" << endl;
    cin >> degrees;
    cout << "nnplease enter at which point (X, Y) you want to rotate the imagen(make space not comma)n" << endl;
    cout << "The center of the image is at " << oregoX << ", " << oregoY << endl;
    cin >> oregoX >> oregoY;
    while (true){
        cap>>image;
        if (!image.data) break;
        if (waitKey(30) >= 0) break;
        cvtColor(image, image, CV_8U); //Converting image to 8-bit
        Mat grayImage; //creating a canvas for the grayscale image
        cvtColor(image, grayImage, CV_RGB2GRAY); //creating the grayscale image
        // Here we create a canvas with the same size as the input image, later to put in the rotated data
        Mat imageOut(grayImage.rows, grayImage.cols, CV_8U);
        Rotation(grayImage, imageOut, degrees, oregoX, oregoY); //Performing the rotation on the image using the Rotation() funcion
        imshow("The Gray Image", grayImage);
        imshow("The rotated image", imageOut);
    }
}

设备管理器->查找内部摄像头->禁用内部摄像头。默认的网络摄像头将变成你插入的任何一个网络摄像头div。

VideoCapture cap(1); //capture image from other webcam