2017-04-04 7 views
1

私のウェブカメラopencvとemgucvを使って画像をキャプチャする際に特に問題があります。キャプチャを1つ取ってから表示する - VB - OpenCV

私は以前にこの機能を使用していましたが、いつもうまく機能しましたが、今はなぜピクチャボックスに以前撮った写真が表示されているのか分かりません。

私はプログラムを開始します - 私はボタンを押して、画像が撮られるまで数秒待っています - img = capturez.QueryFrame()を使って画像を撮ります - PictureBoxに画像を表示します。ここでは、コードされています

Private Sub startButton_Click() Handles startButton.Click 

    Dim timeToWait As Integer 
    PictureBox1.Image = Nothing 
    If globalGMT <> Nothing And globalLatitude <> Nothing And globalLongitude <> Nothing Then 
     'If TextBox_GMT.Text IsNot Nothing And TextBox_LAT.Text IsNot Nothing And TextBox_LON.Text IsNot Nothing Then 
     SunPosition(globalGMT, globalLatitude, globalLongitude, ELEVACIONDELSOL, AZIMUTDELSOL) 
    End If 
    startButton.Enabled = False 
    SetDefaultTimeButton.Enabled = False 
    SetParameters.Enabled = False 
    TextBoxTime.Text = System.DateTime.UtcNow 

    timeToWait = globalTimeLeft 

    For i = 0 To timeLeft 
     wait() 
     i += 1 
     timeToWait -= 1 
     timeLabel.Text = timeToWait & " seconds" 
    Next 

    Dim img As Image(Of Bgr, Byte) = capturez.QueryFrame() 

    For x = 0 To img.Width - 1 
     For y = 0 To img.Height - 1 
      Dim pixelColor As Bgr = img(y, x) 

      If (pixelColor.Blue >= 200 And pixelColor.Blue <= 255) And 
       (pixelColor.Green >= 200 And pixelColor.Green <= 255) And (pixelColor.Red >= 200 And pixelColor.Red <= 255) Then 
       pixelColor.Blue = 255 
       pixelColor.Green = 255 
       pixelColor.Red = 255 
       img(y, x) = pixelColor 
      Else 
       pixelColor.Blue = 0 
       pixelColor.Green = 0 
       pixelColor.Red = 0 
       img(y, x) = pixelColor 
      End If 
     Next 
    Next 

    PictureBox1.Image = img.ToBitmap 

    startButton.Enabled = True 
    SetParameters.Enabled = True 
    SetDefaultTimeButton.Enabled = True 
    SetForm() 

End Sub 

機能待ち()は次のようになります。

Private Sub wait() 
    Dim seconds As Integer = 1 
    For i As Integer = 0 To seconds * 100 
     System.Threading.Thread.Sleep(10) 
     'Application.DoEvents() 
    Next 
End Sub 

たぶん、あなたはなぜあなたはそのためにタイマーを使用していない、尋ねることができますか?タイマーではまったく同じ問題があるからです。

'This function will start the activity of the form 
Private Sub startButton_Click() Handles startButton.Click 

    Dim timeToWait As Integer 
    PictureBox1.Image = Nothing 
    If globalGMT <> Nothing And globalLatitude <> Nothing And globalLongitude <> Nothing Then 
     'If TextBox_GMT.Text IsNot Nothing And TextBox_LAT.Text IsNot Nothing And TextBox_LON.Text IsNot Nothing Then 
     SunPosition(globalGMT, globalLatitude, globalLongitude, ELEVACIONDELSOL, AZIMUTDELSOL) 
    End If 
    startButton.Enabled = False 
    SetDefaultTimeButton.Enabled = False 
    SetParameters.Enabled = False 
    TextBoxTime.Text = System.DateTime.UtcNow 
    StartButtonTimer.Start() 
End Sub 

