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.编写程序(图像二值化)
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <iostream>
using namespace std;
int main (int argc, char *argv[])
{
IplImage* img = cvLoadImage(argv[1], -1);
IplImage* biimg = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
CvScalar s;
int sum = 0;
for (int i = 0;i < img->height;i++)
{
for (int j = 0;j < img->width;j++)
{
s = cvGet2D(img, i, j);
cout << s.val[0] << " " << s.val[1] << " " << s.val[2] <<endl;
sum = (s.val[0] + s.val[1] + s.val[2]) / 3;
if (sum > 128)
s.val[0] = s.val[1] = s.val[2] = 255;
else
s.val[0] = s.val[1] = s.val[2] = 0;
cvSet2D(img, i, j, s);
}
}
cvCopy(img, biimg);
cvSaveImage("combibi.JPG", biimg);
cvNamedWindow("imagebinary");
cvShowImage("imagebinary", biimg);
cvWaitKey(0);
cvDestroyWindow("imagebinary");
cvReleaseImage(&img);
cvReleaseImage(&biimg);
return 0;
}
编译
$ gcc erzhihua.cpp -o erzhi `pkg-config --cflags --libs opencv`
(疑问:编译时运行 gcc `pkg-config --cflags --libs opencv` erzhihua.cpp -o erzhi 就会报错,还不知道为什么??)
运行
$ ./erzhi lena.jpg
运行结果:
以上图片上传到红联Linux系统教程频道中。