opencv Display Image OpenCV Display Image OpenCV Java

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Basic reading image from java

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
     
//Load native library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
//Mat object used to host the image
Mat imageArray;
//Read image file from vile system
imageArray=Imgcodecs.imread("path/to/image");

If you want to view images you can not use imshow because OpenCV-java does not have this method either. Instead, you can write the following method.

private static BufferedImage ConvertMat2Image(Mat imgContainer{
    MatOfByte byteMatData = new MatOfByte();
    //image formatting
    Imgcodecs.imencode(".jpg", imgContainer,byteMatData);
    // Convert to array
    byte[] byteArray = byteMatData.toArray();
    BufferedImage img= null;
    try {
        InputStream in = new ByteArrayInputStream(byteArray);
        //load image
        img= = ImageIO.read(in);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return img;
}

You can view the result object in the Jframe, Jlabel (jlabel icon) etc.



Got any opencv Question?