'This function will start the timer of the form 
Private Sub StartButtonTimer_Tick() Handles StartButtonTimer.Tick 
    Dim X As Integer 
    Dim Y As Integer 

    If timeLeft > 0 Then 
     timeLeft -= 1 
     timeLabel.Text = timeLeft & " seconds" 

     'DLE prueba tomar foto después del tiempo especificado - pongo a negro el fondo del picturebox 
     PictureBox1.BackColor = Color.Black 
    Else 

     'DLE prueba tomar foto después del tiempo especificado - hago foto de lo que ve la camara 
     Dim img As Image(Of Bgr, Byte) = capturez.QueryFrame() 

     For X = 0 To img.Width - 1 
      For Y = 0 To img.Height - 1 
       Dim pixelColor As Bgr = img(Y, X) 

       If (pixelColor.Blue >= 200 And pixelColor.Blue <= 255) And 
        (pixelColor.Green >= 200 And pixelColor.Green <= 255) And 
        (pixelColor.Red >= 200 And pixelColor.Red <= 255) Then 
        pixelColor.Blue = 255 
        pixelColor.Green = 255 
        pixelColor.Red = 255 
        img(Y, X) = pixelColor 
       Else 
        pixelColor.Blue = 0 
        pixelColor.Green = 0 
        pixelColor.Red = 0 
        img(Y, X) = pixelColor 
       End If 
      Next 
     Next 
     StartButtonTimer.Stop() 
     PictureBox1.Image = img.ToBitmap 
     startButton.Enabled = True 
     SetParameters.Enabled = True 
     SetDefaultTimeButton.Enabled = True 
     SetForm() 
    End If 
End Sub 

関数SETFORM()のみいくつかのボタンが有効になります。ここでは、タイマーを使用してコードがあります。

問題は次のとおりです。 私は最初の画像を撮ります - 画像ボックスは最初の画像を表示します。 私は2番目の写真を撮る - ピクチャボックスは再び最初の写真を表示する。 私は第3の写真を撮る - ピクチャボックスは第2の写真を表示する。 私は4番目の写真を撮る - ピクチャボックスは3番目の写真を表示します。 ... ... 写真を撮影した後、私は色だけを認識していますし、(誰かがこの説明を必要とする場合)、黒に白や画像の残りの部分でその色を示し、任意の助け

感謝あなたは私を与えることができます!

編集:私は関数の最後に次の行を追加する場合:= capturez.QueryFrame((BGR、バイト)画像)として暗いイメージ、それがうまく機能:

... 
     ... 
     Next 
     StartButtonTimer.Stop() 
     PictureBox1.Image = img.ToBitmap 
     startButton.Enabled = True 
     SetParameters.Enabled = True 
     SetDefaultTimeButton.Enabled = True 
     SetForm() 
    End If 

    Dim image As Image(Of Bgr, Byte) = capturez.QueryFrame() 

End Sub 

エンドクラス

この最後の変数は何も使用しません。私はそれを使用せずに宣言しています。私はなぜこの行でうまくいくのか分かりませんし、消去するとうまくいきません。

+0

は、クイック検索は、() '犯人としてOpenCV.QueryFrame'を指すヒットの多くを思い付きました。 One [solution](http://stackoverflow.com/a/7517099/2330053)は基本的に 'QueryFrame()'を連続的にポーリングすることを提案しています。 –

+0

あなたの返信ありがとう!最終的に最後に新しいQUeryFrameを使用することにしました。完全に機能するので、私はそのソリューションを使用するつもりです。助けや返事をありがとう!ありがとう! –

答えて

1

これについては、私はしばらく時間がかかりましたが、 t Disposeあなたのキャプチャ。ボタンをクリックするだけで1つのフレームをキャプチャするので(ライブカメラのストリームがないため)、使用後にCaptureオブジェクトのDisposeを取得する必要があります。現在、新しいフレームを要求すると、Captureオブジェクトは古いフレームを返し、次の使用の準備が整った別のフレームを取得し始めます。したがって、別のQueryFrame()をメソッドの最後に配置すると、古いフレームが上書きされるため、再び機能します。

これはあなたの問題を解決する必要があります。

'Make sure you can use your retrieved image even after the dispose 
Dim img As Image(Of Bgr, Byte); 

'Create a new capture object 
Using capturez As New Capture 
    'Since it's a new object the webcam is forced to retrieve a frame now 
    'Also, use Copy() to make sure your image is still available after diposing 
    img = capturez.QueryFrame().Copy() 
End Using 'Dispose the object now 

'Now do with your img object whatever you want! 
+0

あなたの答えをありがとう!あなたが言っていることをブロックを使ってやってみて、それは完璧に動作します。この解決策の問題は、キャプチャが1〜2秒遅れることです。理由はわかりません。他のソリューションについては、別のQueryFrame()を最後に置くことは、完璧に機能し、キャプチャを遅らせることはないので、私はそのソリューションを使用するつもりだと思います。どうもありがとう! –

+0

よろしくお願いします! Webカメラを再起動する必要があるためです。それは少し時間がかかります。 – Jurjen

関連する問題