How to Install and Compile OpenCV3 on RPI and test using motion tracking
These are the detailed steps I used to successfully compile opencv3 on my RPI 2. After all this my testing indicated no speed improvement as far as I could see, but your mileage may vary depending on what opencv code you use. In my case no improvement noticed. Python3 is part of the default RPI build so just did normal update prior to compiling opencv3
step 1 - update raspbian
Code: Select all
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
Note that dist-upgrade may not be required but I did it anyway since I have enough room on my SD card
step2 - Install/Update required build tools and dependencies then compile opencv3
Followed instructions per link below, but changed the first command to delete the gcc-c++ entry
http://www.linuxcircle.com/2015/05/18/o ... erry-pi-2/
These are the commands I used to compile opencv3
Code: Select all
cd ~
sudo apt-get install cmake python3-dev python3.2-dev python3-numpy gcc build-essential cmake-curses-gui
sudo apt-get -y install pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff5-dev libtiff5 libtiffxx0c2 libtiff-tools libeigen3-dev
sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev swig libv4l-0 libv4l-dev libpython3.2 libgtk2.0-dev
git clone --branch 3.0.0-rc1 --depth 1 https://github.com/Itseez/opencv.git
cd opencv
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") -D PYTHON_EXECUTABLE=$(which python3) ..
make -j4
sudo make install
step 3 - Verify installation
Here is the test to verify I have both opencv2 and opencv3 under python3
Code: Select all
]pi@dawn-robot ~/motion-track $ python
>>> from cv2 import __version__
>>> __version__
'2.4.1'
>>> quit()
pi@dawn-robot ~/motion-track $ python3
Python 3.2.3 (default, Mar 1 2013, 11:53:50)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from cv2 import __version__
>>> __version__
'3.0.0-rc1'
step 4 - Test code
Conclusion Does not seem to improve speed over python2 per my testing below
I did get the same findCountours error as mentioned above so I created a separate motion3-track.py and changed the findContour command per below. That was the only change
from
Code: Select all
contours,hierarchy = cv2.findContours(thresholdimage,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
to
Code: Select all
thresholdimage,contours,hierarchy = cv2.findContours(thresholdimage,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
will post the slightly revised code on my github repo if you are interested
Tested motion-track.py using python2 and opencv2. Speed was approx 1.25 fps
Tested motion3-track.py using python3 and opencv3. Speed was about the same (no improvement as far as I can see)
Code: Select all
pi@dawn-robot ~/motion-track $ ./motion-track.py
Loading Please Wait ....
Initializing Camera ....
press ctrl-c to quit
Start Motion Tracking ....
total_Contours= 2 Motion at cx= 47 cy= 83 biggest_area: 35x 28= 639
Processing at 1.33 FPS last 10 frames
total_Contours= 4 Motion at cx= 97 cy= 40 biggest_area:192x238=30066
total_Contours= 7 Motion at cx= 49 cy=221 biggest_area: 97x 21= 1279
total_Contours= 2 Motion at cx=130 cy= 40 biggest_area:258x238=35887
total_Contours=12 Motion at cx= 65 cy= 19 biggest_area:128x110= 8229
total_Contours= 4 Motion at cx= 70 cy= 34 biggest_area:139x200=17323
total_Contours= 1 Motion at cx=109 cy= 35 biggest_area:217x209=24427
total_Contours= 2 Motion at cx=109 cy= 40 biggest_area:216x238=34207
total_Contours= 5 Motion at cx= 92 cy=147 biggest_area:182x110=12177
Processing at 1.25 FPS last 10 frames
total_Contours= 2 Motion at cx= 43 cy= 92 biggest_area: 14x 14= 129
total_Contours= 1 Motion at cx= 50 cy= 87 biggest_area: 26x 20= 223
total_Contours= 1 Motion at cx= 48 cy= 93 biggest_area: 23x 15= 190
total_Contours= 1 Motion at cx= 51 cy= 93 biggest_area: 28x 14= 223
total_Contours= 1 Motion at cx= 52 cy= 90 biggest_area: 29x 19= 341
total_Contours= 1 Motion at cx= 53 cy= 88 biggest_area: 27x 22= 412
total_Contours= 1 Motion at cx= 49 cy= 88 biggest_area: 29x 21= 364
total_Contours= 2 Motion at cx= 43 cy= 87 biggest_area: 12x 13= 83
pi@dawn-robot ~/motion-track $ ./motion3-track.py
Loading Please Wait ....
motion3-track.py Initializing Camera ....
press ctrl-c to quit
Start Motion Tracking ....
Processing at 1.34 FPS last 10 frames
total_Contours= 7 Motion at cx= 72 cy= 34 biggest_area:142x200=14533
total_Contours= 8 Motion at cx=132 cy= 35 biggest_area:262x204=27061
total_Contours=13 Motion at cx=132 cy= 60 biggest_area:262x175=18085
total_Contours= 9 Motion at cx=129 cy= 68 biggest_area:256x165=16880
total_Contours= 9 Motion at cx=129 cy= 35 biggest_area:257x206=22325
total_Contours= 2 Motion at cx=107 cy= 34 biggest_area:213x202=19147
total_Contours= 3 Motion at cx=107 cy= 34 biggest_area:213x202=22261
total_Contours= 6 Motion at cx=133 cy= 85 biggest_area:264x163=25308
total_Contours= 6 Motion at cx=133 cy= 85 biggest_area:264x163=25283
Processing at 1.25 FPS last 10 frames
total_Contours= 9 Motion at cx=133 cy=111 biggest_area:265x146=20328
total_Contours= 9 Motion at cx=133 cy=111 biggest_area:265x146=20243
total_Contours= 7 Motion at cx=133 cy=110 biggest_area:264x148=19651
total_Contours=15 Motion at cx=133 cy=110 biggest_area:264x148=19159
total_Contours= 1 Motion at cx= 59 cy= 90 biggest_area: 9x 12= 64