Ubuntu 12.04下安装OpenCV 2.3.1,图像二值化
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
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] <
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
运行结果:
--------------------------------------分割线 --------------------------------------
Ubuntu Linux下安装OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm
Ubuntu 12.04 安装 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm
CentOS下OpenCV无法读取视频文件 http://www.linuxidc.com/Linux/2011-07/39295.htm
Ubuntu 12.04下安装OpenCV 2.4.5总结 http://www.linuxidc.com/Linux/2013-06/86704.htm
Ubuntu 10.04中安装OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm
基于QT和OpenCV的人脸识别系统 http://www.linuxidc.com/Linux/2011-11/47806.htm
[翻译]Ubuntu 14.04, 13.10 下安装 OpenCV 2.4.9 http://www.linuxidc.com/Linux/2014-12/110045.htm
--------------------------------------分割线 --------------------------------------
OpenCV的详细介绍:请点这里
OpenCV的下载地址:请点这里
更多Ubuntu相关信息见Ubuntu 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=2
本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-02/113627.htm