Is there any alternative for OpenCV imshow() method in Java?

The HighGui class of the org.opencv.highgui package allows you to create and manipulate windows and display them. You can display an image in a window using the imshow() method of this class. This method accepts two parameters−

  • A string variable representing the name of the window.

  • A Mat object representing the contents of an image.

It is recommended to invoke the waitKey() method after imshow().


The following example reads an image, converts it into a grayscale image, detects the edges in it and displays all the three images (original, gray-scale and, edges) in windows using HighGui.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class ImshowExample {
   public static void main(String args[]) {
      //Loading the OpenCV core library
      //Reading the Image from the file
      Mat src = Imgcodecs.imread("D://images//window.jpg");
      HighGui.imshow("Original Image", src);
      //Converting color to gray scale
      Mat gray = new Mat(src.rows(), src.cols(), src.type());
      Imgproc.cvtColor(src, gray, Imgproc.COLOR_RGB2GRAY);
      HighGui.imshow("Gray Scale Image", gray);
      //Applying canny
      Mat dst = new Mat(src.rows(), src.cols(), src.type());
      Imgproc.Canny(gray, dst, 100, 100*3);
      HighGui.imshow("Edges", dst);


On executing, the above program generates three windows as shown below −

Original image −

Grayscale image −

Image with edges highlighted −

Updated on: 13-Apr-2020


Kickstart Your Career

Get certified by completing the course

Get Started