1. 先下载OpenCV的源码 http://opencv.org/downloads.html
2. 解压到任意目录
unzip opencv-2.4.10.zip
3.进入源码目录
cd opencv-2.4.10
4. 事先安装下列软件
sudo apt-get install build-essential cmake libgtk2.0-dev pkg-config python-dev python-numpy libavcodec-dev libavformat-dev libswscale-dev
5. 进入cmake
cd cmake
6. cmake编译生成Makefile,
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
安装所有的lib文件都会被安装到/usr/local目录
7. 编译,并安装
sudo make install
8. 测试
1)源代码:
新建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;
- }