2017-04-04 3 views
1

웹캠, opencv 및 emgucv를 사용하여 사진을 캡처 할 때 특별한 문제가 있습니다.캡쳐 한 장 찍기 전에 - 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 함수()는 일부 버튼있게 다음은 타이머를 사용하는 코드이다.

문제는 다음과 같습니다. 첫 번째 사진 찍기 - 그림 상자에 첫 번째 그림이 표시됩니다. 두 번째 사진 찍기 - 첫 번째 사진을 다시 보여줍니다. 세 번째 그림을 찍습니다 - 그림 상자는 두 번째 그림을 보여줍니다. 네 번째 사진을 찍습니다 - 그림 상자는 세 번째 사진을 보여줍니다. 어떤 도움 ... ... 촬영 후, 난 단지 색상을 인식하고 흰색에 그 색과 검은 색 사진의 나머지 부분을 (다른 사람이 설명을 필요로하는 경우) 표시하고 있습니다 는

감사합니다 너 나 줄 수있어! 나는 함수의 끝 부분에이 라인을 추가하는 경우 :

은 편집 = 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 캡처. 단추를 클릭 할 때 한 프레임 만 캡처하므로 라이브 웹캠 스트림이 없기 때문에 사용 후 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

반가워요! 지연은 웹캠을 다시 시작해야하기 때문입니다. 그것은 조금 걸립니다. – Jurjen

관련 문제