How do you compile OpenCV with cmake? - opencv

I'm trying to compile OpenCV so that it has the the sift module.
I downloaded CMake using this tutorial.
I also downloaded and unzipped opencv-4.3.0 and opencv_contrib.
I made an empty folder, opened the terminal, and navigated to it using the following line:
cd ~/opencvMaster/build
Then, while I was in this empty folder in the terminal, I ran the following line:
cmake -DOPENCV_ENABLE_NONFREE:BOOL=ON -DOPENCV_EXTRA_MODULES_PATH= ~/openCVMaster/opencv_contrib-master/modules ~/openCVMaster/opencv-4.3.0
It configured and generated the files. I then input make. Got the following error:
[ 82%] Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.o
~/opencvMaster/opencv_contrib-e6f32c6a69043456a806a4e802ee3ce7b7059c93/modules/xfeatures2d/src/boostdesc.cpp:654:20: fatal error: boostdesc_bgm.i: No such file or directory
#include "boostdesc_bgm.i"
^~~~~~~~~~~~~~~~~
compilation terminated.
I followed the suggestion found here, and added the following files:
cd ~/openCVMaster/opencv_contrib-4.3.0/modules/xfeatures2d/src
curl wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_lbgm.i >boostdesc_lbgm.i
curl wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_256.i >boostdesc_binboost_256.i
curl wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_128.i >boostdesc_binboost_128.i
curl wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i >boostdesc_binboost_064.i
curl wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i >boostdesc_bgm_hd.i
curl wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i >boostdesc_bgm_bi.i
curl wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i >boostdesc_bgm.i
curl wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i > vgg_generated_120.i
curl wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i >vgg_generated_64.i
curl wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_48.i >vgg_generated_48.i
curl wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i >vgg_generated_80.i
This was a step forward: no more errors relating to vgg or boostdesc. A new error appeared: opencv_contrib-master/modules/xfeatures2d/test/test_features2d.cpp:51:10: fatal error: features2d/test/test_detectors_regression.impl.hpp: No such file or directory
Following the advice of ah-ma here) I downloaded https://github.com/opencv/opencv/tree/master/modules. Once downloaded, I located its subfolder 'features2d' and put it in opencv_contrib-master/modules/xfeatures2d/test/.
Still got an error -
~/openCVMaster/opencv_contrib-master/modules/xfeatures2d/test/features2d/misc/java/src/cpp/features2d_converters.cpp:2:10: fatal error: common.h: No such file or directory
#include "common.h"
I added the following:
cd ~/openCVMaster/opencv_contrib-master/modules/xfeatures2d/test/features2d/misc/java/src/cpp
curl wget https://raw.githubusercontent.com/ray-project/common/master/common.h > common.h
I ran make again, and got the following errors:
~/openCVMaster/opencv_contrib-master/modules/xfeatures2d/test/features2d/misc/java/src/cpp/features2d_converters.cpp: In function ‘void Mat_to_vector_KeyPoint(cv::Mat&, std::vector<cv::KeyPoint>&)’:
~/openCVMaster/opencv_contrib-master/modules/xfeatures2d/test/features2d/misc/java/src/cpp/features2d_converters.cpp:7:38: error: ‘LOGD’ was not declared in this scope
#define CHECK_MAT(cond) if(!(cond)){ LOGD("FAILED: " #cond); return; }
~/openCVMaster/opencv_contrib-master/modules/xfeatures2d/test/features2d/misc/java/src/cpp/features2d_converters.cpp:14:5: note: in expansion of macro ‘CHECK_MAT’
CHECK_MAT(mat.type()==CV_32FC(7) && mat.cols==1);
^~~~~~~~~
~/openCVMaster/opencv_contrib-master/modules/xfeatures2d/test/features2d/misc/java/src/cpp/features2d_converters.cpp: In function ‘void Mat_to_vector_DMatch(cv::Mat&, std::vector<cv::DMatch>&)’:
~/openCVMaster/opencv_contrib-master/modules/xfeatures2d/test/features2d/misc/java/src/cpp/features2d_converters.cpp:7:38: error: ‘LOGD’ was not declared in this scope
#define CHECK_MAT(cond) if(!(cond)){ LOGD("FAILED: " #cond); return; }
Any suggestions welcomed.

