简体   繁体   English

opencv2可以使用cmake进行构建,但不能使用CLion的“构建”功能进行构建吗?

[英]opencv2 can build using cmake but not in CLion “build” functionality?

I'm very confused why I can use cmake in commandline to build a C++ program, but cannot build the program in CLion with its "build" functionality. 我很困惑为什么我可以在命令行中使用cmake来构建C ++程序,却不能使用其“构建”功能在CLion中构建该程序。

This is my CMakeLists.txt file. 这是我的CMakeLists.txt文件。 I am trying to build a simple image classification program with OpenCV, the tutorial is here . 我正在尝试使用OpenCV构建一个简单的图像分类程序,该教程位于此处

cmake_minimum_required(VERSION 3.5.1)
project(opencvdnn )

set(CMAKE_CXX_STANDARD 14)

find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

add_executable(caffe caffe.cpp)
target_link_libraries(caffe ${OpenCV_LIBS} )

case 1: 情况1:

I build directly in the command line successfully with the following commands and results: 我直接使用以下命令和结果在命令行中成功构建:

user:/DATA/opencvdnn/build$ cmake ../CMakeLists.txt

-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /media/yuqiong/DATA/opencvdnn

Case 2 情况二

I use the build functionality in my CLion IDE, which failed, claiming it cannot find a certain library. 我在我的CLion IDE中使用了build功能,但失败了,声称无法找到特定的库。

