2011-11-01 71 views
1

最大的問題在於代碼,不管輸入到VideoCapture cap()的哪個數字,它總是會找到我的筆記本電腦的內部webcap - 或者不再嘗試。 我試過去裝置和停用內部的一個,但 它沒有什麼好處。 (順便說一句,我是新在這個論壇,所以要溫柔。)如何使用OpenCV訪問外部攝像頭?

下面的代碼

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 << "\n\nplease enter at which point (X, Y) you want to rotate the image\n(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); 

} 

}

+0

你在使用什麼操作系統?我懷疑它可能是一個操作系統問題 –

+0

你介意用'C'-接口而不是'C++'並使用'cvCaptureFromCAM(int device)'而不是'VideoCapture'類來設置一個非常簡單的例子嗎? – Atmocreations

+0

爲了避免進一步的故障排除,我重試了內部攝像頭的禁用功能 - 它突然生效。我正在使用Windows 7. 所以得出結論: 設備管理器 - >找到內部攝像頭 - >禁用內部攝像頭。 對不起,浪費你的時間。 –

回答

2

設備管理器(不包括旋轉功能。) - >找到內部攝像頭 - >禁用內部攝像頭。 然後,默認網絡攝像頭將成爲您插入 之後的任何網絡攝像頭。

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