参考网址:
https://www.cnblogs.com/mypsq/p/6114824.html
https://blog.csdn.net/solomon1558/article/details/51967280
----------------------------------------------------------
我是在Ubuntu14.4 下 安装的opencv-2.4.13.3
$ unzip opencv-2.4.13.3.zip
$ cd opencv-2.4.13.3.
$ mkdir build
$ cd build
$ cmake ..
$ sudo make -j4
$ sudo make install
- 下载opencv源码opencv-2.4.13.3.zip并上传到linux中,建立opncv目录放在里面
- 解压
amm@ubuntu:~/opencv/ $ unzip opencv-2.4.13.3.zip
- amm@ubuntu:~/opencv/opencv-2.4.13$ mkdir build
- amm@ubuntu:~/opencv/opencv-2.13.3$ cd build/
- amm@ubuntu:~/opencv/opencv-2.4.13.3/build$ cmake ..
- amm@ubuntu:~/opencv/opencv-2.4.13.3/build$ sudo make –j4
- amm@ubuntu:~/opencv/opencv-2.4.13.3/build$ sudo make install
完成
- 配置环境变量
添加库路径(创建opencv.conf文件)
输入命令:vi /etc/ld.so.conf.d/opencv.conf
输入/usr/local/lib,并保存退出
添加环境变量
输入命令:vi /etc/profile
末尾加入:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
更新环境变量:source /etc/profile
设置另外环境变量
输入命令:vi /etc/bash.bashrc
在末尾加入:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
更新环境变量:source /etc/profile
- 查看opencv是否安装成功
输入命令:
pkg-config—cflags opencv
pkg-config—libs opencv
没报错就证明是正确的。
- 测试程序
在linux系统下,/opencv/OpenCV_Code/下放入 opencv_test.c 和 fruit.jpg
Opencv_test.c 文件内容为:
#include <highgui.h>
int main(int argc,char ** argv) {
IplImage* img = cvLoadImage(argv[1],CV_LOAD_IMAGE_COLOR);
cvNamedWindow("Image_show",CV_WINDOW_AUTOSIZE);
cvShowImage("Image_show",img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Image_show");
return 0;
}
- 进行编译测试
方法一:
gcc Test.c -o Test `pkg-config --cflags --libs opencv`
./Test fruit.jpg
可以看到成功,所以在linux中opencv安装成功。
方法二:
编写CMakeLists.txt 进行编译
CMakeLists.txt内容如下:
#设置CMAKE最低版本 CMAKE_MINIMUM_REQUIRED(VERSION 3.11) #设置项目名称 SET(PROJECT_NAME opencv_test) #建立项目 PROJECT(${PROJECT_NAME}) #寻找OpenCV库 FIND_PACKAGE(OpenCV REQUIRED) INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS}) #打印调试信息 MESSAGE(STATUS "Project: ${PROJECT_NAME}") MESSAGE(STATUS "OpenCV library status:") MESSAGE(STATUS " version: ${OpenCV_VERSION}") MESSAGE(STATUS " libraries: ${OpenCV_LIBS}") MESSAGE(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
#获取代码,在项目中,将所有代码都放在src文件夹中 AUX_SOURCE_DIRECTORY(src DIR_SRCS) MESSAGE(STATUS "Src file: ${DIR_SRCS}") #编译可执行程序 ADD_EXECUTABLE(${PROJECT_NAME} ${DIR_SRCS}) #如果要生成动态链接库 #ADD_LIBRARY(${PROJECT_NAME} SHARED ${DIR_SRCS})
#添加链接库 TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS}) |
Opencv_test.c 内容如下:
#include <highgui.h>
int main(int argc,char ** argv) { IplImage* img = cvLoadImage(argv[1],CV_LOAD_IMAGE_COLOR); cvNamedWindow("Image_show",CV_WINDOW_AUTOSIZE); cvShowImage("Image_show",img); cvWaitKey(0); cvReleaseImage(&img); cvDestroyWindow("Image_show"); return 0; } |
目录结构如下,建立空文件夹build, 将opencv_test.c放入src文件夹中
进行编译
Cmake ..
Make
./opencv_test ../fruit.jpg
成功