简体   繁体   中英

Opencv linking CMAKE not working Ubuntu 16.04 Docker

I am trying to compile a project inside a Docker file but it keeps throwing errors.

Dockerfile:

FROM jjanzic/docker-python3-opencv:contrib-opencv-3.4.2

RUN apt-get update -y
RUN apt-get install -y g++ cmake libboost-dev libgoogle-perftools-dev

COPY . /opt/nsg

WORKDIR /opt/nsg

RUN mkdir -p build && cd build && \
    cmake -DCMAKE_BUILD_TYPE=Release .. && \
    make -j $(nproc)

CMakeLists.txt in root directory:

cmake_minimum_required(VERSION 2.8)

project(efanna2e)
include_directories(${PROJECT_SOURCE_DIR}/include)
find_package(OpenCV REQUIRED)
#OpenMP
find_package(OpenMP)
if (OPENMP_FOUND)
    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
else()
    message(FATAL_ERROR "no OpenMP supprot")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free")
add_definitions (-std=c++14 -O3 -lboost -march=native -Wall -DINFO)

add_subdirectory(src)
add_subdirectory(tests)

CMakeLists.txt in /tests directory:

set(CMAKE_CXX_STANDARD 14)

add_executable(test_nsg_index test_nsg_index.cpp)
target_link_libraries(test_nsg_index ${PROJECT_NAME} -ltcmalloc)

add_executable(test_nsg_search test_nsg_search.cpp)
target_link_libraries(test_nsg_search ${PROJECT_NAME})

add_executable(test_nsg_optimized_search test_nsg_optimized_search.cpp)
target_link_libraries(test_nsg_optimized_search ${OpenCV_LIBS})
target_link_libraries(test_nsg_optimized_search ${PROJECT_NAME} -ltcmalloc)

Code I want to run:

auto body = req.body.substr(file.offset, file.length);
const cv::Mat image = cv::imdecode(body, 0);

After running docker build . this is part of the output:

-- Found OpenCV: /usr/local (found version "3.4.2") 

/opt/nsg/tests/test_nsg_optimized_search.cpp:149:4:   required from here
/opt/nsg/tests/test_nsg_optimized_search.cpp:145:39: error: no matching function for call to ‘imdecode(std::__cxx11::basic_string<char>&, int)’
     const cv::Mat image = cv::imdecode(body, 0);
                           ~~~~~~~~~~~~^~~~~~~~~
In file included from /usr/local/include/opencv2/imgcodecs/imgcodecs.hpp:48:0,
                 from /opt/nsg/tests/test_nsg_optimized_search.cpp:12:
/usr/local/include/opencv2/imgcodecs.hpp:222:18: note: candidate: cv::Mat cv::imdecode(cv::InputArray, int)
 CV_EXPORTS_W Mat imdecode( InputArray buf, int flags );
                  ^~~~~~~~

So it keeps saying that there is no function imdecode . I also tried imread and multiple other functions. It just keeps saying it is not there, and on the next line it says that it found the function I want and points to it.

I have tried multiple things in my code:

  1. I loaded #include <opencv2/core/core.hpp>
  2. Then I loaded #include "opencv2/core/core.hpp" (did that for all included hpp files from opencv
  3. used: using namespace cv;
  4. used the function without the namespace
  5. did: cv::imdecode()
  6. did: imdecode()

These resulted in the same error. 导致了相同的错误。

Yesterday I was on an Ubuntu 18.04 machine with the same exact problem, except that I installed opencv on the machine and not via Docker. Today, I reinstalled Ubuntu to version 16.04 and started using Docker. But again same problem. I think I am doing something wrong here but don't know what.

The error is telling you that the function call:

const cv::Mat image = cv::imdecode(body, 0);

doesn't match what is defined in the header file. The compiler only knows how to use calls matching the function signatures (ie function name, function argument types, return type, etc) that are defined, in your case in imgcodecs.hpp :

cv::Mat cv::imdecode(cv::InputArray, int)

The compiler calls this a candidate match, because while the function name you used, imdecode , is correct, the function argument types do not match. Your function call has this signature (per the error message), showing the first argument is different than what the compiler expects:

cv::Mat imdecode(std::__cxx11::basic_string<char>&, int)

So you have to change the variable body to be a cv::InputArray type, or a type from which InputArray can be constructed. The list of possibilities is here . Considering a cv::InputArray can be constructed from a std::vector<T> type, you may try converting body into a vector of char with something like this:

auto body = req.body.substr(file.offset, file.length);
std::vector<char> bodyVector(body.begin(), body.end());
const cv::Mat image = cv::imdecode(bodyVector, 0);

Or you can explicitly create a cv::InputArray variable from body and pass that to the imdecode function instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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