# Step 1: Install VS 2017 for cmake
# Step 2: Install CMake
Download and install CMake v3.10.0 from https://cmake.org/download/.
During installation select “Add CMake to system PATH”
# step 3: install Anaconda
# step 4: Download and extract opencv-3.3.1 and opencv_contrib-3.3.1
Go to https://github.com/opencv/opencv/releases and download opencv-3.3.1 source code zip
Go to https://github.com/opencv/opencv_contrib/releases and download opencv_contrib-3.3.1 source code zip
Extract both zip files. Although you can keep opencv and opencv_contrib folders anywhere, I suggest that you should keep both in the same directory. I have placed these two folders in “My Documents” directory.
NOTE : From hereon we will refer the path to opencv-3.3.1 folder as OPENCV_PATH. For example, in my case OPENCV_PATH is C:/Users/Vaibhaw Chandel/Documents/opencv-3.3.1
Depending upon where you have kept opencv-3.3.1 folder, this path would be different.
Step 5: Generate Visual Studio project using CMake
Run Cmake, in box “Where is the source code” write value of OPENCV_PATH (which is path to opencv-3.3.1 folder) and path to build directory. We will choose build directory as OPENCV_PATH/build
Now click configure.
You will be asked for permission to create the build folder. Click Yes
When prompted to select a compiler, select Visual Studio 14 2015 Win64.
Click finish and in the next window keep the default parameters checked.
Click finish. Now CMake will look in the system directories and generate the makefiles.
Step 5.1: Additional changes to CMake config
We will make few changes in the default configuration generated by CMake. Click on the images to see a larger version.
Check “INSTALL_C_EXAMPLES” and “INSTALL_PYTHON_EXAMPLES”
In flag “OPENCV_EXTRA_MODULES_PATH”, give path of modules directory within opencv_contrib-3.3.1. In our case we have kept opencv_contrib-3.3.1 in Documents folder so path is “C:/Users/Vaibhaw Chandel/Documents/opencv_contrib-3.3.1/modules”
Now click configure to apply these changes.
On Windows 10, opencv_saliency module fails to build. So we will disable it. Uncheck BUILD_opencv_saliency
Now click on configure again to include this change.
Step 5.2 : Add Python paths for both Python2 and Python3 (optional)
This section is only for people who want to generate OpenCV binary for both Python2 and Python 3. If you are going to use just one Python either 2 or 3, you should skip this section.
CMake was unable to find paths for my Python3 files.
So I manually added paths for Python3
Now click configure again. After configuring is done, search opencv_python in search bar, both BUILD_opencv_python2 and BUILD_opencv_python3 will be automatically checked. Now we are sure that OpenCV binaries for both Python2 and Python 3 will be generated after compilation.
Step 5.3 : Generate build files
If CMake is able to configure without any errors it should say “Configuring done”.
Click generate.
Note: Whenever you make any changes(check/uncheck boxes or change path) to configuration generated by CMake, always click configure and generate.
Step 6: Compile OpenCV
Step 6.1:Compile opencv in Release mode
Open Windows Command Prompt (cmd).
Go to OPENCV_PATH/build directory and run this command
1
cmake.exe --build . --config Release --target INSTALL
Step 6.2 : Compile opencv in Debug mode
Open CMake GUI again as mentioned in Step 5.
Search “python” in search box
Uncheck INSTALL_PYTHON_EXAMPLES, BUILD_opencv_python3 and BUILD_opencv_python2
Click configure
Click generate
Now in windows command prompt
Go to OPENCV_PATH/build directory and run this command
1
cmake.exe --build . --config Debug --target INSTALL
Now that we have compiled OpenCV we will find out how to test a OpenCV project using CMake.
Step 7: Update System Environment Variables
Step 7.1 : Update environment variable – PATH
Step 7.2 : Update user environment variable – OPENCV_DIR

