I am trying to include Opencv to my native C code in an android studio project through Cmake. I did some research online and downloaded the FindOpenCV.cmake file from online and added it to the app directory of my android project. This is also where the CMakeLists.txt is located. I imported OpenCV onto my Android Studio project as a module using this tutorial: https://www.learn2crack.com/2016/03/setup-opencv-sdk-android-studio.html, and when I run:
if(!OpenCVLoader.initDebug()){
System.out.println("Opencv not loaded");
} else {
System.out.println("Opencv loaded");
}
I get that Opencv is loaded.
However, since I'm trying to add OpenCV to my native code, and not the Java code, I don't think I can use this. Here is the CMakeLists I have right now:
# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} FindOpenCV.cmake)
# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.
add_library(# Specifies the name of the library.
apriltag
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/apriltag/apriltag.c
src/main/apriltag/apriltag_jni.c
src/main/apriltag/apriltag_quad_thresh.c
src/main/apriltag/common/g2d.c
src/main/apriltag/common/getopt.c
src/main/apriltag/common/homography.c
src/main/apriltag/common/image_f32.c
src/main/apriltag/common/image_u8.c
src/main/apriltag/common/image_u8x3.c
src/main/apriltag/common/matd.c
src/main/apriltag/common/pnm.c
src/main/apriltag/common/string_util.c
src/main/apriltag/common/svd22.c
src/main/apriltag/common/time_util.c
src/main/apriltag/common/unionfind.c
src/main/apriltag/common/workerpool.c
src/main/apriltag/common/zarray.c
src/main/apriltag/common/zhash.c
src/main/apriltag/common/zmaxheap.c
src/main/apriltag/tag16h5.c
src/main/apriltag/tag25h7.c
src/main/apriltag/tag25h9.c
src/main/apriltag/tag36artoolkit.c
src/main/apriltag/tag36h10.c
src/main/apriltag/tag36h11.c
)
STRING(REPLACE "-O0" "-O4" CMAKE_C_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})
STRING(REPLACE "-O2" "-O4" CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
include_directories(src/main/apriltag/)
include_directories(${OpenCV_INCLUDE_DIRS})
find_package(OpenCV REQUIRED)
find_library(log-lib log)
find_library(jnigraphics-lib jnigraphics)
target_link_libraries(apriltag ${log-lib} ${jnigraphics-lib})
Here are the errors I'm getting while building the gradle:
By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "OpenCV", but
CMake did not find one.
Could not find a package configuration file provided by "OpenCV" with any of
the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed.
So my questions are:
- Can I use the imported OpenCV or do I have to download a different opencv and store it somewhere else?
- What do I have to change in my CMakeLists.txt for my gradle to build?
Ideally, I want to build and be able to add #include <opencv2/opencv.hpp>
and using namespace cv
to my c file and add functions that use opencv functions.