Issue with OpenCV and Swing - Video Display [closed]

asked 2017-06-15 21:52:56 -0500

gde1 gravatar image

I've been messing around with OpenCV, and have an issue with displaying images through Swing.

I'm using Swing, and was running into an issue where my JFrame and JPanel would close after I'd finished reading my video. After a good amount of debugging, this issue appears to be an interaction between the OpenCV VideoCapture and the JFrame. Once VideoCapture reads an empty frame, the JFrame closes, regardless of whether it has any link to the VideoCapture. Here's the simplest code I could write which still shows the issue. When the three commented lines (excluding the while loop) below are commented, this runs fine, and the JFrame stays up until I close it. When the three commented lines below are uncommented, the JFrame closes as soon as the reader reads a blank frame.

Note that there's two while loop lines, that was on purpose to test. The video is 84 frames.

If anyone could run this and see if it happens for them too, I'd appreciate it (or just explain why this happens).

I'm using Scala in IntelliJ on a Mac.

class scalaTest {
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME)

def runTest() : Unit = {
  //val reader = new VideoCapture("src/main/resources/myVideo.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("src/main/resources/testimage.jpg")
  lbl.setIcon(imgIcon)
  var img : Mat = new Mat()

  var count : Int = 0

  //reader.read(img)
  //while (!img.empty()) {
  while (count <=250) {
    //reader.read(img)
    println(count)
    count += 1
  }
}
edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by gde1
close date 2017-06-25 21:12:20.459908

Comments

this code is not complete, right ? (you never draw the frame, or such ?)

but yes, the last frame from the capture will be empty() (the movie's over), so your loop breaks out, and the test finishes (and probably closes the window)

berak gravatar imageberak ( 2017-06-15 23:30:23 -0500 )edit

This is a really simplified version of my actual code. It just displays the image in the JFrame, and can run through the movie grabbing each frame in the while loop. Running this without the VideoCapture results in the JFrame not closing, as I never set that property. Running it with the VideoCapture results in the JFrame closing as soon as the VideoCapture reads a blank frame, as opposed to when the program ends. In my actual program, there's more code after the while loop that attempts to use the given JFrame. However, it disappears as soon as the VideoCapture reads a blank frame, and I can't make it display afterwards.

Hope that helps explain.

gde1 gravatar imagegde1 ( 2017-06-16 09:21:34 -0500 )edit