Unexpected interaction between Swing and VideoCapture?

asked 2017-06-25 17:38:16 -0500

gde1 gravatar image

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 {

  def runTest() : Unit = {
    //val reader = new VideoCapture("video.mp4")
    val frm : JFrame = new JFrame("TestWindow")
    val lbl : JLabel = new JLabel()

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

    var count : Int = 0

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

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

    //Other code here
edit retag flag offensive close merge delete


berak gravatar imageberak ( 2017-06-25 20:47:44 -0500 )edit

I cleaned it up and further clarified my question. It's really not that there's an issue with Swing and VideoCapture, it's that I think there's some weird interaction going on. I'll delete the older one.

gde1 gravatar imagegde1 ( 2017-06-25 21:11:51 -0500 )edit