Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Unexpected interaction between Swing and VideoCapture?

I've come across what I believe is a weird interaction between Swing and the VideoCapture class in OpenCV, and I was wondering if anyone either had an explanation or at least could duplicate my issue.

I've included minimal version of my code below.

Essentially, this code opens a VideoCapture and sets up a JFrame. It sets the Jframe to show an image, then reads in the video using VideoCaptue, and attempts to read a number of frames more than there are actual frames in the video. It then changes the JFrame to show a different image. However, it doesn't work correctly. If the two lines involving the VideoCapture are not commented out, once the VideoCapture reads a blank frame from the video, the JFrame closes. This is despite the fact that the VideoCapture never interacts with any of the content in the JFrame. The final line in the code, where we attempt to show something else, doesn't work. The JFrame remains closed, and that image is not shown. When the lines involving the VideoCapture are commented out, this runs as expected, it shows the first image briefly, runs through the loop (doing essentially nothing), and then shows the second image.

Any thoughts on what exactly is happening would be appreciated. I'm on a Mac, coding in Scala in IntelliJ, using OpenCV 3.0 with the official java wrapper. If not thoughts, if anyone on a Mac could test this, that'd be appreciated too.

class scalaTest {
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME)

  def runTest() : Unit = {
    //val reader = new VideoCapture("video.mp4")
    val frm : JFrame = new JFrame("TestWindow")
    val lbl : JLabel = new JLabel()
    frm.add(lbl)
    frm.pack()
    frm.setVisible(true)
    frm.setSize(600,600)

    val imgIcon = new ImageIcon("image1.jpg")
    lbl.setIcon(imgIcon)
    var img : Mat = new Mat()

    var count : Int = 0

    // Video is 84 frames, so this overshoots it
    while (count <=100) {
     // reader.read(img)
      println(count)
      count += 1
    }

    //Show something else
    lbl.setIcon(new ImageIcon("image2.jpg"))

    //Other code here
  }
}