/opt/clion-2018.2.5/bin/cmake/linux/bin/cmake --build /media/yuqiong/DATA/opencvdnn --target caffe -- -j 6
Scanning dependencies of target caffe
[ 50%] Building CXX object CMakeFiles/caffe.dir/caffe.cpp.o
/media/yuqiong/DATA/opencvdnn/caffe.cpp:5:27: fatal error: opencv2/dnn.hpp: No such file or directory
compilation terminated.
CMakeFiles/caffe.dir/build.make:62: recipe for target 'CMakeFiles/caffe.dir/caffe.cpp.o' failed
make[3]: *** [CMakeFiles/caffe.dir/caffe.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/caffe.dir/all' failed
make[2]: *** [CMakeFiles/caffe.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/caffe.dir/rule' failed
make[1]: *** [CMakeFiles/caffe.dir/rule] Error 2
Makefile:118: recipe for target 'caffe' failed
make: *** [caffe] Error 2

My question is why would that be? 我的问题是为什么会这样? Is CLion not using the same CMake with the cmake tool? CLion是否与cmake工具未使用同一CMake? Thanks! 谢谢!

If helps, here is my directory structure: 如果有帮助,这是我的目录结构:

├── build
├── bvlc_googlenet.caffemodel
├── bvlc_googlenet.prototxt
├── caffe.cpp
├── cmake-build-debug
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   │   ├── 3.12.3
│   │   │   ├── CMakeCCompiler.cmake
│   │   │   ├── CMakeCXXCompiler.cmake
│   │   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   │   ├── CMakeSystem.cmake
│   │   │   ├── CompilerIdC
│   │   │   │   ├── a.out
│   │   │   │   ├── CMakeCCompilerId.c
│   │   │   │   └── tmp
│   │   │   ├── CompilerIdCUDA
│   │   │   │   ├── CMakeCUDACompilerId.cu
│   │   │   │   └── tmp
│   │   │   └── CompilerIdCXX
│   │   │       ├── a.out
│   │   │       ├── CMakeCXXCompilerId.cpp
│   │   │       └── tmp
│   │   ├── caffe.dir
│   │   │   ├── build.make
│   │   │   ├── cmake_clean.cmake
│   │   │   ├── CXX.includecache
│   │   │   ├── DependInfo.cmake
│   │   │   ├── depend.internal
│   │   │   ├── depend.make
│   │   │   ├── flags.make
│   │   │   ├── link.txt
│   │   │   └── progress.make
│   │   ├── clion-environment.txt
│   │   ├── clion-log.txt
│   │   ├── cmake.check_cache
│   │   ├── CMakeDirectoryInformation.cmake
│   │   ├── CMakeError.log
│   │   ├── CMakeOutput.log
│   │   ├── CMakeTmp
│   │   ├── feature_tests.bin
│   │   ├── feature_tests.c
│   │   ├── feature_tests.cxx
│   │   ├── Makefile2
│   │   ├── Makefile.cmake
│   │   ├── opencvdnn.dir
│   │   │   ├── build.make
│   │   │   ├── cmake_clean.cmake
│   │   │   ├── DependInfo.cmake
│   │   │   ├── depend.make
│   │   │   ├── flags.make
│   │   │   ├── link.txt
│   │   │   └── progress.make
│   │   ├── Progress
│   │   │   ├── 1
│   │   │   └── count.txt
│   │   ├── progress.marks
│   │   └── TargetDirectories.txt
│   ├── cmake_install.cmake
│   ├── Makefile
│   └── opencvdnn.cbp
├── CMakeCache.txt
├── CMakeFiles
│   ├── 3.12.3
│   │   ├── CMakeCCompiler.cmake
│   │   ├── CMakeCXXCompiler.cmake
│   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   ├── CMakeSystem.cmake
│   │   ├── CompilerIdC
│   │   │   ├── a.out
│   │   │   ├── CMakeCCompilerId.c
│   │   │   └── tmp
│   │   └── CompilerIdCXX
│   │       ├── a.out
│   │       ├── CMakeCXXCompilerId.cpp
│   │       └── tmp
│   ├── 3.5.1
│   │   ├── CMakeCCompiler.cmake
│   │   ├── CMakeCXXCompiler.cmake
│   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   ├── CMakeSystem.cmake
│   │   ├── CompilerIdC
│   │   │   ├── a.out
│   │   │   └── CMakeCCompilerId.c
│   │   └── CompilerIdCXX
│   │       ├── a.out
│   │       └── CMakeCXXCompilerId.cpp
│   ├── caffe.dir
│   │   ├── build.make
│   │   ├── cmake_clean.cmake
│   │   ├── DependInfo.cmake
│   │   ├── depend.make
│   │   ├── flags.make
│   │   ├── link.txt
│   │   └── progress.make
│   ├── clion-environment.txt
│   ├── cmake.check_cache
│   ├── CMakeDirectoryInformation.cmake
│   ├── CMakeOutput.log
│   ├── CMakeTmp
│   ├── feature_tests.bin
│   ├── feature_tests.c
│   ├── feature_tests.cxx
│   ├── Makefile2
│   ├── Makefile.cmake
│   ├── progress.marks
│   └── TargetDirectories.txt
├── cmake_install.cmake
├── CMakeLists.txt
├── Makefile
├── opencvdnn.cbp
├── space_shuttle.jpg
└── synset_words.txt

This is the source code, it basically just reads a caffe model and use it on an image. 这是源代码,它基本上只是读取caffe模型并在图像上使用它。

#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core/utils/trace.hpp>
using namespace cv;
using namespace cv::dnn;
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
/* Find best class for the blob (i. e. class with maximal probability) */
static void getMaxClass(const Mat &probBlob, int *classId, double *classProb)
{
    Mat probMat = probBlob.reshape(1, 1); //reshape the blob to 2x1000 matrix
    Point classNumber;
    minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);
    *classId = classNumber.x;
}
static std::vector<String> readClassNames(const char *filename = "synset_words.txt")
{
    std::vector<String> classNames;
    std::ifstream fp(filename);
    if (!fp.is_open())
    {
        std::cerr << "File with classes labels not found: " << filename << std::endl;
        exit(-1);
    }
    std::string name;
    while (!fp.eof())
    {
        std::getline(fp, name);
        if (name.length())
            classNames.push_back( name.substr(name.find(' ')+1) );
    }
    fp.close();
    return classNames;
}
int main(int argc, char **argv)
{
    CV_TRACE_FUNCTION();
    String modelTxt = "bvlc_googlenet.prototxt";
    String modelBin = "bvlc_googlenet.caffemodel";
    String imageFile = (argc > 1) ? argv[1] : "space_shuttle.jpg";
    Net net;
    try {
        net = dnn::readNetFromCaffe(modelTxt, modelBin);
    }
    catch (cv::Exception& e) {
        std::cerr << "Exception: " << e.what() << std::endl;
        if (net.empty())
        {
            std::cerr << "Can't load network by using the following files: " << std::endl;
            std::cerr << "prototxt:   " << modelTxt << std::endl;
            std::cerr << "caffemodel: " << modelBin << std::endl;
            std::cerr << "bvlc_googlenet.caffemodel can be downloaded here:" << std::endl;
            std::cerr << "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel" << std::endl;
            exit(-1);
        }
    }
    Mat img = imread(imageFile);
    if (img.empty())
    {
        std::cerr << "Can't read image from the file: " << imageFile << std::endl;
        exit(-1);
    }
    //GoogLeNet accepts only 224x224 BGR-images
    Mat inputBlob = blobFromImage(img, 1.0f, Size(224, 224),
                                  Scalar(104, 117, 123), false);   //Convert Mat to batch of images
    Mat prob;
    cv::TickMeter t;
    for (int i = 0; i < 10; i++)
    {
        CV_TRACE_REGION("forward");
        net.setInput(inputBlob, "data");        //set the network input
        t.start();
        prob = net.forward("prob");                          //compute output
        t.stop();
    }
    int classId;
    double classProb;
    getMaxClass(prob, &classId, &classProb);//find the best class
    std::vector<String> classNames = readClassNames();
    std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;
    std::cout << "Probability: " << classProb * 100 << "%" << std::endl;
    std::cout << "Time: " << (double)t.getTimeMilli() / t.getCounter() << " ms (average from " << t.getCounter() << " iterations)" << std::endl;
    return 0;
} //main

I am afraid your first approach using command line did not work. 恐怕您使用命令行的第一种方法无效。 cmake will only ever create make files. cmake只会创建make文件。 Its the make command that actually builds the target. 实际上是建立目标的make命令。 Your clion output suggests that clion was able to successfully generate the make files, but was unable to build. 您的clion输出表明clion能够成功生成make文件,但是无法生成。 I would expect identical error if you tried to do a make manually via command line. 如果您尝试通过命令行手动进行制作,则可能会遇到相同的错误。

If you can build camke project via command line, it should 100% of the time build in clion as well. 如果您可以通过命令行构建camke项目,那么也应该100%地将其构建在clion中。

I would also recommend checking if you have opencv2 installed on your system. 我还建议您检查系统上是否安装了opencv2。 Check if you have a folder /usr/include/opencv or if it is /usr/include/opencv2. 检查您是否有一个文件夹/ usr / include / opencv或它是/ usr / include / opencv2。 Since your find_package(OpenCV REQUIRED) succeeded, it is certain that you have a local installation. 由于您的find_package(需要OpenCV)成功,因此可以确定您具有本地安装。

Another way is to look for the file CMakeCache.txt. 另一种方法是查找文件CMakeCache.txt。 If you search for opencv in it, you will find the some OpenCV vars set in it. 如果在其中搜索opencv,则会在其中找到一些OpenCV变量集。 That should give you a clue. 那应该给你一个线索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM