在树莓派3 Ubuntu 16.04 Mate中安装OpenCV

1. Install OpenCV in Ubuntu:

 

(1) Update the list of package repositories:


  1. sudo apt-get update  
  2. sudo apt-get upgrade  

(2) Remove previously installed FFMPEG and x264 libraries:


  1. sudo apt-get remove ffmpeg x264-dev  

(3) Install the necessary packages for compiling the OpenCV sources:

  1. sudo apt-get install ocl-icd-libopencl1 build-essential checkinstall cmake pkg-config yasm libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libgstreamer0.10-dev  libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils  

(4) Make a directory to download and build OpenCV:


  1. mkdir opencv  
  2. cd opencv  

(5) Download the OpenCV sources for Linux, then unzip it:


  1. wget -O opencv-2.4.10.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.10/opencv-2.4.10.zip/download  
  2. unzip opencv-2.4.10.zip  
  3. cd opencv-2.4.10  

(6) Create a directory to compile the OpenCV sources:


  1. mkdir build  
  2. cd build  

(7) Building OpenCV sources with CMake and install:


  1. cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..  


(8) Make and install:


  1. make  
  2. sudo make install  
  3. sudo sh -c 'echo "/usr/local/lib">/etc/ld.so.conf.d/opencv.conf'  
  4. sudo ldconfig  
                     

(9) Restart the system for everything to take effect:


  1. sudo shutdown -r now  


2. Test OpenCV in Ubuntu:

 

(1) Create a directory DisplayImage for test project:


  1. mkdir DisplayImage  
  2. cd DisplayImage  

(2) Create DisplayImage.cpp and edit it:


  1. gedit DisplayImage.cpp  

Then edit the DisplayImage.cpp:


  1. #include   
  2. #include   
  3. using namespace cv;  
  4.    
  5. int main(int argc, char** argv)  
  6. {  
  7.          if(argc!= 2)  
  8.          {  
  9.                    printf("usage:DisplayImage.out \n");  
  10.                    return -1;  
  11.          }  
  12.    
  13.          Mat image;  
  14.          image = imread(argv[1], 1);  
  15.    
  16. if(!image.data)  
  17. {  
  18.                    printf("Noimage data\n");  
  19.                    return -1;  
  20.          }  
  21.    
  22.          namedWindow("DisplayImage",CV_WINDOW_AUTOSIZE);  
  23.          imshow("DisplayImage", image);  
  24.    
  25.          waitKey(0);  
  26.          return 0;  
  27. }  

(3) Create a CMake file:


  1. gedit CMakeLists.txt  

Then edit the CMakeLists.txt:


  1. cmake_minimum_required(VERSION 2.8)  
  2. project(DisplayImage)  
  3. find_package(OpenCV REQUIRED)  
  4. add_executable(DisplayImage DisplayImage.cpp)  
  5. target_link_libraries(DisplayImage ${OpenCV_LIBS})  

(4) Generate the executable file:


  1. cmake .  
  2. make  

(5) Execute it:


  1. ./DisplayImage lena.jpg  

lena.jpg is the test image



 ffmpeg  error:

cmake  ........  -D  WITH_FFMPEG=OFF

你可能感兴趣的:(ubuntu,树莓派)