Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava Program to Draw Geometric Shapes on Images in OpenCV

Java Program to Draw Geometric Shapes on Images in OpenCV

The OpenCV library in Java contains a class named Imgproc which provides various methods such as resize(), wrapAffine( ), filter2D to process an input image. We will use the javax.swing package here. The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser, etc. Concepts of java involved here are Swing class and abstract windows toolkit as mentioned below:

Swing Class: Here I will explain all the classes of the javax.swing package which will be used in the program later:

  • javax.swing.ImageIcon : The class ImageIcon is an implementation of the Icon interface that paints Icons from Images.
  • javax.swing.Jframe : The class JFrame is a type of container which inherits the java. JFrame works like the main window where components like labels, buttons, textfields are added to create a GUI
  • javax.swing.JLabel : The class JLabel is used to display a short string or an image icon.
  • javax.swing.JMenu : The class JMenu is used to pull down menu  component which is displayed from the menubar.
  • javax.swing.JMenuBar : The class JMenuBar is used to display menubar on the window or frame. It may have several menus.
  • javax.swing.JMenuItem : The JMenuItem class represents the actual item in a menu. All items in a menu should derive from class JMenuItem, or one of its subclasses.

We also need to use the Abstract Window Toolkit (AWT) in the program. It is part of the Java Foundation Classes (JFC). I will briefly describe AWT here. 

The Abstract Window Toolkit (AWT) is a Java package used for creating graphical user interfaces. AWT features include:

  • A set of native interface components
  • A robust event-handling model
  • Graphics and imaging tools, including shape, color, and font classes
  • Layout managers, for flexible window layouts that do not depend on a particular window size or screen resolution
  • Data transfer classes, for cut and paste through the native platform clipboard

Procedure: Steps to draw geometric shapes on images in OpenCV

  1. Create a project and add OpenCV library
  2. Create Package
  3. Create a Class
  4. Create a folder named “images” and put a .jpg or .png file in it.
  5. Write the following code in the java file.

Sample Input Image:

Step 1: Create a project and add OpenCV library

  • Click on File> New > Java Project.
  • Give a name to your project and checkmark the necessary options under the JRE and Project Layout options as shown below in the figure. Here, I have named the project OpenCVShape.
  • After marking the options click on Next.

  • After that go to the Libraries option in the same dialogue box. Click on classpath -> Add external jars.
  • Add the opencv.jar file from your local machine or can be download opencv.jar file.

  • After adding the jar file, expand the Classpath option, and click on Native library location: (None), and click on Edit.

  • Select External Folder… and browse to select the folder containing the OpenCV libraries (e.g., C:\opencv\build\java\x64 under Windows). Click on Finish.

Step 2: Create Package

  • Right-click on src. Go to New → Package.
  • A dialog box will appear. Type the name of the package as com.pkg and click on Finish.

Step 3: Create a Class

  • Right-click on com.pkg. Go to new → Class.
  • A dialog box will appear. Type the name of the class. I have named it OpenCVShape.
  • Checkmark the necessary options and click on Finish.

Step 4: Create a folder named “images” and put a .jpg or .png file in it.

  • Rightclick on the name of your project (in this case OpenCVShape). Go to New -> Folder. Type the name of the folder (images) and click OK.
  • Put an image with extension .jpg or .png in this folder.

Step 5: Write the following code in the java file

Implementation: The above steps are computed to get different geometric shapes as shown in examples below: 

  1. Line
  2. Rectangles
  3. Circles
  4. Ellipses

Example 1: Drawing Lines 

Java




// Java Program using openCV to draw lines
  
package com.pkg;
  
// Importing abstract windows toolkit classes
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
  
// Importing Swing classes 
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
  
// Importing OpenCV modules
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
  
public class OpenCVShapes extends JFrame {
  
    Mat image;
    Mat tempImage;
  
    JLabel imageView;
  
    // Menu for save image
    private JMenuBar mb;
    private JMenu menu;
    private JMenuItem saveMenuItem;
  
