Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

VideoCapture, impossible to switch to correct

I have a movie encoded as mj2. The individual frames are 16 bits unsigned grayscale. Using the Java API through Scala, I devised the following code to test the video capture possibilities of OpenCV:

import org.opencv.core.{Core, CvType, Mat}
import org.opencv.imgcodecs.Imgcodecs
import org.opencv.videoio.{Videoio, VideoCapture}

object OpenVideo {
    def main {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME)

        var myVid = new VideoCapture("data/DEVISSAGE_181.mj2")
        println("default value: myVid.get(8): " + myVid.get(8))

        val setOK8 = myVid.set(8, CvType.CV_16UC1) 
        val setOK16 = myVid.set(16, 0)

        println("setOK8: " + setOK8 + ", setOK16: " + setOK16)
        println("myVid.get(): " + myVid.get(8) + ", expected value: " + CvType.CV_16UC1)

        var m = new Mat(288, 1152, CvType.CV_16UC1)
        println("before read, m.total(): " + m.total() + ", m.channels(): " + m.channels() + ", m.depth(): " + m.depth())
        println("Position: " + myVid.get(Videoio.CAP_PROP_POS_MSEC))

        myVid.read(m)

        println("m.rows: " + m.rows + ", m.cols: " + m.cols)
        println("after read, m.total(): " + m.total() + ", m.channels(): " + m.channels() + ", m.depth(): " + m.depth())
        println("fps: " + myVid.get(5))
        println("Position (ms): " + myVid.get(Videoio.CAP_PROP_POS_MSEC))

        Imgcodecs.imwrite("data/out.jpg", m)
    }
}

The output is:

default value: myVid.get(8): 0.0
setOK8: false, setOK16: false
myVid.get(): 0.0, expected value: 2
before read, m.total(): 331776, m.channels(): 1, m.depth(): 2
Position: 0.0
m.rows: 288, m.cols: 1152
after read, m.total(): 331776, m.channels(): 3, m.depth(): 0
fps: 5.5
Position (ms): 181.8181818181818

You can see from setOK8: false, setOK16: false that it is not possible to set the output MAT format or to force RGB to false. Hence the resulting grabbed image is 8 bit 3 channels. Why is that ? Why can't I capture the individual frames at their original depth and number of channels ?

I know for sure that the images are 16 bits unsigned grayscale, I opened the mj2 file in Matlab. But I am looking to use OpenCV as an alternative.

My platform is MacOS Sierra.

Note that I already asked a similar question here: http://stackoverflow.com/questions/39795849/can-not-set-the-number-of-channels-in-videocapture-in-opencv, using the CPP Api, but I did not get any definitive answer.