How to set-up OpenCV in Intellij IDEA

Aadam
3 min readFeb 5, 2016

After searching for about an hour on how to set up OpenCV in IntelliJ IDEA, and not finding a good solution for beginners, I’ve decided to post it myself. I am not an expert, clearly, just started learning Java and have no experience with IntelliJ IDEA. So, there might be a better solution than this but I didn’t find one.

I assume that you have already downloaded and installed OpenCV and IntelliJ IDEA. Create a new project.

Go to File > Project Structure.

File > Project Structure
Modules > Dependencies > Add

Select Modules from the side panel and the slect the Dependencies tab. Click the + icon on the right to add a dependency. Then select the Add Jar/Directory option.

Navigate to jar

Then browse to the path where you installed the OpenCV and select build/java/opencv-***.jar and click OK.

It will be shown as dependency in the window. Now, we also have to add the Native Library Location. To do that, double click on the opencv-310.jar (currently selcted in the image above).

Then click on the + icon to add the Native Library Location.

opencv/build/java/x64

And then browse to the locatoin where you installed OpenCV and select build/java/x64. You should select x64 or x86 based on your system specification. Now click OK.

This will add Native Library Location to the dependency. Now click OK and apply the changes.

Here is a test to check whether your setup worked or not.

Remeber to include the highlighted line of code to load the library, otherwise it will not compile.

Here is the code.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.core.Scalar;

public class Test {
// Compulsory
static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

public static void main(String[] args) {
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());
}
}

Let me know if you liked it and more importantly, if you know a better way to do this.

--

--

Aadam

I am a passionate individual with a zest for knowledge which drives me to learn about new concepts and technologies.