    private Point originPoint;
    public OpenCVShapes()
    {
        // Loading image from local directory 
        image = Imgcodecs.imread("images/sample_image.png");
  
        // Method to view setup
        setUpView();
  
        // Loading image to jlabel
        loadImage(image);
  
        // Setting iframe property
        setSize(image.width(), image.height());
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
  
  // Method to load image 
    private void loadImage(Mat img)
    {
        final MatOfByte mof = new MatOfByte();
        Imgcodecs.imencode(".png", img, mof);
  
        final byte[] imageData = mof.toArray();
  
        // Change image byte to image icon
        final ImageIcon icon = new ImageIcon(imageData);
          
      // Add icon to jlabel
        imageView.setIcon(icon);
    }
  
    private void setUpView()
    {
        setLayout(null);
  
        imageView = newJLabel();
        imageView.setBounds(0, 20, image.width(),
                            image.height());
  
        // Add mouse listener
        imageView.addMouseListener(new MouseAdapter() {
            @Override public void mousePressed(MouseEvent e)
            {
                super.mousePressed(e);
                
                // Storing location of mouse pressed
                originPoint = new Point(e.getX(), e.getY());
            }
  
            @Override
            public void mouseReleased(MouseEvent e)
            {
                super.mouseReleased(e);
                // when mouse release replace tempimage to
                // image
                image = tempImage.clone();
            }
        });
  
        // Adding another event mousemotionlistener
        imageView.addMouseMotionListener(
            new MouseMotionListener() {
                
                @Override
                public void mouseMoved(MouseEvent e)
                {
                    // TODO Auto-generate method stub
                }
  
                @Override
                public void mouseDragged(MouseEvent e)
                {
                    // Create temp image for drawing
                    tempImage = image.clone();
                    final Point point
                        = new Point(e.getX(), e.getY());
  
                    // NOW, DRAWING SHAPES
  
                    // 1. Drawing line
  
                  // Color in bgr format
                    Imgproc.line(
                        tempImage, originPoint, point,
                        new Scalar(0, 0, 0),
                        5); 
  
                    loadImage(tempImage);
                }
            });
  
        add(imageView);
  
        // Adding menu
        mb = new JMenuBar();
        menu = new JMenu("file");
        saveMenuItem = new JMenuItem("save");
        menu.add(saveMenuItem);
        mb.add(menu);
  
        mb.setBounds(0, 0, image.width(), 20);
        add(mb);
  
        saveMenuItem.addActionListener(
            new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    // TODO Auto-generated method stub
                    Imgcodecs.imwrite("images/ind1.png",
                                      image);
                }
            });
    }
  
    private JLabel newJLabel()
    {
        // TODO Auto-generated method stub
        return null;
    }
  
  // Main driver method
    public static void main(String[] args)
    {
        // Loading library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        EventQueue.invokeLater(new Runnable() {
            @Override public void run()
            {
                new OpenCVShapes();
            }
        });
    }
}


 
 

Output:

 

 

Example 2: Drawing rectangles

 

Java




// Java Program using openCV to draw rectangles
  
package com.pkg;
  
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
  
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
  
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
  
public class OpenCVShapes extends JFrame {
  
    Mat image;
    Mat tempImage;
  
    JLabel imageView;
  
    // menu for save image
    private JMenuBar mb;
    private JMenu menu;
    private JMenuItem saveMenuItem;
  