Download opencv-4.3.0.zip and opencv_contrib-4.3.0.zip. Unzip them.
Open opencv_contrib-4.3.0\modules. Copy the folder that says xfeatures2d. Open opencv-4.3.0\modules, and paste the folder just copied (xfeatures2d)
Make an empty folder, and call it 'Build45.'
Open cmake-gui.
In the box that says, 'Where to build the binaries' put the file path representing your empty folder (Build45)
In the box that says, 'Where is the source code' put the file path representing opencv-4.3.0.
Click configure. Choices will appear.
Input 'free' into the search box and check the box that says OPENCV_ENABLE_NONFREE.
Input xfeatures2d and check the box that says 'Build_opencv_xfeatures2d`
Click configure again. (Doublecheck you've done this right: there is a second box in cmake gui that gives build details. You should see Non-free algorithms: YES.
Click Generate.
Check that the build45 folder is now filled with stuff.
Open your terminal and navigate to the build45 folder (cd ~/opencv-master/build45)
Type cmake .
Type make
NOTE:* It will take a couple hours to compile. Make yourself a cup of tea*.
Type sudo make install
Check that it worked: in your terminal, type python3
Type import cv2
Type sift = cv2.xfeatures2d.SIFT_create()
You should get no errors. If you do get errors, the following troubleshooting tools may be helpful:
import cv2
print cv2.getBuildInformation()
import cv2
funcs = dir(cv2)
for f in funcs:
... print(f)
Also, if you are worried about infringing patents, check here for discussion: https://www.reddit.com/r/computervision/comments/ba1l8g/according_to_this_the_patent_on_sift_was_due_to/

Related

Using ZMQ on iOS

I am trying to build an iOS app that is exposed to a fairly extensive Rust project, but I am having issues with one of the dependencies in the project. The Rust project has a ZMQ dependency, and while I am able to build the project on my Mac, when I try to incorporate the Rust project I get the following error:
error: failed to run custom build command for `zmq-sys v0.8.3`
Caused by:
process didn't exit successfully: `/Users/lisa/company/project/project_detail/target/debug/build/zmq-sys-16dfc88dff84d855/build-script-build` (exit status: 101)
--- stdout
cargo:rerun-if-env-changed=LIBZMQ_NO_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_aarch64-apple-ios
cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_aarch64_apple_ios
cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
cargo:rerun-if-env-changed=PKG_CONFIG_aarch64-apple-ios
cargo:rerun-if-env-changed=PKG_CONFIG_aarch64_apple_ios
cargo:rerun-if-env-changed=TARGET_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64-apple-ios
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64_apple_ios
cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
--- stderr
thread 'main' panicked at 'Unable to locate libzmq:
pkg-config has not been configured to support cross-compilation.
Install a sysroot for the target platform and configure it via
PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a
cross-compiling wrapper for pkg-config and set it via
PKG_CONFIG environment variable.', /Users/lisa/.cargo/registry/src/github.com-1ecc6299db9ec823/zmq-sys-0.8.3/build.rs:31:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
I am trying to build the appropriate library for the iOS and iOS simulator architectures without relying on pkg-config, but if someone can provide useful instructions for configuring pkg-config for cross-compilation, I am not opposed to it, I just don't know how and can't find any up to date resources.
I have cloned the libzmq project from this github: https://github.com/zeromq/libzmq.git, then navigate into libzmq.
Inside libzmq, I run ./autogen.sh followed by ./configure --prefix [directory location] --target "aarch64-apple-ios", then make and make install.
The above procedure without the --target flag is how I got the project to compile on my Mac in the first place, and with the target flag in place make and make install both complete successfully, but when I try to build the project using PROJECT_DIR="${PWD}/ProjectName" ./ProjectName/build-rust.sh,
I get the previously posted error.
I am following the Swift-Bridge Book (https://chinedufn.github.io/swift-bridge/building/xcode-and-cargo/index.html) to try to load the Rust libraries into Xcode.
One thing to note: In the libzmq git hub, there is a folder called builds/ios/ and inside this folder is what looks to be a build file for all of the iOS archs, but I haven't been able to get the repo to build with that file. I think figuring out how to use the build_ios.sh build file as opposed to the standard build.sh provided might be the answer, but I've tried dropping build_ios.sh where build.sh is, and ./autogen.sh doesn't work, because it complains that it can't find configure.ac, even though this file does exist.

OpenCV build error: does not appear to contain CMakeLists.txt

I am following this guide to build an optimized version of OpenCV for android:
https://medium.com/#tomdeore/opencv-on-android-tiny-with-optimization-enabled-932460acfe38
Step 4:
I downloaded OpenCV from Github (https://github.com/opencv/opencv)
I run:
fabio#fabio-XPS-15-9560:~/Software/opencv-3.4.9$ mkdir build
fabio#fabio-XPS-15-9560:~/Software/opencv-3.4.9$ cd build/
fabio#fabio-XPS-15-9560:~/Software/opencv-3.4.9/build$ cmake -DCMAKE_TOOLCHAIN_FILE=../platforms/android/android.toolchain.cmake -DANDROID_STL=gnustl_shared -DANDROID_NATIVE_API_LEVEL=23
But I got this error:
CMake Error: The source directory "/home/fabio/Software/opencv-3.4.9/build" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
What am I doing wrong?
Note, in your linked build guide, the cmake command ends with a .. telling CMake to run on the parent directory:
cmake \
-DCMAKE_TOOLCHAIN_FILE=../platforms/android/android.toolchain.cmake\
-DANDROID_STL=gnustl_shared \
-DANDROID_NATIVE_API_LEVEL=23 ..
You are running CMake from the build directory, which is incorrect. You want to run on the parent directory, which does contain a CMakeLIsts.txt file.

install MongoDB C++ Driver problem in ubuntu 16.04

I want to install MongoDB C++ Driver, so first is mongocxx
I follow this installation:
http://mongocxx.org/mongocxx-v3/installation/
but I can not pass step 4
when I run this in mongo-cxx-driver/build
sudo cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
it shows
-- Auto-configuring bsoncxx to use MNMLSTC for polyfills since C++17 is inactive
CMake Error at src/mongocxx/CMakeLists.txt:37 (find_package):
By not providing "Findlibmongoc-1.0.cmake" in CMAKE_MODULE_PATH this
project has asked CMake to find a package configuration file provided by
"libmongoc-1.0", but CMake did not find one.
Could not find a package configuration file provided by "libmongoc-1.0"
(requested version 1.13.0) with any of the following names:
[![enter image description here][1]][1]
libmongoc-1.0Config.cmake
libmongoc-1.0-config.cmake
Add the installation prefix of "libmongoc-1.0" to CMAKE_PREFIX_PATH or set
"libmongoc-1.0_DIR" to a directory containing one of the above files. If
"libmongoc-1.0" provides a separate development package or SDK, be sure it
has been installed.
second question,
Step 2: Choose a C++17 polyfill how can I set MNMLSTC/core?
does anyone can help me,I already trap here for a long time ?
my env:
mongo-c-driver 1.15.1
libmongoc-1.0
mongocxx-3.4.x
Cmake is complaining about not finding a package configuration file (xxx.cmake), probably because you didn't build libmongoc/libbson.
I've tried to reproduce your issue and hit the same problem when I only installed them (apt-get install), so my suggestion is that you get the sources and build them as described at: http://mongoc.org/libmongoc/current/installing.html
Here's the list of commands (with the latest version of mongo-c-driver=1.15.1) which I just tried and worked fine:
wget https://github.com/mongodb/mongo-c-driver/releases/download/1.15.1/mongo-c-driver-1.15.1.tar.gz
tar xzf mongo-c-driver-1.15.1.tar.gz
cd mongo-c-driver-1.15.1
mkdir cmake-build
cd cmake-build
cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF ..
make
sudo make install
At this point you can go back into mongocxx/build and run again the command you were stuck at:
cd ../../mongo-cxx-driver/build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..

OpenCV installation help?

I am on Mac OS X Snow Leopard, and I am trying to get OpenCV to work with CodeBlocks. I downloaded the source files for OpenCV 2.3, and then downloaded the latest version of Cmake. The direct source file contains a folder "include" which has two folders in it labeled OpenCV and OpenCV 2. However after I use Cmake on the source files, the two "OpenCV" and OpenCV 2" folders are gone. Anyone know why this is happening and how to fix it? Instead of the folders all I have now is a CmakeFiles Folder, MakeFile and cmake_install.cmake
Thank You
After you download the source files, go into the opencv folder that's inside the folder you created and execute these commands:
sudo cmake -G "Unix Makefiles" .
sudo make -j8
sudo make install
Don't try to do this from the include folder. That's one level too deep. You want to do it from the folder that contains the include folder.
Terminal is in Applications/Utilities. That's how you get to the Unix command line. To get into your opencv folder, run Terminal and type "cd ". (That's cd followed by a space.) Then drag your opencv folder from a Finder window onto the Terminal window. This will append the folder name to the command line. Then press Return and you'll be in that folder. Then you can enter the commands above.
If you want to use ccmake instead of cmake, just replace "cmake" with "ccmake" in the first command.

CMake can't find FFMPEG in custom install path

I am compiling a dependency for a project on Ubuntu 10.10, and instead of having it install to /usr/local by default, I am instead installing it to /tmp/stage/usr/local. How do I go about informing CMake of the location of this custom installed dependency when I call it to generate the build files for said project.
I am running CMake 2.8.1, and I have tried to set CMAKE_PREFIX_PATH on the cmake command line, like so
cmake -D CMAKE_PREFIX_PATH=/tmp/stage/usr/local
but this doesn't seem to make a difference - the project doesn't seem to detect the dependency.
Also, if it matters, the project in question is OpenCV 2.2, and the dependency in question is FFMPEG...
I figured out how to fix my problem, and trying to point CMake at the appropriate install location isn't the issue.
Apparently, CMake is unable to find the pkg-config files for FFMPEG (i.e. libavcodec.pc, libavdevice.pc, etc.) that tell it where the FFMPEG headers and libraries are located. In a typical install scenario, these files would be located at /usr/lib/pkgconfig. However because of the custom install location, they are instead located at /tmp/stage/usr/local/lib/pkgconfig.
So that CMake could find these files, I had to add the following environment variable:
export PKG_CONFIG_PATH=/tmp/stage/usr/local/lib/pkgconfig
After which point, OpenCV built against FFMPEG as expected.

Resources