编译opencv2.4.9

来源:互联网 发布:天尚网络机顶盒 编辑:程序博客网 时间:2024/06/05 15:17

终于,在填了不少坑后,编译通过了。
编译命令

mkdir buildcd buildcmake ..  -DENABLE_PRECOMPILED_HEADERS=OFFmake -j32

然而
cmake配置文件

cmake_minimum_required(VERSION 3.7)project(study_opencv)set(CMAKE_CXX_STANDARD 14)set(CMAKE_MODULE_PATH /home/pikachu/dev/opencv-2.4.9/build)set(CMAKE_PREFIX_PATH /home/pikachu/dev/opencv-2.4.9/build)FIND_PACKAGE(OpenCV REQUIRED)include_directories(include)aux_source_directory(src DIRSRCS)set(SOURCE_FILES main.cpp)add_executable(study_opencv ${SOURCE_FILES} ${DIRSRCS})target_link_libraries(study_opencv ${OpenCV_LIBS})

cpp代码

#include <stdio.h>#include <iostream>#include <cvaux.h>#include "opencv2/core/core.hpp"//因为在属性中已经配置了opencv等目录,所以把其当成了本地目录一样#include "opencv2/features2d/features2d.hpp"#include "opencv2/highgui/highgui.hpp"#include "opencv2/nonfree/features2d.hpp"using namespace cv;using namespace std;void readme();int main(int argc,char* argv[]){    Mat img_1=imread("/home/pikachu/Desktop/1.png",CV_LOAD_IMAGE_GRAYSCALE);//宏定义时CV_LOAD_IMAGE_GRAYSCALE=0,也就是读取灰度图像    Mat img_2=imread("/home/pikachu/Desktop/2.png",CV_LOAD_IMAGE_GRAYSCALE);//一定要记得这里路径的斜线方向,这与Matlab里面是相反的    if(!img_1.data || !img_2.data)//如果数据为空    {        cout<<"opencv error"<<endl;        return -1;    }    cout<<"open right"<<endl;//第一步,用SURF算子检测关键点    int minHessian=400;    SurfFeatureDetector detector(minHessian);    std::vector<KeyPoint> keypoints_1,keypoints_2;//构造2个专门由点组成的点向量用来存储特征点    detector.detect(img_1,keypoints_1);//将img_1图像中检测到的特征点存储起来放在keypoints_1中    detector.detect(img_2,keypoints_2);//同理//在图像中画出特征点    Mat img_keypoints_1,img_keypoints_2;    drawKeypoints(img_1,keypoints_1,img_keypoints_1,Scalar::all(-1),DrawMatchesFlags::DEFAULT);    drawKeypoints(img_2,keypoints_2,img_keypoints_2,Scalar::all(-1),DrawMatchesFlags::DEFAULT);    imshow("surf_keypoints_1",img_keypoints_1);    imshow("surf_keypoints_2",img_keypoints_2);//计算特征向量    SurfDescriptorExtractor extractor;//定义描述子对象    Mat descriptors_1,descriptors_2;//存放特征向量的矩阵    extractor.compute(img_1,keypoints_1,descriptors_1);    extractor.compute(img_2,keypoints_2,descriptors_2);//用burte force进行匹配特征向量    BruteForceMatcher<L2<float>>matcher;//定义一个burte force matcher对象    vector<DMatch>matches;    matcher.match(descriptors_1,descriptors_2,matches);//绘制匹配线段    Mat img_matches;    drawMatches(img_1,keypoints_1,img_2,keypoints_2,matches,img_matches);//将匹配出来的结果放入内存img_matches中//显示匹配线段    imshow("surf_Matches",img_matches);//显示的标题为Matches    waitKey(0);    return 0;}

然后报错

/home/pikachu/src/c++/test/study_opencv/cmake-build-debug/study_opencvopen rightOpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvShowImage, file /home/pikachu/dev/opencv-2.4.9/modules/highgui/src/window.cpp, line 501terminate called after throwing an instance of 'cv::Exception'  what():  /home/pikachu/dev/opencv-2.4.9/modules/highgui/src/window.cpp:501: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImageProcess finished with exit code 6

安装依赖

sudo apt install libgtk2.0-devsudo apt install pkg-config

重新编译
然后就可以了