how to install opencv in ubuntu
2015-12-07 09:50
645 查看
Installation
Many people are having problem with installing OpenCV even from Ubuntu Software Centre. Here a simple .sh script file get all dependancy files from internet and compile the source finally install opencv on your system. So that users can easily write their CVfiles from C,C++, and Python
Step 1
Download the latest opencv.sh from https://github.com/jayrambhia/Install-OpenCV/blob/master/Ubuntu/ or Copythe following script to gedit and save as opencv.sh
切换行号显示
version="$(wget -q -O - http://sourceforge.net/projects/opencvlibrary/files/opencv-unix | egrep -m1 -o '\"[0-9](\.[0-9]+)+' | cut -c2-)" echo "Installing OpenCV" $version mkdir OpenCV cd OpenCV echo "Removing any pre-installed ffmpeg and x264" sudo apt-get -qq remove ffmpeg x264 libx264-dev echo "Installing Dependenices" sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-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 ffmpeg cmake qt5-default checkinstall echo "Downloading OpenCV" $version wget -O OpenCV-$version.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/$version/opencv-"$version".zip/download echo "Installing OpenCV" $version unzip OpenCV-$version.zip cd opencv-$version 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 WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON .. make -j2 sudo checkinstall sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf' sudo ldconfig echo "OpenCV" $version "ready to be used"
Note: If you are running 13.10 and you don't have a nvidia card then ensure you install ocl-icd-libopencl1 (sudo apt-get install ocl-icd-libopencl1) before running this script. Ubuntu 13.10 will install nvidia-319-updates as a dependency for libopencv-dev by
default if ocl-icd-libopencl1 is not installed (see bug
report).
Note: As of Utopic (14.10) libxine-dev is replaced with libxine2-dev
Step 2
Open terminal.切换行号显示
1 $ chmod +x opencv.sh 2 $ ./opencv.sh
This will complete opencv installation
Running OpenCV
Python
Loading an image in Python
切换行号显示1 from cv2.cv import * 2 3 img = LoadImage("/home/USER/Pictures/python.jpg") 4 NamedWindow("opencv") 5 ShowImage("opencv",img) 6 WaitKey(0)
切换行号显示
1 $ python filename.py
Note that the test program waits for a key press to end.
in C
Loading an image file in C
切换行号显示1 #include 2 #include<opencv2/highgui/highgui.hpp> 3 4 int main() 5 { 6 IplImage* img = cvLoadImage("/home/USER/Pictures/python.jpg",CV_LOAD_IMAGE_COLOR); 7 cvNamedWindow("opencvtest",CV_WINDOW_AUTOSIZE); 8 cvShowImage("opencvtest",img); 9 cvWaitKey(0); 10 cvReleaseImage(&img); 11 return 0; 12 }
To compile C program, Let’s assume the file is opencvtest.c
切换行号显示
1 $ gcc -ggdb `pkg-config --cflags opencv` -o `basename opencvtest.c .c` opencvtest.c `pkg-config --libs opencv` 2 $ ./opencvtest
In C++
Loading an image file in C++
切换行号显示1 #include<opencv2/highgui/highgui.hpp> 2 using namespace cv; 3 4 int main() 5 { 6 7 Mat img = imread("/home/USER/Pictures/python.jpg",CV_LOAD_IMAGE_COLOR); 8 imshow("opencvtest",img); 9 waitKey(0); 10 11 return 0; 12 }
to compile in C++
切换行号显示
1 $ g++ -ggdb `pkg-config --cflags opencv` -o `basename opencvtest.cpp .cpp` opencvtest.cpp `pkg-config --libs opencv` 2 $ ./opencvtest
Note: Always include OpenCV header files in C and C++ as
切换行号显示
1 #include "opencv2/core/core_c.h" 2 #include "opencv2/core/core.hpp" 3 #include "opencv2/flann/miniflann.hpp" 4 #include "opencv2/imgproc/imgproc_c.h" 5 #include "opencv2/imgproc/imgproc.hpp" 6 #include "opencv2/video/video.hpp" 7 #include "opencv2/features2d/features2d.hpp" 8 #include "opencv2/objdetect/objdetect.hpp" 9 #include "opencv2/calib3d/calib3d.hpp" 10 #include "opencv2/ml/ml.hpp" 11 #include "opencv2/highgui/highgui_c.h" 12 #include "opencv2/highgui/highgui.hpp" 13 #include "opencv2/contrib/contrib.hpp" 14
A bash script to compile opencv programs.Making a Bash Script to Compile OpenCV:
It’s kind of boring typing all this stuff. So, A bash file to compile OpenCV programs. Name
it .compile_opencv.sh and keep it in your home directory.
切换行号显示
1 #!/bin/bash 2 echo "compiling $1" 3 if [[ $1 == *.c ]] 4 then 5 gcc -ggdb `pkg-config --cflags opencv` -o `basename $1 .c` $1 `pkg-config --libs opencv`; 6 elif [[ $1 == *.cpp ]] 7 then 8 g++ -ggdb `pkg-config --cflags opencv` -o `basename $1 .cpp` $1 `pkg-config --libs opencv`; 9 else 10 echo "Please compile only .c or .cpp files" 11 fi 12 echo "Output file => ${1%.*}"
Add an alias in .bashrc or .bash_aliases
切换行号显示
1 $ alias opencv="~/.compile_opencv.sh" 2 $ opencv opencvtest.c 3 $ ./opencvtest
Note that the .bashrc is a hidden file in the $HOME directory (cd ~ to access the home directory). Do not include the '$' characters at the beginning of each line. The alias will work after you log out and back. You can type the alias opencv... command at the
prompt to set the alias for the current session.
相关文章推荐
- 配置maven构建的webApp项目部署到Tomcat服务器
- centOS常用指令及快捷键
- Linux命令大全
- Linux内核模块简介
- 【转】深入浅出:Linux设备驱动之字符设备驱动
- Centos 操作指令-2
- 头像裁剪插件Jcrop的使用示例
- OPP--面向对象知识点
- centos mysql 安装及配置
- CentOS 操作指令-1
- 京东咚咚架构演进
- 每天一个Linux命令(2):cd
- VS2010(VS2008)下安装配置OpenCV
- 在树莓派上搭建LAMP(Linux Apache Mysql PHP)服务器
- Nginx 学习笔记
- shell中局部变量及local命令
- linux中各种括号之(),(()),[],[[]],{}
- # Optional 可选值
- mooc网站以及学习资料收集
- python scrapy window linux 坑