magnatag wrote:Hi Kevin,
Any chance you can share your modified opencv code/constructor and your procedures? I am building an opencv project using RPI cam and I am definitely running into the super low framerate issue.
Thanks!
Greetings,
I have no problems sharing the code snippets. My only condition is that you agree that this is not a particularly acceptable solution; it's just a means of bypassing the problem for the moment. For this build I used the sourceforge repository to get the OpenCV 2.4.9 source, created a release directory off of the main folder, then ran 'cmake ..' from there. I then made the modifications, returned to my release directory and then ran 'make;sudo make install'
modules/highgui/src/cap_v4l.c: added two lines after 246
Code: Select all
245: #define DEFAULT_V4L_WIDTH 1920
246: #define DEFAULT_V4L_HEIGHT 1080
<added>: static int default_resolution_width = DEFAULT_V4L_WIDTH;
<added>: static int default_resolution_height = DEFAULT_V4L_HEIGHT;
modules/highgui/src/cap_v4l.c: lines 425-426 modified
Code: Select all
425 original: capture->form.fmt.pix.width = DEFAULT_V4L_WIDTH;
426 original: capture->form.fmt.pix.height = DEFAULT_V4L_HEIGHT;
425 new: capture->form.fmt.pix.width = default_resolution_width;
426 new: capture->form.fmt.pix.height = default_resolution_height;
modules/highgui/src/cap_v4l.c: line 874 modified
Code: Select all
874 original: icvSetVideoSize(capture, DEFAULT_V4L_WIDTH, DEFAULT_V4L_HEIGHT);
874 new: icvSetVideoSize(capture, default_resolution_width, default_resolution_height);
modules/highgui/src/cap_v4l.c added to end of file, just above #endif
Code: Select all
int cvCameraDefaultResolution_V4L( int w, int h )
{
default_resolution_width = w;
default_resolution_height = h;
return 0;
}
modules/highgui/src/precomp.hpp added the following line
Code: Select all
int cvCameraDefaultResolution_V4L( int w, int h );
modules/highgui/src/cap.cpp added
Code: Select all
VideoCapture::VideoCapture(int device, int w, int h)
{
cvCameraDefaultResolution_V4L( w, h );
open(device);
}
modules/highgui/include/opencv2/highgui.hpp: Added after line 209
Code: Select all
CV_WRAP VideoCapture(int device, int w, int h);
Additionally, I used the following modifications to view the error returned from the ioctl on setting the resolution.
modules/highgui/src/cap_v4l.cpp
Code: Select all
2505: int rtn = ioctl (capture->deviceHandle, VIDIOC_S_FMT, &capture->form);
<added on next line> fprintf(stderr, "Setting video size to (%dx%d). Return: %d, Errno: %d\n", w, h, rtn, errno);
The FPS cap exists on both the v4l2 side and the OpenCV side. To lift the OpenCV cap, change it on this line. The default is 30.
modules/highgui/src/cap_v4l.cpp
Code: Select all
2512: setfps.parm.capture.timeperframe.denominator = 30;
With respect to my local vision code, I use the following v4l2 settings to get the advertised frame rates.
Code: Select all
system("sudo modprobe bcm2835-v4l2");
char command[255] = {0};
sprintf(command, "v4l2-ctl --set-fmt-video=width=%d,height=%d,pixelformat=%d\n", width, height, mode);
system(command);
system("v4l2-ctl --set-ctrl horizontal_flip=1,vertical_flip=1");
system("v4l2-ctl --set-ctrl white_balance_auto_preset=0");
system("v4l2-ctl -p 40");
Note that OpenCV will not use width or height whatsoever from the v4l2 controls, but the rest of it works as advertised within that environment.
Sorry to the moderators about the length of these messages. Just hope that it's of some help; again, with the understanding that this is a hack to workaround an OpenCV issue, not a fix. It's just what I modified to get us running in OpenCV with v4l2. The current issue with resolution is that it's locked to 720P. Exceeding 1280x720 will cause the capture to fail within OpenCV. I still don't have anything to go off of on this issue.
Kevin