    private Point originPoint;
    public OpenCVShapes()
    {
        // load image
        image = Imgcodecs.imread("images/sample_image.png");
  
        // view setup
        setUpView();
  
        // load image to jlabel
        loadImage(image);
  
        // set iframe property
        setSize(image.width(), image.height());
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
  
    private void loadImage(Mat img)
    {
        final MatOfByte mof = new MatOfByte();
        Imgcodecs.imencode(".png", img, mof);
  
        final byte[] imageData = mof.toArray();
  
        // change image byte to image icon
        final ImageIcon icon = new ImageIcon(imageData);
        // add icon to jlabel
        imageView.setIcon(icon);
    }
  
    private void setUpView()
    {
        setLayout(null);
  
        imageView = newJLabel();
        imageView.setBounds(0, 20, image.width(),
                            image.height());
  
        // add mouse listener
        imageView.addMouseListener(new MouseAdapter() {
            @Override public void mousePressed(MouseEvent e)
            {
                super.mousePressed(e);
                // store location of mouse pressed
                originPoint = new Point(e.getX(), e.getY());
            }
  
            @Override
            public void mouseReleased(MouseEvent e)
            {
                super.mouseReleased(e);
                // when mouse release replace tempimage to
                // image
                image = tempImage.clone();
            }
        });
  
        // add another event mousemotionlistener
        imageView.addMouseMotionListener(
            new MouseMotionListener() {
                @Override
                public void mouseMoved(MouseEvent e)
                {
                    // TODO Auto-generate method stub
                }
  
                @Override
                public void mouseDragged(MouseEvent e)
                {
                    // create temp image for drawing
                    tempImage = image.clone();
                    final Point point
                        = new Point(e.getX(), e.getY());
  
                    // here we will draw shapes
  
                    // draw rectangle
                    Imgproc.rectangle(
                        tempImage, originPoint, point,
                        new Scalar(255, 0, 0), 5);
  
                    loadImage(tempImage);
                }
            });
  
        add(imageView);
  
        // add menu
        mb = new JMenuBar();
        menu = new JMenu("file");
        saveMenuItem = new JMenuItem("save");
        menu.add(saveMenuItem);
        mb.add(menu);
  
        mb.setBounds(0, 0, image.width(), 20);
        add(mb);
  
        saveMenuItem.addActionListener(
            new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    // TODO Auto-generated method stub
                    Imgcodecs.imwrite("images/ind1.png",
                                      image);
                }
            });
    }
  
    private JLabel newJLabel()
    {
        // TODO Auto-generated method stub
        return null;
    }
  
  // main driver method
    public static void main(String[] args)
    {
        // Loading library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        EventQueue.invokeLater(new Runnable() {
            @Override public void run()
            {
                new OpenCVShapes();
            }
        });
    }
}


 
 

Output:

 

 

Example 3: Drawing circles

 

Java




// Java Program using openCV to draw circles
  
package com.pkg;
  
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
  
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
  
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
  
public class OpenCVShapes extends JFrame {
  
    Mat image;
    Mat tempImage;
  
    JLabel imageView;
  
    // menu for save image
    private JMenuBar mb;
    private JMenu menu;
    private JMenuItem saveMenuItem;
  
    private Point originPoint;
    public OpenCVShapes()
    {
        // load image
        image = Imgcodecs.imread("images/sample_image.png");
  
        // view setup
        setUpView();
  
        // load image to jlabel
        loadImage(image);
  
        // set iframe property
        setSize(image.width(), image.height());
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
  
    private void loadImage(Mat img)
    {
        final MatOfByte mof = new MatOfByte();
        Imgcodecs.imencode(".png", img, mof);
  
        final byte[] imageData = mof.toArray();
  
        // change image byte to image icon
        final ImageIcon icon = new ImageIcon(imageData);
        // add icon to jlabel
        imageView.setIcon(icon);
    }
  
    private void setUpView()
    {
        setLayout(null);
  
        imageView = newJLabel();
        imageView.setBounds(0, 20, image.width(),
                            image.height());
  
        // add mouse listener
        imageView.addMouseListener(new MouseAdapter() {
            @Override public void mousePressed(MouseEvent e)
            {
                super.mousePressed(e);
                // store location of mouse pressed
                originPoint = new Point(e.getX(), e.getY());
            }
  
            @Override
            public void mouseReleased(MouseEvent e)
            {
                super.mouseReleased(e);
                // when mouse release replace tempimage to
                // image
                image = tempImage.clone();
            }
        });
  
        // add another event mousemotionlistener
        imageView.addMouseMotionListener(
            new MouseMotionListener() {
                @Override
                public void mouseMoved(MouseEvent e)
                {
                    // TODO Auto-generate method stub
                }
  
                @Override
                public void mouseDragged(MouseEvent e)
                {
                    // create temp image for drawing
                    tempImage = image.clone();
                    final Point point
                        = new Point(e.getX(), e.getY());
  
                    // here we will draw shapes
  
                    // draw circle
                    // first find distance of origin point
                    // and point
  
                    double ab2
                        = Math.pow(originPoint.x - point.x,
                                   2)
                          + Math.pow(
                                originPoint.y - point.y, 2);
                    int distance = (int)Math.sqrt(ab2);
  
                    Imgproc.circle(
                        tempImage, originPoint, distance,
                        new Scalar(0, 255, 0), 5);
  
                    loadImage(tempImage);
                }
            });
  
        add(imageView);
  
        // add menu
        mb = new JMenuBar();
        menu = new JMenu("file");
        saveMenuItem = new JMenuItem("save");
        menu.add(saveMenuItem);
        mb.add(menu);
  
        mb.setBounds(0, 0, image.width(), 20);
        add(mb);
  
        saveMenuItem.addActionListener(
            new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    // TODO Auto-generated method stub
                    Imgcodecs.imwrite("images/ind1.png",
                                      image);
                }
            });
    }
  
    private JLabel newJLabel()
    {
        // TODO Auto-generated method stub
        return null;
    }
  
    
  // Main driver method
    public static void main(String[] args)
    {
        // Loading library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        EventQueue.invokeLater(new Runnable() {
            @Override public void run()
            {
                new OpenCVShapes();
            }
        });
    }
}


 
 

Output:

 

 

Example 4: Drawing ellipses

 

Java




// Java Program using openCV to draw ellipses
  
package com.pkg;
  
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
  
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
  
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
  
public class OpenCVShapes extends JFrame {
  
    Mat image;
    Mat tempImage;
  
    JLabel imageView;
  
    // menu for save image
    private JMenuBar mb;
    private JMenu menu;
    private JMenuItem saveMenuItem;
  
    private Point originPoint;
    public OpenCVShapes()
    {
        // load image
        image = Imgcodecs.imread("images/sample_image.png");
  
        // view setup
        setUpView();
  
        // load image to jlabel
        loadImage(image);
  
        // set iframe property
        setSize(image.width(), image.height());
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
  
    private void loadImage(Mat img)
    {
        final MatOfByte mof = new MatOfByte();
        Imgcodecs.imencode(".png", img, mof);
  
        final byte[] imageData = mof.toArray();
  
        // change image byte to image icon
        final ImageIcon icon = new ImageIcon(imageData);
        // add icon to jlabel
        imageView.setIcon(icon);
    }
  
    private void setUpView()
    {
        setLayout(null);
  
        imageView = newJLabel();
        imageView.setBounds(0, 20, image.width(),
                            image.height());
  
        // add mouse listener
        imageView.addMouseListener(new MouseAdapter() {
            @Override public void mousePressed(MouseEvent e)
            {
                super.mousePressed(e);
                // store location of mouse pressed
                originPoint = new Point(e.getX(), e.getY());
            }
  
            @Override
            public void mouseReleased(MouseEvent e)
            {
                super.mouseReleased(e);
                // when mouse release replace tempimage to
                // image
                image = tempImage.clone();
            }
        });
  
        // add another event mousemotionlistener
        imageView.addMouseMotionListener(
            new MouseMotionListener() {
                @Override
                public void mouseMoved(MouseEvent e)
                {
                    // TODO Auto-generate method stub
                }
  
                @Override
                public void mouseDragged(MouseEvent e)
                {
                    // create temp image for drawing
                    tempImage = image.clone();
                    final Point point
                        = new Point(e.getX(), e.getY());
  
                    // here we will draw shapes
  
                    // draw eclipse
                    double x
                        = Math.abs(point.x - originPoint.x);
                    double y
                        = Math.abs(point.y - originPoint.y);
                    Size size = new Size(x * 2, y * 2);
  
                    Imgproc.ellipse(
                        tempImage,
                        new RotateRect(originPoint, size,
                                       0),
                        new Scalar(255, 255, 0), 5);
  
                    loadImage(tempImage);
                }
            });
  
        add(imageView);
  
        // add menu
        mb = new JMenuBar();
        menu = new JMenu("file");
        saveMenuItem = new JMenuItem("save");
        menu.add(saveMenuItem);
        mb.add(menu);
  
        mb.setBounds(0, 0, image.width(), 20);
        add(mb);
  
        saveMenuItem.addActionListener(
            new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    // TODO Auto-generated method stub
                    Imgcodecs.imwrite("images/ind1.png",
                                      image);
                }
            });
    }
  
    private JLabel newJLabel()
    {
        // TODO Auto-generated method stub
        return null;
    }
  
  // Main driver method
    public static void main(String[] args)
    {
        // Loading library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        EventQueue.invokeLater(new Runnable() {
            @Override public void run()
            {
                new OpenCVShapes();
            }
        });
    }
}


 
 

Output: 

 

 

RELATED ARTICLES

Most Popular

Recent Comments