How to write an opencv image (IplImage) to a V4L2 loopback device? - c++

As the title says I am not sure how to write an IplImage to a V4l2 loopback device. I know how to write to a device as I have posted here How to write/pipe to a virtual webcam created by V4L2loopback module?
But now I am not sure how I can exactly write IplImage objects in to the device. If I just write the image->imageData where image is an IplImage*, when I view the device using "luvcview" the malformed frames show up for about a second then it throws the following error.
luvcview 0.2.6
SDL information:
Video driver: x11
A window manager is available
Device information:
Device path: /dev/video3
Stream settings:
Frame format: YUYV (MJPG is not supported by device)
Frame size: 520x474 (requested size 640x480 is not supported by device)
Frame rate: 30 fps
libv4l2: error dequeuing buf: Invalid argument
Unable to dequeue buffer: Invalid argument
Error grabbing
Cleanup done. Exiting ...
Could it be because I have not converted the opencv images to v4l2 format? or the v4l2 arguments does not match with the IplImage properties? If so how to do it?
If anyone knows what this error means please let me know.
I decided to post this question separately as this question is not about writing to the device its particularly about write an IplImage to the device.
Could anyone please give me a code snippet that shows how to write an IplImage to a V4l2 loopback device?

I worked out that the above issue was not an issue anymore as it seems that luvcview or skype does not fully support v4l2. But if I view the loopback device using VLC player, it works fine.

Related

OpenCV VideoCapture using H.264 Camera

I've got an embedded linux device with a USB Camera attached which provides an MJPEG as well as an H.264 stream. Until now, I used the MJPEG stream (device index 0) to initialize my VideoCapture object and to read frames from the camera. The problem is that the CPU of the device isn't exactly powerful, so decoding utilizes a lot of it, which is why I want to use the H.264 stream (device index 2) that the camera already provides. However, OpenCV refuses to accept this format at all.
Aug 23 08:08:17: VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
Aug 23 08:08:17: VIDEOIO ERROR: V4L: can't open camera by index 2
After several online searches I found that you can hint OpenCV to accept H.264 streams by setting the FourCC value of the object cap.set(CAP_PROP_FOURCC, VideoWriter::fourcc('H', '2', '6', '4'));, but this didn't do the trick.
I also tried "avc1", which didn't work as well.
Is there any way how I can use the native H.264 stream of the camera using OpenCV or am I stuck with MJPEG?

Resolution Issue when create .mp4 video file from camera streaming using SourceReader

