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
- Create a project and add OpenCV library
- Create Package
- Create a Class
- Create a folder named “images” and put a .jpg or .png file in it.
- 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.
- Right–click 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:
Example 1: Drawing Lines
Example 2: Drawing rectangles
Example 3: Drawing circles
Example 4: Drawing ellipses
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.