树莓派学习:源码方式安装opencv
1:下载若干依赖项:
更新软件源:sudo apt-get update/upgrade;
依次安装一下依赖项:
sudo apt-get install build-essential sudo apt-get install cmake sudo apt-get install libgtk2.0-dev sudo apt-get install pkg-config sudo apt-get install python-dev python-numpy sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev2:获取源代码:本人从opencv官网下载opencv2.4.10的linux版本。建立opencv文件夹,解压opencv2.4.10与opencv内。
unzip opencv-2.4.10.zip
3:编译源代码:
进入opencv源代码目录,新建一个名为release的文件夹:
cd /home/pi/opencv/opencv-2.4.10mkdir releasecd release使用cmake工具在该文件夹中生成makefile文件,指定opencv的安装目录为/usr/local/cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_FFMPEG=OFF -D CMAKE_INSTALL_PREFIX=/usr/local ..注意需要加上-D WITH_FFMPEG=OFF,不然编译过程中会出现错误。原因是ffmpeg的更新问题。编译源代码的时间非常的长。makesudo make install; 4:测试test.cpp#include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace cv; using namespace std; int main (int argc, char **argv) { Mat image, image_gray; image = imread(argv[1], CV_LOAD_IMAGE_COLOR ); if (argc != 2 || !image.data) { cout << "No image data\n"; return -1; } cvtColor(image, image_gray, CV_RGB2GRAY); namedWindow("image", CV_WINDOW_AUTOSIZE); namedWindow("image gray", CV_WINDOW_AUTOSIZE); imshow("image", image); imshow("image gray", image_gray); waitKey(0); return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.8) project(test) find_package(OpenCV REQUIRED) add_executable(test test.cpp) target_link_libraries(test ${OpenCV_LIBS})
cmake . 生成Makefile文件,注意“.”不能少
make编译生成test程序。
./test 1.jpg 进行测试