How to use OpenCV with IntelliJ IDEA 12

I think things have changed a little since the previous answers were posted and I have tried them right now (opencv 2.4.9) and wanted to add a few things:

From the beginning:

first execute from terminal cmake -DBUILD_SHARED_LIBS=OFF opencv-2.4.9/ from the folder "above" the open-cv document that you have just extracted, then execute make -j8, this might take a while..

Now, in Intellij go to File | Project Structure, and choose Global Libraries, and add the open-cv jar that is located under opencv/bin.

In that point, if you'll try to run one of the examples you'll probably get something like Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java249 in java.library.path

Next, as dlx.folmead1 suggested above, go to Run | Edit Configuration, and add to VM options -Djava.library.path=/absolute-path-to/opencv/lib

Of course, it is always a good idea to take a look at open-cv's documentation about java and open-cv


First install Chocolatey via PowerShell by The Following command

Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iwr https://community.chocolatey.org/install.ps1 -UseBasicParsing | iex

After chocolatey installation

choco install opencv

The Above command will install the opencv latest version available

You will need to manually create an OPENCV_DIR environment variable then add %OPENCV_DIR%\bin to your PATH.Environment setup AFter this just add The following Maven dependencies

 <dependencies>
    <dependency>
        <groupId>org.openpnp</groupId>
        <artifactId>opencv</artifactId>
        <version>4.5.1-2</version>
    </dependency>
</dependencies>

Then add this piece of code ;

 OpenCV.loadLocally();

and boom it will work hunky dory!!!!

Bellow code snipet is to test .

 OpenCV.loadLocally();
    System.out.println("Welcome to OpenCV " + Core.VERSION);

    Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
    System.out.println("OpenCV Mat: " + m);
    Mat mr1 = m.row(1);
    mr1.setTo(new Scalar(1));
    Mat mc5 = m.col(5);
    mc5.setTo(new Scalar(5));
    System.out.println("OpenCV Mat data:\n" + m.dump());

In order to use native libraries in Java you need to specify java.library.path system property, so that JVM knows where to look for them.

In IntelliJ this be can be done in Run/Debug Configuration -> Application -> VM options, enter:

-Djava.library.path=path/to/dll

  • Download OpenCV-2.4.5-android-sdk.zip from OpenCV site
  • Extract to where ever as OpenCV-2.4.5-android-sdk, mine happened to be

    /home/anthony/Documents/OpenCV-2.4.5-android-sdk/

  • Open IntelliJ and choose Import

  • Select the folder to import

    /home/anthony/Documents/OpenCV-2.4.5-android-sdk/sdk/java/

     yours will be a little different, 
     don't worry, just chose where you 
     extracted OpenCV-2.4.5-android-sdk
    
  • Once the Import wizard finishes, build the app with menu

Build -> Rebuild Project

  • Close Project

  • Create New or Open existing project
  • Then

File->Import Module

  • This time select

/home/anthony/Documents/OpenCV-2.4.5-android-sdk/sdk/java/XXX.iml

mine was sdk.iml, but yours could be anything but there will be only one

iml file Select iml file to import module

You can now start using OpenCV functions, start by typing

import org.

once you type the period IntelliJ should drop a list of options one of which is

opencv

Now OpenCV is correctly integrated in your IDE

The rest is up to you.