1,安装libgtk2.0-dev and pkg-config
终端运行 sudo apt-get install libgtk2.0-dev pkg-config
2.下载OpenCV-2.3.1(到openCV官网下载即可)
cd 命令进入相应的下载目录。依次运行下列命令
$ tar xf OpenCV-2.3.1.tar.bz2
$ cd OpenCV-2.3.1
3.终端运行
$ mkdir build
$ cd build
$ cmake .. 此处为cmake空格后面两个点。
4 依次运行
$ make
$ sudo make install
此时编译成功。
5 配置必要的库。
运行
$ sudo
gedit /etc/ld.so.conf.d/opencv.conf
添加 /usr/
local
/lib
到文件中
6 运行
$ sudo
ldconfig
7 运行
sudo
gedit /etc/
bash
.bashrc,
添加下面两行到文件的末尾并保存。
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
8 运行samples
$ cd ~
$ mkdir openCV_samples
$ cp OpenCV-2.3.1/samples/c/* openCV_samples
$ cd openCV_samples/
$ chmod +x build_all.sh
$ ./build_all.sh
$./one_way_sample . ./one_way_train_0000.jpg ./one_way_train_0001.jpg
运行结果
8 编写程序(图像二值化)
1 #include "cv.h" 2 #include "cxcore.h" 3 #include "highgui.h" 4 #include <iostream> 5 6 using namespace std; 7 8 int main (int argc, char *argv[]) 9 { 10 IplImage* img = cvLoadImage(argv[1], -1); 11 IplImage* biimg = cvCreateImage(cvGetSize(img), img->depth, img->nChannels); 12 CvScalar s; 13 int sum = 0; 14 for (int i = 0;i < img->height;i++) 15 { 16 for (int j = 0;j < img->width;j++) 17 { 18 s = cvGet2D(img, i, j); 19 cout << s.val[0] << " " << s.val[1] << " " << s.val[2] <<endl; 20 sum = (s.val[0] + s.val[1] + s.val[2]) / 3; 21 if (sum > 128) 22 s.val[0] = s.val[1] = s.val[2] = 255; 23 else 24 s.val[0] = s.val[1] = s.val[2] = 0; 25 26 cvSet2D(img, i, j, s); 27 } 28 } 29 cvCopy(img, biimg); 30 cvSaveImage("combibi.JPG", biimg); 31 cvNamedWindow("imagebinary"); 32 cvShowImage("imagebinary", biimg); 33 cvWaitKey(0); 34 cvDestroyWindow("imagebinary"); 35 cvReleaseImage(&img); 36 cvReleaseImage(&biimg); 37 return 0; 38 }
编译
$ gcc erzhihua.cpp -o erzhi `pkg-config --cflags --libs opencv`
(疑问:编译时运行 gcc `pkg-config --cflags --libs opencv` erzhihua.cpp -o erzhi 就会报错,还不知道为什么??)
运行
$ ./erzhi lena.jpg
运行结果: