JackCarmichael
JackCarmichael

Reputation: 45

Installing opencv From Command Line (Windows)

I am trying to use opencv in a project, and am running into problems 'installing' it. I have extracted the opencv files and have created a small test program:

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

int main(int argc, char **argv){
    cv::Mat im=cv::imread((argc==2)? argv[1]: "testing.jpg",1);
    if (im.empty()){
        std::cout << "Cannot open image." << std::endl;
    } else {
        cv::imshow("image",im);
        cv::waitKey(0);
    }
    return 0;
}

To compile the program I have used the command below:

g++ -I"../../PortableGit/opt/opencv/build/include/" -L"../../PortableGit/opt/opencv/build/x64/vc15/lib" main.cpp -lopencv_core -lopencv_highgui -o main

I get the errors below:

In file included from ../../PortableGit/opt/opencv/build/include/opencv2/core.hpp:3293:0,
                 from ../../PortableGit/opt/opencv/build/include/opencv2/highgui.hpp:46,
                 from ../../PortableGit/opt/opencv/build/include/opencv2/highgui/highgui.hpp:48,
                 from main.cpp:1:
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:714:14: error: 'recursive_mutex' in namespace 'std' does not name
 a type
 typedef std::recursive_mutex Mutex;
              ^~~~~~~~~~~~~~~
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:25: error: 'Mutex' is not a member of 'cv'
 typedef std::lock_guard<cv::Mutex> AutoLock;
                         ^~
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:25: error: 'Mutex' is not a member of 'cv'
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:34: error: template argument 1 is invalid
 typedef std::lock_guard<cv::Mutex> AutoLock;

I believe that it has something to do with mingw binaries no longer being included with opencv. I am missing the opencv/build/x86/mingw directory.

My questions are:

Any help is appreciated.

Edit:

This appears to be a problem with GCC's implementation of threads on windows. Using mingw-w64 instead of mingw fixed the std::recursive_mutex issue, but now the linker cannot find the proper files.

/i686-w64-mingw32/bin/ld.exe: cannot find -lopencv_core
/i686-w64-mingw32/bin/ld.exe: cannot find -lopencv_highgui

Upvotes: 0

Views: 2752

Answers (2)

Feliks
Feliks

Reputation: 65

Correction for JackCamichael's answer those 2 commands won't work in Windows

export CC=/PortableGit/MinGW-w64/mingw32/bin/gcc.exe
export CXX=/PortableGit/MinGW-w64/mingw32/bin/g++.exe

This should be

setx -m CC C:\msys64\mingw64\bin\gcc.exe
setx -m CXX C:\msys64\mingw64\bin\g++.exe 

C:\msys64\mingw64\bin is mingw64 path on my machine

Upvotes: 0

JackCarmichael
JackCarmichael

Reputation: 45

After quite a bit of trying things out, this is what I got to work. Oddly, following the LINUX guide to install opencv worked better than the WINDOWS guide, even though I have a windows computer.

Guide to Installing OpenCV on Windows Without VS

Heads-up: This is a multi-step process, 3 separate tools are required. Be prepared for this to take a while.

