关于这个问题,网上有很多现成的方法,但是很多方法也都存在问题,所以自己总结一下,把自己的安装过程记录下来,也方便以后查阅。
1.需要的包:
GCC 4.4.x 或更高
CMake 2.8.7 或更高
Git
GTK+2.x 或更高,包括头(libgtk2.0-dev)
pkg-config
Python 2.6 或以后,Numpy 1.5或以后,和开发包(python-dev,python-numpy)
ffmpeg 或者libav开发包:libavcodec-dev, libavformat-dev, libswscale-dev
可选包:libtbb2 ,libtbb-dev
可选包:libdc1394 2.x
可选包:libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
(如果需要的包没有安装的话,openCV是没法使用的,所以还是按步骤老老实实一个一个安装把)
然后准备工作做完了,接下来就是开始安装了。
1.
sudo apt-get install build-essential
<pre class="brush:shell;toolbar: true; auto-links: false;"><code class="hljs q">sudo apt-<span >get</span> install cmake git libgtk2.0-<span >dev</span> pkg-config libavcodec-<span >dev</span> libavformat-<span >dev</span> libswscale-<span >dev</span></code>
2.下载opencV压缩包,并解压到你需要的位置
3.利用终端进入你解压的opencV的包
4.新建build文件夹并进入这个文件夹
mkdir build
cd build
5.配置opencV,
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
6.进行编译(这个过程话费的时间比较长,可能有十分钟到半个小时)
make
7.安装opencV库到系统
sudo make install
这些步骤完成之后,一般opencV就可以使用了。
然后测试一下。
新建一个HelloWorld.cpp的源程序(为了保证程序的运行,提前把测试图片发到这个程序的同一个目录下)
#include<stdio.h>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
int main( int argc, char** argv )
{
if( argc != 2 )//判断参数是否是两个
{
printf( " No image data \n " );
return -1;
}
char* imageName = argv[1];
Mat image;
image = imread( imageName, 1 );//读入图片
if( !image.data)//判断是否有数据
{
printf( " No image data \n " );
return -1;
}
namedWindow( imageName, CV_WINDOW_AUTOSIZE );
imshow( imageName, image );//显示图片
waitKey(0);
return 0;
}
新建CMakeList.txt文件如下(里面的参数是我的文件的名字,如果你文件的名字不同,也需要做相应的更改。)
cmake_minimum_required(VERSION 2.8)
project(helloworld)
add_executable(helloworld HelloWorld.cpp)
find_package(OpenCV REQUIRED)
target_link_libraries(helloworld ${OpenCV_LIBS}
然后就可以编译
mkdir build
cd build
cmkae ..
make
编译完成之后就运行它(把图片作为参数传递进函数)
./helloworld pitcure.jpg
如果各项步骤等执行正确的话,运行程序,就可以看到显示的图片了。
可能遇到的问题:
(1):报错如下:
In file included from /home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:45:0:
/home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp:1174:0: warning: "CV_CODEC_ID" redefined
#define CV_CODEC_ID c_id = c->codec_id;
^
/home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp:162:0: note: this is the location of the previous definition
# define CV_CODEC_ID AVCodecID
^
In file included from /home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:45:0:
/home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp:1556:0: warning: "static" redefined
#define static inline bool cv_ff_codec_tag_list_match(const AVCodecTag *const *
^
/home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp:1546:0: note: this is the location of the previous definition
#define static inline bool cv_ff_codec_tag_match(const AVCodecTag *tags, enum C
^
In file included from /home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:45:0:
/home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp: In function ‘AVStream* icv_add_video_stream_FFMPEG(AVFormatContext*, AVCodecID, int, int, int, double, int)’:
/home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp:1177:19: error: ‘c_id’ was not declared in this scope
c->codec_id = c_id;
^
In file included from /home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:45:0:
/home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp: At global scope:
/home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp:1547:1: error: expected unqualified-id before ‘{’ token
{
^
In file included from /home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:45:0:
/home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp:1023:21: warning: ‘const char* icvFFMPEGErrStr(int)’ defined but not used [-Wunused-function]
static const char * icvFFMPEGErrStr(int err)
^
/home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp:1104:18: warning: ‘AVFrame* icv_alloc_picture_FFMPEG(int, int, int, bool)’ defined but not used [-Wunused-function]
static AVFrame * icv_alloc_picture_FFMPEG(int pix_fmt, int width, int height, b
^
/home/cuizhiyan/code/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp:1130:18: warning: ‘AVStream* icv_add_video_stream_FFMPEG(AVFormatContext*, AVCodecID, int, int, int, double, int)’ defined but not used [-Wunused-function]
static AVStream *icv_add_video_stream_FFMPEG(AVFormatContext *oc,
^
modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:182: 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:6727: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
Makefile:149: recipe for target 'all' failed
make: *** [all] Error 2
从上面可以看出,编译失败了,主要原因是因为ffmpeg这个东西的问题(查了不少东西,这个ffmpeg好象是为了显示视频用的,有些人建议如果只用opencv做图片开发,可以把这个卸载了。但是我觉得这样做不保险,以后很有可能要操作视频,到时候又会报错。所以这个方法不可取)
最后找了一个确实可行的解决方法,该报错的原因是因为opencv源码里本来有bug,需要对opencv的源码进行更改,附上链接:
具体的操作是,如果你用的是opencv3.0.0版本,那么修改文件
opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp,
除了在#define定义的后面的那个以外,把剩下所有的AVCodecID用CV_CODEC_ID替换。
(2):报错如下:
/home/usrname/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:120:54: error: ‘NppiGraphcutState’ has not been declared
typedef NppStatus (*init_func_t)(NppiSize oSize, NppiGraphcutState** ppStat
^/home/usrname/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:135:18: error: ‘NppiGraphcutState’ does not name a type
operator NppiGraphcutState*() ^/home/usrname/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:141:9:
error: ‘NppiGraphcutState’ does not name a type NppiGraphcutState* pState;
这个包括是因为CUDA的问题,尝试改一处源码就可以了。
在graphcut.cpp文件中,将
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
改为
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)
就可以了。