element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Pi IoT
  • Challenges & Projects
  • Design Challenges
  • Pi IoT
  • More
  • Cancel
Pi IoT
Blog [Pi IoT] Plant Health Camera #5 - OpenCV
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: gpolder
  • Date Created: 25 Jul 2016 7:59 AM Date Created
  • Views 1610 views
  • Likes 4 likes
  • Comments 5 comments
  • image processing
  • opencv
  • machine_vision
  • git
  • github_repository
Related
Recommended

[Pi IoT] Plant Health Camera #5 - OpenCV

gpolder
gpolder
25 Jul 2016

As described in my first post, OpenCV is an important requirement for my application, In this post I will describe how I installed OpenCV on the Pi 3.

 

Previous posts:

[Pi IoT] Plant Health Camera #4 - Putting the parts together

[Pi IoT] Plant Health Camera #3 - First steps

[Pi IoT] Plant Health Camera #2 - Unboxing

[Pi IoT] Plant Health Camera #1 - Application

 

Install OpenCV

Next thing we need is OpenCV. I installed it from source, as described in this Install guide: Raspberry Pi 3 + Raspbian Jessie + OpenCV 3 - PyImageSearch tutorial. This is a very well written tutorial, it doesn't make much sense to repeat all steps here, so below I only describe the steps in which I deviate from this tutorial.

First make sure that I indeed run the latest Raspbian version:

 

pi@pi3iot:~/planthealthcam $ cat /etc/os-release 
PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
NAME="Raspbian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

 

Then I followed the steps of the above mentioned Install guide until 'Step #3: Download the OpenCV source code'.

I decided to do a git clone instead of downloading the zip file. This way it is much easier to update to the latest version.

pi@pi3iot:~/opencv $ git clone https://github.com/opencv/opencv.git
Cloning into 'opencv'...
remote: Counting objects: 191347, done.
remote: Compressing objects: 100% (49/49), done.
remote: Total 191347 (delta 22), reused 0 (delta 0), pack-reused 191298
Receiving objects: 100% (191347/191347), 414.50 MiB | 904.00 KiB/s, done.
Resolving deltas: 100% (132424/132424), done.
Checking connectivity... done.
Checking out files: 100% (4791/4791), done.
pi@pi3iot:~ $ git clone https://github.com/opencv/opencv_contrib.git
Cloning into 'opencv_contrib'...
remote: Counting objects: 15903, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 15903 (delta 0), reused 0 (delta 0), pack-reused 15897
Receiving objects: 100% (15903/15903), 114.08 MiB | 910.00 KiB/s, done.
Resolving deltas: 100% (9087/9087), done.
Checking connectivity... done.
pi@pi3iot:~ $

 

Tutorial Step #4: Python 2.7 or Python 3? instructs to install pip. In my case pip seems to be already installed:

pi@pi3iot:~ $ pip --version
pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7)

I also installed the virtualenvwrapper, as recommended by the tutorial.

I decided to use python3 for my project:

pi@pi3iot:~/planthealthcam $ mkvirtualenv cv -p python3
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/pi/.virtualenvs/cv/bin/python3
Also creating executable in /home/pi/.virtualenvs/cv/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/pi/.virtualenvs/cv/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/pi/.virtualenvs/cv/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/pi/.virtualenvs/cv/bin/preactivate
virtualenvwrapper.user_scripts creating /home/pi/.virtualenvs/cv/bin/postactivate
virtualenvwrapper.user_scripts creating /home/pi/.virtualenvs/cv/bin/get_env_details
(cv) pi@pi3iot:~/planthealthcam $

Note the (cv) preceding my prompt, indicating that I am in the cv  virtual environment.

 

Since I cloned the distribution using git, I used the following make build:

cd ~/opencv/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON ..

 

Unfortunately the make -j4 build process stoppen after 44% (27 min) with an error compiling ffmpeg:

 

Building CXX object modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o
[ 44%] Built target opencv_dnn
In file included from /home/pi/opencv/modules/videoio/src/cap_ffmpeg.cpp:47:0:
/home/pi/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp: In member function ‘double CvCapture_FFMPEG::get_fps() const’:
/home/pi/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1138:49: error: ‘AVStream’ has no member named ‘r_frame_rate’
     double fps = r2d(ic->streams[video_stream]->r_frame_rate);
                                                 ^
modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:169: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o' failed
make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o] Error 1
CMakeFiles/Makefile2:6100: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
Makefile:147: recipe for target 'all' failed
make: *** [all] Error

 

Quickest solution was to exclude ffmpeg from the build:

 

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON \
    -D WITH_FFMPEG=OFF ..

 

Now the make -j4 runs just fine, and after 95 minutes and 37 the make process finished at 100%.

Last step is to install it with:

$ sudo make install
$ sudo ldconfig

 

Test OpenCV

After some small additional steps described in the tutorial I have a working OpenCV environment.

Lets give it a try:

 

pi@pi3iot:~/planthealthcam $ workon cv
(cv) pi@pi3iot:~/planthealthcam $ python
Python 3.4.2 (default, Oct 19 2014, 13:31:11) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.1.0-dev'
>>>

 

Now let's try a small demo program, in this python script a camera image is grabbed, and a canny edge detector is applied, showing the edges in the image. Both the input image and the edges are displayed.

# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
rawCapture = PiRGBArray(camera)

# allow the camera to warmup
time.sleep(0.1)

# grab an image from the camera
camera.capture(rawCapture, format="bgr")
image = rawCapture.array
edges = cv2.Canny(image,100,200)

# display the image on screen and wait for a keypress
cv2.imshow("Image", image)
cv2.imshow("Edges", edges)
cv2.waitKey(0)

 

This gives me the following result:

imageimage

 

Success!

An important software requirement for my plant health camera is now ready to use.

 

stay tuned for the next steps.

  • Sign in to reply

Top Comments

  • gpolder
    gpolder over 9 years ago in reply to pettitda +2
    David, no this will have no effect on the Pi B+, luckily the Pi 3 has 4 cores.
  • gpolder
    gpolder over 9 years ago in reply to clem57 +1
    its a video codec, if you need to work with video formats like .avi, .mp4, .mpeg, etc. I don't need it for my application.
  • gpolder
    gpolder over 9 years ago in reply to pettitda

    David,

     

    no this will have no effect on the Pi B+, luckily the Pi 3 has 4 cores.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • pettitda
    pettitda over 9 years ago

    Does the -j4 make option have any effect if the machine only has one core? 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • gpolder
    gpolder over 9 years ago in reply to clem57

    its a video codec, if you need to work with video formats like .avi, .mp4, .mpeg, etc.

    I don't need it for my application.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • clem57
    clem57 over 9 years ago

    You say:

    Quickest solution was to exclude ffmpeg from the build:

    Do you know what this piece does?  And why you do not need it?

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 9 years ago

    Very good post.

     

    I was curious how easy it was to put OpenCV on the Rpi, and you have shown that it can be very easy.

     

    Definitely looking forward to the next post.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube