opencv2.4.9在ubuntu(树莓派)上安装
参考原文:https://my.oschina.net/u/1757926/blog/293976
1. 先从sourceforge上下载OpenCV的源码
2. 解压到任意目录
-
unzip opencv-2.4.9.zip
3. 进入源码目录,创建release目录
-
cd opencv-2.4.9
-
mkdir release
4. 可以看到在OpenCV目录下,有个CMakeLists.txt文件,需要事先安装一些软件
-
sudo apt-get install build-essential cmake libgtk2.0-dev pkg-config python-dev python-numpy libavcodec-dev libavformat-dev libswscale-dev
5. 新建release目录,并进入release目录,安装OpenCV是所有的文件都会被放到这个release目录下
-
cd release
6. cmake编译OpenCV源码,安装所有的lib文件都会被安装到/usr/local目录下
-
cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_FFMPEG=OFF -D CMAKE_INSTALL_PREFIX=/usr/local ..
7. 安装
-
sudo make install
8. 测试,在某个目录下建立一个test.cpp文件
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main(int argc, char* argv[])
{
Mat image;
image = imread(argv[1], 1);
if (argc != 2 || !image.data)
{
printf("No image data\n");
return -1;
}
namedWindow("Display Image", CV_WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(0);
return 0;
}
9. 写一个cmake的makefile,也叫CMakeLists.txt
project(test)
find_package(OpenCV REQUIRED)
add_executable(test test)
target_link_libraries(test ${OpenCV_LIBS})
cmake_minimum_required(VERSION 2.8)
10. 编译+运行
-
cmake .
-
make
-
得到可执行文件test10. 编译+运行
11.安装树莓派时会出现
error when trying to build opencv 2.4.9
home/greg/Applications/opencv-2.4.10/modules/highgui/src/ffmpeg_codecs.hpp:104:7: error: ‘CODEC_ID_H264’ was not declared in this scope
{ CODEC_ID_H264, MKTAG('H', '2', '6', '4') },
^
解决方式:cmake ......... -D WITH_FFMPEG=OFF
12. 打开摄像头
int main()
{
VideoCapture cap(0);
if(!cap.isOpened())
{
printf("camera open faile!\n");
return -1;
}
namedWindow("hhh", CV_WINDOW_AUTOSIZE);
while(1)
{
Mat frame;
cap>>frame;
imshow("hhh",frame);
waitKey(30);
}
return 0;
}