2013-06-18 68 views
3

我使用EmguCV的Capture類從We​​bCam中獲取圖像。設置捕獲設備EmguCV

根據該課程的文檔(http://www.emgu.com/wiki/files/2.0.0.0/html/18b6eba7-f18b-fa87-8bf2-2acff68988cb.htm),Capture擁有3個構造函數。

使用public Capture()它應該使用默認的攝像頭,它的工作正常。

正如我在其中一個例子所看到的,似乎

public Capture(string fileName) //takes a video file as the source for the captures. 

最後一個構造函數是

public Capture(int camIndex) //which is supposed to "Create a capture using the specific camera" 

我試圖用這最後一個構造函數允許用戶選擇的情況下,該裝置他有多個相機(例如,筆記本電腦中的集成相機或插入的USB攝像頭)

我的問題是我不知道如何獲得可用設備的列表。試圖創建索引從0到99的捕獲,並試圖抓住一個期待異常的框架,但它只需要100個捕獲的黑色圖像。另外,當我使用默認相機時,我不知道如何獲得他的索引。

任何幫助?

編輯:隨着溼婆的答案的信息,我得到了它與這方面的工作(我將它張貼以備將來參考):

private void onLoad(object sender, RoutedEventArgs e) 
{ 
    //Add the image processing to the dispatcher 
    this.Dispatcher.Hooks.DispatcherInactive += new EventHandler(dispatcherTimer_Tick); 

    //Get the information about the installed cameras and add the combobox items 
    DsDevice[] _SystemCamereas = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); 
    Video_Device[] WebCams = new Video_Device[_SystemCamereas.Length]; 
    for (int i = 0; i < _SystemCamereas.Length; i++) 
    { 
     WebCams[i] = new Video_Device(i, _SystemCamereas[i].Name, _SystemCamereas[i].ClassID); //fill web cam array 
     ComboBoxDevices.Items.Add(WebCams[i].ToString()); 
    } 
} 

private void dispatcherTimer_Tick(object sender, EventArgs e) 
{ 
    if (capture != null) 
    { 
     //Capture an image 
     Image<Bgr, byte> img = capture.QueryFrame(); 
     //Show the image in the window 
     ImageOriginal.Source = ImageProcessor.ToBitmapSource(img); 
    } 
} 

private void ComboBoxDevices_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    //If there is already a capture, dispose it 
    if (capture != null) 
    { 
     capture.Dispose(); 
    } 
    //Get the selected camera 
    int selectedDevice = ComboBoxDevices.SelectedIndex; 
    try 
    { 
     //Create new capture with the selected camera 
     capture = new Capture(selectedDevice); 
    } 
    catch (Exception excpt) 
    { 
     MessageBox.Show(excpt.Message); 
    } 
} 

回答

3

捕獲物可用於使用下面的代碼

Capture grabber = new Emgu.CV.Capture(@".\..\..\file.avi");//can be relative path or absolute path of the video file. 

爲了找到連接網絡攝像頭的名單將需要進口類似的Direct Show給靜態文件輸入(DirectShow.Net.dll)到項目中並使用以下代碼檢索連接的網絡攝像頭列表。

DsDevice[] _SystemCamereas = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); 
    Video_Device[] WebCams = new Video_Device[_SystemCamereas.Length]; 
     for (int i = 0; i < _SystemCamereas.Length; i++) 
     { 
      WebCams[i] = new Video_Device(i, _SystemCamereas[i].Name, _SystemCamereas[i].ClassID); //fill web cam array 
      Camera_Selection.Items.Add(WebCams[i].ToString()); 
     } 

檢查此鏈接的全部代碼 http://www.emgu.com/wiki/index.php?title=Camera_Capture

此列表可以被填充到一個組合框和每個連接的設備可以被選擇爲檢索來自特定裝置的視頻輸入端。

示例可以在這裏找到: http://fewtutorials.bravesites.com/entries/emgu-cv-c/level-2---use-multiple-cameras-in-one-application

爲了您的最後一個問題,默認的攝像頭總是具有0 的索引,以便用於初始化與默認攝像機捕捉對象,你將不得不使用下面的代碼

Capture grabber = new Emgu.CV.Capture(0); 
+1

感謝您的回覆。檢查完這些例子後,我終於找到了它。 – Evans

1

審視EMGU CV source似乎表明,它只是路過作爲cvCreateCameraCapture(int index)函數的一部分,關閉底層OpenCV庫的索引。該功能是... A bit of a mess of #ifdefs,但是從我所能看到的(以及註釋所表示的內容)中,索引用於指定所需的相機以及它應該使用的API。

嘗試連續嘗試百的倍數;每個應該使用不同的編解碼器,試圖使用第一臺攝像機。這可能是因爲您的某個API列入了您的OpenCV副本,但在您的系統上無法正常工作。

編輯:進一步深入,好像它在this函數調用,它使用MFEnumDeviceSources函數來獲取列表結束。您想要的設備然後退出該列表(請參閱getDevice function a few lines higher up)。因此,在我看來,您在評論中提到的對話框是Windows MediaFoundation內容的一部分,在這種情況下,您可能需要Google信息的措辭,查看一些具有MF經驗的人是否可以將您指向正確的方向。

+0

我試圖用一百年的倍數並發現對於一些值(在我的情況下爲0,200和700),它找到了一個有效的驅動程序。但是,如果我安裝了多臺攝像機,它會顯示一個窗口,詢問我是否想使用該攝像機。適用於從0到99,200到299,700到799(全部採用正確捕捉)的每個值,而不是使用索引來使用具體的相機。 – Evans