Computer Vision in Blender (OpenCV) - TUTORIAL

Hi
I’m currently working on a project of computer vision inside Blender!
The goal is to use a webcam to recognize objects in front of it (faces, balls etc…) and to use these elements inside a BGE project.

There are no up to date tutorial to do that so I propose to use my experiments to create that.

First, I needed :
-Blender 2.70
-Python-OpenCV
I’m on Ubuntu 14.04

  1. INSTALLATION OF OPENCV :
    you must use the last version of OpenCV, since we need a Python3.4-enabled OpenCV (the older versions use Python2.X) :
sudo mkdir opencv
cd opencv
sudo git clone https://github.com/Itseez/opencv.git
cd opencv
sudo mkdir build
cd build
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_opencv_java=OFF -D PYTHON_EXECUTABLE=/usr/bin/python3.4 ..
sudo make -j8
sudo make install

And voilà! a Python3.4 Python-OpenCV

2) WEBCAM INSTALLATION
For testing, I’ve got a Logitech QuickCam Pro 400. A quite old webcam but I think it will be enough for object detection.
You can test it’s working easily with a program like guvcview for example.

More to come… :cool:

Best sources :

Installation: http://docs.opencv.org/trunk/doc/py_tutorials/py_setup/py_setup_in_fedora/py_setup_in_fedora.html#install-opencv-python-in-fedora
Tutorial to use OpenCV : http://docs.opencv.org/trunk/doc/py_tutorials/py_gui/py_table_of_contents_gui/py_table_of_contents_gui.html

The next part…

3) DETECTION OF OBJECTS THANKS TO COLORS
I need now to detect objects. Colors is used for that.
First step is to convert the picture (or frame of the video for this context) from RGB (or more precisely BGR since it’s inverted in Opencv…) into HSV (hue, saturation, value).
Then we detect the color: by detecting only color between an lower and upper limit.
Finally the mask is applied to the original image.

Here some quick example (complete source code inside the video description):