I'm developing the Window Desktop Camera application using SourceReader technique.I have completed Video streaming and still capture.
Now, I'm working on to capturing an .mp4 video file from USB camera. I'm able to capture video file for the following resolutions: 640 x 480,1280 x 720 and 1920 X 1080.
I have encountered an issue when changing the video resolution higher than 1920 x 1080 and call SetInputMediaType for the IMFSinkWriter object returns an HRESULT error code 0xc00d36b.
I used video subtype for encoding : MFVideoFormat_H264
Is there any other subtype available for encoding .mp4 file other than MFVideoFormat_H264?
Why cant i capturing an .mp4 file higher than FULL HD resolution? Am i missing anything to encode the video file? If yes, please provide me some guidelines to solve this issue.
Thanks in advance.
The likely bottleneck is the maximal resolution supported by video encoder. You are presumably using the encoder implicitly as a part of Sink Writer. Sink Writer on its own does not limit the resolution, but if encoder is unable to handle specific media type then encoding is impossible. Specifically, in Windows 7 resolution is (or might be) limited to 1920x1088.
Also, you lost one digit from the error code.
See also:
IMFSinkWriter can't export a large size video of mp4
The max resolution for mp4(h264) encoder
Roman gave a great answer, but just to add on the topic of other codecs - if you check MPEG-4 File Sink on MSDN you see that it mentions MJPG support as well (even though not clear if it's only available from Windows 8 or it simply has been improved in Windows 8), so you should be able to use MFVideoFormat_MJPG as well. I assume that should not have any size limitations, but of course the size of the resulting .mp4 file will grow drasticly.

Why my App cannot decode the RTSP stream?

I use live555 to receive RTP video frame (frame encoded in H264). I use Live555 open my local .sdp file to receive frame data. I just saw DummySink::afterGettingFrame was called ceaselessly。 if fReceiveBuffer in DummySink is correct, Why FFMPEG cannot decode the frame? My code is wrong?
Here is my Code Snippet:
http://paste.ubuntu.com/12529740/
the function avcodec_decode_video2 is always return failed , its value less than zero
fReceiveBuffer is present one video frame?
Oh, Here is my FFMPEG init code need to open related video decoder:
http://paste.ubuntu.com/12529760/
I read the document related H264 again, I found out that I-frame(IDR) need SPS/PPS separated by 0x00000001 insert into the header and decoder have a capacity to decode the frame correctly. Here is a related solution
FFmpeg can't decode H264 stream/frame data
Decoding h264 frames from RTP stream
and now, My App works fine, it can decode the frame and convert it to OSD Image for displaying to screen .

Capturing video from SMI Grabber Device using Opencv C++

I am trying to capture a video from an SMI grabber device using opencv c++. The line VideoCapture cap(0) obtains video from web cam. How do I get the feed from the device that I have connected.
From openCV docs
VideoCapture
public VideoCapture(int device)
Parameters:
device - id of the opened video capturing device (i.e. a camera index). If there is a single camera connected, just pass 0.
As per this answer, it's not possible at the moment to enumerate devices via OpenCV however you could try passing 1 and see if the SMI device is found there.

Capturing H264 with logitech C920 to OpenCV

I’ve been trying to capture a H264 stream from my two C920 Logitech camera with OpenCV (On a Raspberry Pi 2). I have come to the conclusion that this is not possible because it is not yet implemented. I’ve looked a little in OpenCV/modules/highgui/cap_libv4l.cpp and found that the “Videocapture-function” always convert the pixelformat to BGR24. I tried to change this to h264, but only got a black screen. I guess this is because it is not being decoded the right way.
So I made a workaround using:
V4l2loopback
h264_v4l2_rtspserver
Gstreamer-0.10
(You can find the loopback and rtspserver on github)
First I setup a virtual device using v4l2loopback. Then the rtspserver captures in h264 then streams rtsp to my localhost(127.0.0.1). Then I catch it again with gstreamer and pipe it to my virtual v4l2 video device made by loopback using the “v4l2sink” option in gst-launch-0.10.
This solution works and I can actually connect to the virtual device with the opencv videocapture and get a full HD picture without overloading the cpu, but this is nowhere near a good enough solution. I get a roughly 3 second delay which is too high for my stereo vision application and it uses a ton of bandwidth.
So I was wondering if anybody knew a way that I could use the v4l2 capture program from Derek Molloys boneCV/capture program (which i know works) to capture in h264 then maybe pipe it to gst-launche-0.10 and then again pipe it to the v4l2sink for my virtual device?
(You can find the capture program here: https://github.com/derekmolloy/boneCV)
The gstreamer command I use is:
“gst-launch-0.10 rtspsrc location=rtsp://admin:pi#127.0.0.1:8554/unicast ! decodebin ! v4l2sink device=/dev/video4”
OR maybe in fact you know what I would change in the opencv highgui code to be able to capture h264 directly from my device without having to use the virtual device? That would be amazingly awesome!
Here is the links to loopback and the rtspserver that I use:
github.com/mpromonet/h264_v4l2_rtspserver
github.com/umlaeute/v4l2loopback
Sorry about the wierd links I don't have enough reputation yet to poste more links..
I don't know exactly where you need to change in the OpenCV, but very recently I started to code using video on Raspberry PI.
I'll share my findings with you.
I got this so far:
can read the C920 h264 stream directly from the camera using V4L2 API at 30 FPS (if you try to read YUYV buffers the driver has a limit of 10 fps, 5 fps or 2 fps from USB...)
can decode the stream to YUV 4:2:0 buffers using the broadcom chip from raspberry using OpenMax IL API
My Work In Progress code is at: GitHub.
Sorry about the code organization. But I think the abstraction I made is more readable than the plain V4L2 or OpenMAX code.
Some code examples:
Reading camera h264 using V4L2 Wrapper:
device.streamON();
v4l2_buffer bufferQueue;
while (!exit_requested){
//capture code
device.dequeueBuffer(&bufferQueue);
// use the h264 buffer inside bufferPtr[bufferQueue.index]
...
device.queueBuffer(bufferQueue.index, &bufferQueue);
}
device.streamOFF();
Decoding h264 using OpenMax IL:
BroadcomVideoDecode decoder;
while (!exit_requested) {
//capture code start
...
//decoding code
decoder.writeH264Buffer(bufferPtr[bufferQueue.index],bufferQueue.bytesused);
//capture code end
...
}
check out Derek Molloy on youtube. He's using a Beaglebone, but presumably ticks this box
https://www.youtube.com/watch?v=8QouvYMfmQo