OpenCV_3.4.3 hangs on VideoCapture. Resolved

asked 2018-11-17 07:32:04 -0600

russc243 gravatar image

updated 2018-11-18 02:55:22 -0600

Built OpenCV-3.4.3 with java bindings. Running on raspberry Pi 3B+ with Stretch and v4l2. OpenCV works fine with a USB camera but not with the onboard MIPI camera. I can capture images and video with raspistill and raspivid using the onboard MIPI camera.

When I try to open the mipi camera with OpenCV using VideoCapture camera = new VideoCapture(0); it hangs on this line. If I replace the 0 with a 1 for the USB camera, it works fine. I suspect that the default pixel format of YUV12 is not compatible so I try to change it to other formats with v4l2-ctl but creating the VideoCapture objects seems to reset the pixel format to YUV12. This is verified with v4l2-ctl -V

Setting the pixel format can't be done in the program with camera.set(CAP_PROP_... because the VideoCapture object needs to exist to do that. Setting before running the program with v4l2-ctl does not work either.

I would try an older version of Rasbien but apparently the Pi ver 3B+ is not compatible with older versions. I posted a similar question to the raspberry pi forum but not much help with this specific issue.

Any suggestions debug or resolve is greatly appreciated. Thanks, Russ

edit retag flag offensive close merge delete

Comments

any chance, you've built it with gstreamer support ? (could be used alternatively to v4l)

I suspect that the default pixel format of YUV12 is not compatible

python users reported similar problems before.

berak gravatar imageberak ( 2018-11-17 07:53:31 -0600 )edit

berak, When I set enviornment varible OPENCV_VIDEOIO_DEBUG=1 and run I see this when it tried to create VideoCapture(0) object. [ WARN:0] VIDEOIO(createGStreamerCapture(index)): trying ... It just hangs there....

Before the OpenCV build I executed sudo apt-get install libgstreamer-plugins-base1.0-dev I thought that was a needed dependency of OpenCV. These were my build options for OpenCV: cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_LIBV4L=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.4.3/modules ..

I tried to create the VideoCapture object then set the properties then open. When I read back the settings before the open they are all zero.

I will try to build OpenCV without 1st installing libgstreamer-plugins. Thanks, Russ

russc243 gravatar imagerussc243 ( 2018-11-17 08:54:01 -0600 )edit
1

With some help from this forum and the Raspberry Pi forum, my issue is resolved. I will describe my understanding of the issue. The correctness of my description may be a bit lacking as I am new to Linux , Java and OpenCV.

My problem was that I installed gstreamer because I incorrectly thought it was a needed dependency when building OpenCV. (During CMAKE I saw warnings like “failed to find gstreamer”. So I installed gstreamer and the warnings went away so I thought I was one step closer to getting a usable build.)

Two of the imports I have in my Netbeans project are import org.opencv.videoio.VideoCapture; import org.opencv.videoio.Videoio;

When you import create VideoCapture object, Videoio 1st tries to use GStreamer regardless of if it is installed or not. If GStreamer is not instal

russc243 gravatar imagerussc243 ( 2018-11-18 03:01:47 -0600 )edit
1

If GStreamer is not installed, Videoio tries v4l2. The later is what you want because gstreamer is known to be buggy and not work well with the onboard CSI MIPI Pi camera.

After uninstalling a handful of gstreamer components, NOT loading V4l2 with sudo modprobe bcm2835-v4l2, setting environment variable OPENCV_VIDEOIO_DEBUG=1, then a reboot, I get the following in the SSH putty session: [ WARN:0] VIDEOIO(createGStreamerCapture(index)): result=(nil) isOpened=-1 ... [ WARN:0] VIDEOIO(cvCreateCameraCapture_V4L(index)): trying ... [ WARN:0] VIDEOIO(cvCreateCameraCapture_V4L(index)): result=(nil) ... When gstreamer was installed, the program would hang on the createGStreamerCapture step. With gstreamer still uninstalled and loading v412, I get the following: [ WARN:0] VIDEOIO(createGStreamerCa

russc243 gravatar imagerussc243 ( 2018-11-18 03:03:33 -0600 )edit
1

With gstreamer still uninstalled and loading v412, I get the following: [ WARN:0] VIDEOIO(createGStreamerCapture(index)): result=(nil) isOpened=-1 ... [ WARN:0] VIDEOIO(cvCreateCameraCapture_V4L(index)): trying ... [ WARN:0] VIDEOIO(cvCreateCameraCapture_V4L(index)): result=0x6523c7a8 ...

Then camera opens, OpenCV runs fine, I can see live video OpenCV canny images from onboard Pi camera in an xming window on my laptop. At 640x480 I see about 15fps with a few OpenCV test functions applied to each frame(grayscale, blur, canny). This is with Netbeans 9, OpenCV 3.4.3 and Raspbian Stretch. Thanks for the help. Russ

russc243 gravatar imagerussc243 ( 2018-11-18 03:05:09 -0600 )edit