博主最近在研究图像处理相关的项目,需要用到OPENCV库,前前后后遇到各种问题各种编译报错,大概前后快一周了。
还有受到了一些博客的误导,真的花了很多时间。
就在今天,就在刚才,终于编译成功了!!!!!
撒花!
这里和大家分享如何无痛一次,在ubuntu系统下完成我们的OPENCV编译,并且可以使用OPENCV扩展库。
特别感谢:
https://blog.csdn.net/jindunwan7388/article/details/80397700
确实在那之后这两种收到商业保护的算子从opencv库中移除了出去。但是只需要需要下载opencv_contrib库,并且编译时设置选项即可使用。
简直就是给我等小白开发的神器,解决了博主在编译下载各个环节的错误,可视化操作简单易懂!
基于上述,给大家总结一下博主基于CMake-gui的OPENCV一步到位配置编译过程。
1、首先更新一下系统
sudo apt-get update
sudo apt-get upgrade
2、接着安装官方给的opencv依赖包,在终端输入:
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
3、单击下载opencv及opencv_contrib
https://github.com/Itseez/opencv/archive/4.1.0.zip
https://github.com/opencv/opencv_contrib/archive/4.1.0.zip
4、接下来安装CMake-gui,在终端输入
sudo apt-get install cmake-qt-gui
1、将OpenCV4.1.0和OpenCV_contrib-4.1.0解压(提取),如图所示
2、双击进入解压出来的OpenCV文件夹,右键打开终端(或者在别处打开终端,通过输入cd opencv进入当前目录下),然后依次输入(不要忘了第三行的最后的空格和两个点):
mkdir build
cd build
cmake-gui ..
3、然后会弹出CMake的图形化界面,在上方的两个路径里面,选择好代码所在文件夹的路径和要安装的路径
4、点击左下方的Configure按钮,选择Unix Makefiles,选择Use default native compilers(默认),然后点击Finish
5、需要下载一些文件,要等待一段时间。然后CMake即载入默认配置,如下图所示:
窗口的中间部分即配置列表,这里和使用cmake命令直接生成makefile文件一致的。正如上文所述,这里通过图形界面的方式来进行配置,更加直观方便。不知道比网上那些代码行方便到哪儿去了。
6、针对个人需求进行修改:
1)在CMAKE_BUILD_TYPE 值处输入RELEASE,其他保持不变(如果已经存在就不必修改)。
2)在OPENCV_EXTRA_MODULES_PATH处,选择输入目录(单击这一行后方空白处即可选中),选到我们刚才解压的opencv_contrib/modules那里
3)另外在OPENCV_ENABLE_NONFREE这个地方也要打上勾,才能使用surf,sift这些算子。别问我怎么知道的,这里不打勾的话,你后面用到这两个算子会报错,然后你一查才发现原来是在这里。(真的是一坑接一坑)
7、点击Generate生成配置文件
8、接着,在build目录下打开终端,输入
make
sudo make install
9、然后就开始安装了,这一步又需要等比较长的时间了……不要急,进行到这一步说明很快就能安装成功了~
10、等下面显示到100%就Ok了
1、安装成功后还需要设置opencv的环境变量。打开文件:
sudo gedit /etc/ld.so.conf.d/opencv.conf
2、将以下内容添加到最后:
/usr/local/lib
3、接下来配置库:
sudo ldconfig
4、更改环境变量:
sudo gedit /etc/bash.bashrc
5、在文件后添加:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
6、保存退出,在运行下面的例程之前,需要重新开启终端来使配置生效。
到此,安装和配置的整个过程都完成了!
为了方便大家,我把测试的代码放在了我的github上,欢迎大家下载
https://github.com/bilibili2007/opencv
displayImage.cpp
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
cout<<"usage: DisplayImage.out \n" ;
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
cout<<"No image data \n";
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
CMakeLists.txt
# cmake needs this line
cmake_minimum_required(VERSION 3.1)
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Define project name
project(DisplayImage)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# Declare the executable target built from your sources
add_executable(DisplayImage DisplayImage.cpp)
# Link your application with OpenCV libraries
target_link_libraries(DisplayImage ${OpenCV_LIBS})
1、在同一目录下放置上面的两个文件和一张图片,如下图所示:
2、按照顺序在终端中输入
cmake .
make
./DisplayImage box.jpg
步骤同上,这里不再赘述,只给出代码和运行结果。
FeatureDetection.cpp
#include <iostream>
#include "opencv2/core.hpp"
#ifdef HAVE_OPENCV_XFEATURES2D
#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/xfeatures2d.hpp"
using namespace cv;
using namespace cv::xfeatures2d;
using std::cout;
using std::endl;
int main( int argc, char* argv[] )
{
CommandLineParser parser( argc, argv, "{@input | box.jpg | input image}" );
Mat src = imread( parser.get<String>( "@input" ), IMREAD_GRAYSCALE );
if ( src.empty() )
{
cout << "Could not open or find the image!\n" << endl;
cout << "Usage: " << argv[0] << " " << endl;
return -1;
}
//-- Step 1: Detect the keypoints using SURF Detector
int minHessian = 400;
Ptr<SURF> detector = SURF::create( minHessian );
std::vector<KeyPoint> keypoints;
detector->detect( src, keypoints );
//-- Draw keypoints
Mat img_keypoints;
drawKeypoints( src, keypoints, img_keypoints );
//-- Show detected (drawn) keypoints
imshow("SURF Keypoints", img_keypoints );
waitKey();
return 0;
}
#else
int main()
{
std::cout << "This tutorial code needs the xfeatures2d contrib module to be run." << std::endl;
return 0;
}
#endif
CMakeLists.txt
# cmake needs this line
cmake_minimum_required(VERSION 3.1)
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Define project name
project(FeatureDetection)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# Declare the executable target built from your sources
add_executable(FeatureDetection FeatureDetection.cpp)
# Link your application with OpenCV libraries
target_link_libraries(FeatureDetection ${OpenCV_LIBS})
输出结果为SURF算子检测到的特征点可视化
得到上述的结果说明SURF算子可以使用。
恭喜 !说明你已经搭建了OPENCV环境!祝你在图像处理的正义之地好运连连!