Ubuntu 安装OpenCV3.0.0

时间:2021-11-29 16:15:50

Ubuntu安装OpenCV3.0.0
为了看看opencv3.0的HDR效果,尝试安装opencv3.0到ubuntu12.04上面,安装了好几次终于成功了。
参考博客:
http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/
http://blog.163.com/huai_jing@126/blog/static/171861983201311103411229

http://www.cnblogs.com/steven-blog/archive/2013/04/27/3046357.html
http://www.cnblogs.com/emouse/archive/2013/02/22/2922940.html
http://blog.sina.com.cn/s/blog_8835a8480100x23h.html
安装过程出现问题解决方法
http://blog.sina.com.cn/s/blog_675e6b570101gpsj.html
卸载方法 OpenCv卸载
http://*.com/questions/13134151/how-to-uninstall-opencv-in-ubuntu

You can also uninstall by going to the "build" folder directory of opencv from terminal, and execute the following
make uninstall
cd ..
sudo rm -r build
sudo rm -r /usr/local/include/opencv2
/usr/local/include/opencv
/usr/include/opencv
/usr/include/opencv2
/usr/local/share/opencv
/usr/local/share/OpenCV
/usr/share/opencv
/usr/share/OpenCV
/usr/local/bin/opencv*
/usr/local/lib/libopencv*

install reference
http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/

OpenCV安装
1 系统更新
利用ubuntu自带的更新管理器或者
sudo apt-get update
sudo apt-get upgrade

2 安装必要的组件
sudo apt-get install build-essential libgtk2.0-dev libjpeg-dev
libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy
python-tk libtbb-dev libeigen3-dev yasm libfaac-dev
libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev
libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common
texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev
libavformat-dev libswscale-dev default-jdk ant libvtk5-qt4-dev

3 下载opencv3.0
http://opencv.org/downloads.html
解压到某个文件下,这里选择
/home/yourname/opencv3.0

4  编译opecv3.0
进入解压后的opencv3.0目录
mkdir build
进入build目录
cd build
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D
INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON
-D WITH_QT=ON -D WITH_OPENGL=ON ..

5 安装opencv3.0
在build目录
make
sudo make install

6 配置opencv.conf file ,加入环境变量
sudo gedit /etc/ld.so.conf.d/opencv.conf

在opencv.conf里面加入命令
/usr/local/lib

更新库目录
sudo ldconfig

打开文件bash.bashrc
sudo gedit /etc/bash.bashrc
加入下面两行
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

7 测试你的opencv3.0是否安装成功

编译不通过请注销或者重启电脑试试
测试代码:opencvtest.cpp

  1. #include <opencv2/core/core.hpp>
  2. #include <opencv2/highgui/highgui.hpp>
  3. using namespace cv;
  4. int main()
  5. {
  6. Mat src = imread("test.jpg",1);
  7. imshow("src",src);
  8. waitKey(0);
  9. return 0;
  10. }

注意:linux下面的目录形式为/.../....

所以“#include <opencv2\core\core.hpp>” 会找不到头文件

编译方式:
1 命令行
g++  opencvtest.cpp -o opencvtest  `pkg-config  --cflags --libs opencv`

2 借助cmake,写CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

project(opencvtest)

find_package( OpenCV REQUIRED )

add_executable( opencvtest opencvtest.cpp )

target_link_libraries( opencvtest ${OpenCV_LIBS} )

之后 执行

cmake .
Make
 运行
./ opencvtest

8 opnecv3.0的HDR效果测试

  1. #include <opencv2/photo.hpp>
  2. #include "opencv2/imgcodecs.hpp"
  3. #include <opencv2/highgui.hpp>
  4. #include <vector>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8. using namespace cv;
  9. using namespace std;
  10. void loadExposureSeq(string, vector<Mat>&, vector<float>&);
  11. int main(int, char**argv)
  12. {
  13. vector<Mat> images;
  14. vector<float> times;
  15. string strpath = "./SampleLighthouse";
  16. loadExposureSeq(strpath, images, times);
  17. for (int i = 0; i < times.size(); ++i)
  18. {
  19. cout << times[i] << " ";
  20. }
  21. cout << endl << "here" << endl;
  22. Mat response;
  23. Ptr<CalibrateDebevec> calibrate = createCalibrateDebevec();
  24. calibrate->process(images, response, times);
  25. Mat hdr;
  26. Ptr<MergeDebevec> merge_debevec = createMergeDebevec();
  27. merge_debevec->process(images, hdr, times, response);
  28. Mat ldr;
  29. Ptr<TonemapDurand> tonemap = createTonemapDurand(2.2f);
  30. tonemap->process(hdr, ldr);
  31. Mat fusion;
  32. Ptr<MergeMertens> merge_mertens = createMergeMertens();
  33. merge_mertens->process(images, fusion);
  34. imwrite("fusion.png", fusion * 255);
  35. imwrite("ldr.png", ldr * 255);
  36. imwrite("hdr.hdr", hdr);
  37. return 0;
  38. }
  39. void loadExposureSeq(string path, vector<Mat>& images, vector<float>& times)
  40. {
  41. path = path + std::string("/");
  42. ifstream list_file((path + "ST.txt").c_str());
  43. string name;
  44. float val;
  45. while(list_file >> name >> val) {
  46. Mat img = imread(path + name);
  47. images.push_back(img);
  48. times.push_back(1 / val);
  49. }
  50. list_file.close();
  51. }

曝光时间 ST.txt
s1.jpg 40.0
s2.jpg 10.0
s3.jpg 2.0

Ubuntu  安装OpenCV3.0.0

Ubuntu  安装OpenCV3.0.0

Ubuntu  安装OpenCV3.0.0

HDR图片

在普通的显示中无法正常显示,就不给出了

LDR图片
Ubuntu  安装OpenCV3.0.0

色调映射算法感觉不好,图像的很多细节丢失了。