Using OpenCV Java Bindings to read an image

for macOs you can use :

import org.opencv.imgcodecs.Imgcodecs;


Mat logo = Imgcodecs.imread("resources/images/gambar.jpg");

for me, I use opencv_java320.dll


If you have correctly installed Opencv with support for Java desktop and included opencv-2.4.4.jar , your should import:

import org.opencv.imgproc.Imgproc;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;

And your code will look like this:

Mat img = Highgui.imread(argv[1], Highgui.CV_LOAD_IMAGE_GRAYSCALE);
int erosion_size = 5;
Mat element  = Imgproc.getStructuringElement(
    Imgproc.MORPH_CROSS, new Size(2 * erosion_size + 1, 2 * erosion_size + 1), 
    new Point(erosion_size, erosion_size)
);
Imgproc.erode(img, img, element);

To read an image with OpenCV for Java:

OpenCV 2.x (JavaDoc)

Mat img = Highgui.imread("path/to/img");

OpenCV 3.x (JavaDoc)

Mat img = Imgcodecs.imread("path/to/img");

In both versions you can pass a second parameter specifying how to load the image:

  • CV_LOAD_IMAGE_ANYDEPTH: returns 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
  • CV_LOAD_IMAGE_COLOR: always convert image to a color one.
  • CV_LOAD_IMAGE_GRAYSCALE: always convert image to a grayscale one.

Example:

// OpenCV 2.x   
Mat img = Highgui.imread("path/to/img", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
// OpenCV 3.x
Mat img = Imgcodecs.imread("path/to/img", Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);