1

In the python way, loading and displaying images is quite straightforward.

import cv2 




image = cv2.imread("path/to/image")
cv2.imshow("test window", image)
cv2.waitKey(0) # any key press will close the window and stop the program

In java, it looks like this.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.highgui.HighGui;


public class HelloWorld {


  static {
        System.load("/Users/ongyichong/opencv/libopencv_java401.dylib");
    }


  public static void main(String[] args) {

    System.out.println("hello world");

    Mat testFile  = Imgcodecs.imread("/Users/ongyichong/SikuliX/Scripts/NUSivle/0.png");
    HighGui.imshow("test window", testFile);
    HighGui.waitKey(0);
  }

}

Im using opencv 4 and I have not been successful in implementing the same functionality in java's opencv.

HighGui.waitKey(0) does not work as expected compared to python ( Any key press will close the window in python ) and I have to manually close "test window" to cause the programme to stop.

I have read somewhere that says that opencv in java is unable to display images. However doesnt HighGui provides this functionality ? I read all the API's from the java opencv api

The api docs are not as detailed in helping me understand what exactly the function does and i can only guess what it does from python.

calveeen
  • 621
  • 2
  • 10
  • 28

2 Answers2

0

Have you tried explicitly calling:

   HighGui.destroyAllWindows();
marketmi
  • 28
  • 3
0

add System.exit(0); after waitkey().

Guagua
  • 1