1st step: Install the Dependencies
To install the dependencies required from OpenCV, just run the following commands:
sudo apt-get -y install libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff4-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip2nd step: Download OpenCV 3.0.0 alpha
You can download manually or run the commands below to get OpenCV:
mkdir opencv cd opencv wget https://github.com/Itseez/opencv/archive/3.0.0-alpha.zip -O opencv-3.0.0-alpha.zip unzip opencv-3.0.0-alpha.zip3rd step: Install OpenCV
Now, we’ll install OpenCV. Cmake command has a lot of options: choose those that better suit your needs and run the commands below. If you’re planning to use Qt 5.x, don’t use -D WITH_QT=ON. Learn how to use OpenCV 3 with Qt Creator 3.2 (Qt 5.3). [Update] According to one of the users that tested it on Ubuntu 14.10, you’ll need to use WITH_FFMPEG=OFF. [Update]
BUILD_NEW_PYTHON_SUPPORTis no longer used.cd opencv-3.0.0-alpha mkdir build cd build cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D INSTALL_PYTHON_EXAMPLES=ON -DBUILD_EXAMPLES=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON .. make -j $(nproc) sudo make install4rd step: Finishing installation
To get OpenCV working properly, we need to tell Ubuntu:
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf' sudo ldconfigAfter all is done I recommend rebooting your system. Done!
Then, you might be wondering: “So, that’s it?” Yes!If you prefer, you can download the script below. If you do so, you’ll see that in addition to installing OpenCV, the script will generate a log for you, to know how long did it take. At the end of this post you’ll see a demonstration video of the installation and test.
Script - Install OpenCV 3.0.0 (12356 downloads)Lets test it!
We will test to check if everything is working properly. Doesn’t matter if I say you should do this way if it doesn’t work, right? Come on! I’m assuming you just restart your computer after you have performed the above steps or the given script.
1st test: Running an OpenCV sample
First of all, we need to compile the samples. Worth saying that the commands below can be avoided by adding the options to build samples on the 3rd step of installation.
cd opencv/opencv-3.0.0-alpha/samples/ sudo cmake . sudo make -j $(nproc)Now you can run a sample. I chose two, but feel free to run any other. The goal here is to prove that our OpenCV installation was a success. The samples we’re going to run are the FaceDetect and HoughtLines developed in C++.
cd cpp/ ./cpp-example-facedetect lena.jpg // (../data/lena.jpg) OpenCV 3.0 beta ./cpp-example-houghlines pic1.png // (../data/pic1.jpg) OpenCV 3.0 betaNote 1: If you’re using OpenCV 3.0 beta, both images are inside “../data/” directory;
Note 2: To close the window with Lenna, just press ENTER. The same to finish HoughLines. Go on and run other samples;2nd test: Running our own program
What about compiling something we made? Just below I am providing a simple program whose purpose is to display an image. Unzip it and run the commands below inside the folder you just created.
sudo cmake . sudo make ./DisplayImage lena.jpg
问题1:
/usr/bin/ld: warning: libpng16.so.16, needed by /home/andrei/anaconda/lib/libopencv_highgui.so, not found (try using -rpath or -rpath-link)
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_create_read_struct@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_interlace_handling@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_IHDR@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_get_io_ptr@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_longjmp_fn@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_gray_to_rgb@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_compression_level@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_bgr@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_filter@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_rgb_to_gray@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_init_io@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_destroy_read_struct@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_swap@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_get_IHDR@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_palette_to_rgb@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_compression_strategy@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_get_tRNS@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_write_info@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_packing@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_read_fn@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_create_info_struct@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_read_end@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_read_update_info@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_write_image@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_write_end@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_expand_gray_1_2_4_to_8@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_create_write_struct@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_read_image@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_read_info@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_strip_alpha@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_write_fn@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_destroy_write_struct@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_error@PNG16_0'
/home/andrei/anaconda/lib/libopencv_highgui.so: undefined reference to `png_set_strip_16@PNG16_0'
解决办法:
cd /usr/lib/x86_64-linux-gnu sudo ln -s ~/anaconda/lib/libpng16.so.16 libpng16.so.16 sudo ldconfig