Part 1: Get everything ready

  • Download MinGW-w64.
    • On the downloads page, click on the "MinGW-w64-builds" option. Do not click on the "win-builds" option.
    • MinGW-w64 builds option
    • The reason MinGW-w64 has to be used is because it is a newer version of the MinGW compiler suit that has been improved for windows. This means that it supports the posix thread system, where as the standard MinGW compiler only supports the win32 thread system. OpenCV relies on the posix thread system, necessitating the MinGW-w64 compiler.
    • Extract the MinGW-w64 zip folder to a directory. In my case its PortableGit/opt/MinGW-w64
    • At this point, you can add the MingGW-w64/mingw32/bin folder to your path. (Assuming that this won't cause any conflicts.) If you do so, you will not have to constantly specify the g++ executable directory to run it. This is up to your discretion.
  • Download an opencv release.
    • Do not download the package for windows, click the button that says "sources"
    • Sources, not the windows package
    • Extract the opencv sources zip folder to a directory. In my case its PortableGit/opt/opencv-4.3.0
    • Also download the opencv_contrib source files directly from the repository.
    • Extract that folder and place it inside the top level opencv folder: PortableGit/opt/opencv-4.3.0/opencv_contrib in my case.
  • Download CMake.
    • I downloaded the zip folder, but you can download the installer if you wish.
    • Extract the CMake zip folder if you downloaded that, or run the installer. I put my CMake folder here: PortableGit/opt/cmake-3.17.1-win32-x86
    • At this point, you can add the cmake-3.17.1-win32-x86/bin folder to your path. (Assuming that this won't cause any conflicts.) If you do so, you will not have to constantly specify the cmake executable directory to run it. This is up to your discretion.

Part 2: Build OpenCV

  • Navigate to the opencv directory and create a build folder and cd into it.
    • mkdir build && cd build
  • Run the following export commands.
    • export CC=/PortableGit/MinGW-w64/mingw32/bin/gcc.exe
    • export CXX=/PortableGit/MinGW-w64/mingw32/bin/g++.exe
    • This is to make sure the next cmake command uses the proper compilers.
  • Run the following cmake command from within that folder: PortableGit/opt/cmake-3.17.1-win32-x86/cmake.exe -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DOPENCV_VS_VERSIONINFO_SKIP=1 -DOPENCV_EXTRA_MODULES_PATH="/PortableGit/opt/opencv-4.3.0/opencv_contrib/modules/" ..
    • The -G flag specifies that we are creating build files for the MinGW compiler
    • The -DCMAKE_BUILD_TYPE=Release specifies that we are building the release version of opencv and not the debug version.
    • The DOPENCV_EXTRA_MODULES_PATH needs to be set to the modules folder inside the opencv_contrib folder. For me it was PortableGit/opt/opencv-4.3.0/opencv_contrib/modules
    • The DOPENCV_VS_VERSIONINFO_SKIP specifies to not include version info. If not set, the compiler will throw an error complaining about not having version files. (Shown below for reference.)
gcc: error: long: No such file or directory
mingw32-make[2]: *** [modules\core\CMakeFiles\opencv_core.dir\build.make:1341:
modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1
  • If successful, the cmake command will finish like this:
    • Successfull build
  • Now run this command, again from the build folder: /PortableGit/opt/MinGW-w64/mingw32/bin/mingw32-make.exe -j7
    • mingw32-make.exe is the windows equivalent of the Linux make command.
    • The -j7 option run the process with a maximum of 7 threads.
    • This will take a while! It took my laptop ~20 minutes to complete
    • If the make command ends in an error, make sure to reset your build directory before continuing any troubleshooting. This is done through this series of commands
rm -rf build
mkdir build
cd build

Part 3: Using OpenCV

  • To use the opencv library that you just compiled in a project of your own, compile the project with these flags from your projects main directory.
  • Remember that your compiler now has to be set to the mingw-w64 compiler for opencv support.
  • I added indentation and newlines for readability, but when entering this in the terminal do not include the newlines or indents.
  • The number at the end of the linker options may change depending on the version of opencv you downloaded. I downloaded opencv-4.3.0, making my number 430, but yours may be different.
PortableGit/opt/MinGW-w64/bin/g++.exe
    -I../../PortableGit/opt/opencv-4.3.0/include/
    -I../../PortableGit/opt/opencv-4.3.0/build/
    -I../../PortableGit/opt/opencv-4.3.0/modules/core/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/calib3d/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/dnn/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/features2d/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/flann/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/gapi/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/highgui/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/imgcodecs/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/imgproc/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/ml/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/objdetect/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/photo/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/stitching/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/ts/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/video/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/videoio/include/
    -I../../PortableGit/opt/opencv-4.3.0/modules/world/include/
    -L../../PortableGit/opt/opencv-4.3.0/build/lib/
    *.hpp
    *.cpp
    -lopencv_calib3d430
    -lopencv_core430
    -lopencv_dnn430
    -lopencv_features2d430
    -lopencv_flann430
    -lopencv_highgui430
    -lopencv_imgcodecs430
    -lopencv_imgproc430
    -lopencv_ml430
    -lopencv_objdetect430
    -lopencv_photo430
    -lopencv_stitching430
    -lopencv_video430
    -lopencv_videoio430
    -o
    main

Or you could download VS. Up to you. Hope this helps.

Upvotes: 2

Related Questions