Monday, May 19, 2014

Onboard Camera - V4L wrapper for use with OpenCV

Many people have been asking how to use the onbard camera (CSI and MIPI that comes with the Freesacale's development board) with OpenCV.

Unfortunately the mxc driver for this camera is not compatible with OpenCV and there is no way to use it directly, instead, we can use wrapper functions to access it. In this post I will share with you guys some utility functions I created (based on an Ipu demo app from Rogerio Pimentel - Freescaler) that uses V4L to access the camera.

When using these functions we will get frames on UYVY format (YUV422) and to use with OpenCV it must be converted to RGB24 (24bpp), all these are included in the source code.

The code can be downloaded from here, it also comes with a sample application, you just need to export the ROOTFS envioroment variable and check if the toolchain you are going to use is the same as defined in the Makefile, otherwise you must change it too.

The demo application (tested on i.MX6 sabresd board) performs the Canny edge detector and displays it using cvShowImage, you could display it directly in the framebuffer using the utility function, just uncomment the functions in the code and also add the conversion from RGB888toYUV422.

to run this application, make sure to have these drivers installed:

modprobe ov5642_camera
modprobe ov5640_camera_mipi
modprobe mxc_v4l2_capture

here is the result:




ps:  There is a "small" issue in these wrapper functions, as it uses multiple buffers for capturing and output (if you use it instead cvShowImage), you need to at least use the  V4LWrapper_QueryFrame function 3 times in a row to fill all buffers and the display it, so, the best scenario would be using this function like cvQueryFrame, when I got a fix for this I will update this post. Meanwhile just throw this function inside a loop function and you are good to go =)

EOF !



17 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Looks like your modprobe lines have an extra "ko" extension that's not needed.
    Also I suppose the article is about the i.MX6 correct? I have such a setup but have some trouble including "linux/ipu.h and "linux/mxcfb.h" when building but I guess I might just be missing a package. Anyways thanks for your numerous posts, they are very helpful.

    ReplyDelete
    Replies
    1. Hi Andre,

      YEs, it is for i.mx6 or any other imx with IPU and camera support, I am using the default BSP configuration (minimal profile), and didn't need to install any other package to get the linux/mxcfb.h. Which bsp release are you using ?

      regards,
      Andre

      Delete
    2. Hi Andre

      I am in trouble including "linux/ipu.h" when building. I build yocto image
      using "bitbake core-image-x11" with opencv.

      Please help.

      Delete
  3. Hi Andre,
    thanks for your confirmation. I was able to spend some more time on it and finally got the code to compile -- by switching to Kernel 3.0.35-r0 (originally I was using a 3.10.17-r0 based rootfs/SDK that I built using FSL's current Yocto SDK). It seems like the inclusion of Kernel headers into a userspace application (the v4l wrapper) causes all kinds of build issues under 3.10 that I wasn't able to silence easily ;) But even 3.0 gives out a warning pointing into a similar direction (although it compiles).... include/linux/types.h:13:2: warning: #warning "Attempt to use kernel headers from user space, see http://kernelnewbies.org/KernelHeaders" [-Wcpp]
    #warning "Attempt to use kernel headers from user space, see http://kernelnewbies.org/KernelHeaders" Anyways I'll try some more to get it to build under 3.10.

    Andre

    ReplyDelete
  4. Ok I "hacked" it and got it to work under 3.10.17. Basically I removed the inclusion of the kernel headers linux/ipu.h and linux/mxc_v4l2.h from your v4l_wrapper.h, and pointed to the Kernel source tree version of mxcfb.h. I know we shouldn't be pointing into the kernel source tree but for whatever reason when building the FSL SDK those header files do not get populated into the /usr/include/linux folder - neither in the SDK nor in the target rootfs (they only get populated into /usr/src/kernel/include). I'm not smart enough to know if this is something I'm doing wrong (or incorrectly expecting) or if this is an opportunity for improvement in the FSL SDK. If it's the later I would appreciate someone picking up the ball and filing an enhancement request.

    Anyways the code now works like a charm, thanks again.

    ReplyDelete
    Replies
    1. Hi Andre,

      I am glad you made it, I also don´t like to mix kernel space header on user space application, but it is the only way I got it working, I will try to change that in future.

      regards,
      Andre

      Delete
    2. Hi Andre,
      Now, we have the same errors with you, that error including kernel headers linux/ipu.h and linux/mxc_v4l2.h from v4l_wrapper.h. we notice that you have made it by removing them and pointing to the Kernel source tree version of mxcfb.h. We are confused how to point to the Kernel source tree version of mxcfb.h? Could you tell us some tips? Many thanks!

      Regards,
      Yao

      Delete
    3. Hi Yao,

      the mxcfb.h is included when the IPU package is included as well, I didn´t try on yocto yet but I believe if you include the IPU libraries it will be added automatically as well. Can you give it a try ?

      regards,
      Andre

      Delete
    4. Also, there is this another approach using gstreamer: http://stackoverflow.com/questions/23570572/using-custom-camera-in-opencv-via-gstreamer I believe it works too, in case if you want to test.

      regards,
      Andre

      Delete
    5. hi

      how to implement custom camera via gstreamer using its pipeline?
      please explani in detail.

      Delete
    6. Hi Andre

      one more question:
      I have checked that IPU package is not included on yocto.
      then what is workaround?

      Delete
    7. Hi Kumar,

      here it goes: http://imxcv.blogspot.com/2014/08/onboard-camera-v4l-wrapper-with-yocto.html

      regards,
      Andre

      Delete
  5. Hi Andre,

    Is there any way to start OV5640 in resolution higher than 640x480? As I know, this camera supports resolutions up to 1080p@30, but I've failed to run it. When i pass higher values to V4LWrapper_CreateCameraCapture, I still get small image in an upper left corner and all the free area simply filled with one color :(

    Any ideas?

    regards,
    Boris

    ReplyDelete
    Replies
    1. Hi Boris,

      I will check it and let you know soon as possible.,

      regards,
      Andre

      Delete
  6. Hello Andre,

    I have Sabrelite BD-SL-i.MX6 board from boundary devices.

    I have ov5640 camera (Image Sensor).

    Now I want to know the luminance value(s) in the middle of the video line. So i can get luminance min and max values for that line

    What is the way to go ahead on this.? How should i get luminance value.

    Please suggest me a way to this.

    ReplyDelete
    Replies
    1. Hi Bajaj,

      sorry for answering you just now, but I wasn't being able to update the blog. I hope you already got your answer, but if you don't you can try the following approach: http://stackoverflow.com/questions/4876315/determining-image-luminance-brightness

      cheers,
      Andre

      Delete