Search code examples
compilation64-bitmingw-w64

Compiling opencv 3 mingw64 under windows 7 -'bits


Can anyone point me to the procedure to compile Opencv 3.0 with Mingw64 for use in Java.

Can you point me to the opencv 64bits compiler compatible version as I have tried many versions and none works.

TDM-gcc 64bits just crashes at some point for instance.


Solution

  • OpenCV is an open source multi-platform project, developed by and for developers (not end users) and computer vision scientists. Although some pre-compiled binaries are included by default (for Java too), if you have trouble with them you should build opencv by yourself in your particular OS and hardware, with the modules you need.

    This is the way I just compiled OpenCV 3.2 with MinGW (because I don't want to install visual studio) on my Windows machine. These instructions also work if you're building without Java support.

    1. (Optional) Just in case, remove any old OpenCV installation. If you're having major trouble, also remove previous MinGW, Ant, JDK, and CMake installations and make sure your Windows System PATH doesn't point to them... You might want to start from scratch.
    2. Download and install MinGW (latest version). I like to install it in "C:\MinGW". It's better if you make sure you don't have other MinGW installations in your system so you don't have conflicts.
    3. Download and install CMake (latest version, in my case it was beta 3.9.0)
    4. Add mingw's bin path to the Windows system PATH; just in case, here's a basic tutorial on it (in my case I added C:\MinGW\bin).
      • (If you need Java, otherwise you can skip) If you haven't already, download and install the JDK and Ant (I like to restart after I install JDK). Make sure the JAVA_HOME Windows system variable is set to the right place, and that the Windows system PATH includes the route to the "bin" directory of the JDK.
    5. Restart Windows so it recognizes the path changes.
    6. Download the full opencv source code from its github project page (https://github.com/opencv/opencv). This is the latest public version. There's a "Clone or download" button there if you don't know how to use Git.
    7. Extract opencv's source code somewhere easy to find (I like to do it in C:\opencv so that the file README.md in the opencv root folder appears in C:\opencv\README.md).
    8. Open CMake GUI (here's a video on its basic usage).
    9. Set the source code (in my case C:/opencv) and the binaries build route (I like to do it in C:/opencv/build).
    10. Press Configure. Since we're doing this with MinGW select "MinGW Makefiles" and "Use default native compilers" (if you have already installed other MinGW versions, you might want to check your system PATH so that there are no conflicts, or select "specify native compilers" and set the routes manually).
    11. Lots of red stuff, normal on the first pass. For CMake beta 3.9.0 I had to uncheck ENABLE_PRECOMPILED_HEADERS to dodge some weird error but this might change in the future. I like to uncheck BUILD_TESTS and BUILD_PERF_TESTS to make the compilation faster but it's up to your needs.
      • (if you need Java) Since you installed Ant, the variable ANT_EXECUTABLE should have been detected automagically by CMake, otherwise, fix this (probably set te path manually). Also check the new BUILD_FAT_JAVA_LIB that appeared, it makes sure Java wrappers are created.
      • (if you need Java) A bunch of variables with the "JAVA_" prefix should have appeared; review them and set manually if needed. Here's a (not so elegant) example you can use to guide yourself (make sure to use YOUR paths): cmake java vars example
    12. Click the Configure button until no more red. Then click Generate. If everything went well, at the end it should say something like "cvconfig.h is in: C:/opencv/build , Configuring done , Generating done". Now we can finally build.
    13. Open your console (cmd; here's a basic tutorial on it).
    14. Navigate to the build directory (C:/opencv/build) and execute the command mingw32-make -j4 where "-j4" is the number of parallel threads, I recommend as many as your CPU number of cores allow for max speed.
    15. Wait. A lot. It should finish after 100%.
    16. Now execute mingw32-make install .
    17. We're done. Look for your new binaries in a path similar to C:\opencv\build\install\x86\mingw\bin and you probably want to add this to your Windows System PATH.

    Now, you should test if what you build actually works. Open your preferred IDE, and try running a simple opencv helloworld project that shows a simple Mat, like the equivalent of this C/C++ code:

    #include "opencv2/core/core.hpp"
    #include "opencv2/highgui/highgui.hpp"
    using namespace cv;
    
    int main(int argc, char *argv[]) {
        Mat img = Mat::ones(256, 256, CV_8U) * 200;
        imshow("Gray image", img);
        waitKey(0);
        return 0;
    }
    

    (before building the project, remember to set the opencv includes and libs in your IDE, in my case "C:\opencv\build\install\include" and the lib files you need in "C:\opencv\build\install\x86\mingw\lib")

    If something appears on the screen, congrats!