Skip to content
Related Articles

Related Articles

Java Program to Copy and Paste an image in OpenCV
  • Last Updated : 15 Dec, 2020

OpenCV is a Machine Learning and open-source computer vision software library, the main purpose for which it was developed is to enable a common medium to increase the use of machine perception in commercial businesses and accelerate the development of computer vision applications, it is a smooth and easy transition for businesses to adopt and use OpenCV as it has the advantage of being BSD-licensed product.

  1. The library of OpenCV has 2000+ algorithms that are efficiently optimized and which also contains state-of-art computer vision and classical Machine Learning Algorithms.
  2. These Algorithms were mainly used for performing various tasks like recognizing faces, identification of objects, classification of human activities in videos, extraction of 3D models of objects, extraction of higher resolution in images, finding similar images from a picture database… etc.
  3. OpenCV also contains various language interfaces such as Java, Python, C++, MATLAB and it extensively supports Linux, Android, Windows, macOS. In other words, it supports almost all popular existing Operating Systems thus enabling a large Audience in its user’s list.

Input Image: Sample image present at the local directory in the system at the local directory. Here the directory from which the image is extracted to interpret as the multidimensional array is shown below:

Local directory from where below sample image is copied: “C:/opencv/gfgarticleimg.png”

Now, as we got to know what is OpenCV, let us try to build a java program that enables us in copying and pasting an image with OpenCV.



Algorithm:

  1. Import OpenCV modules and load core libraries.
  2. Read the image from the local directory and store it in the Matrix object.
  3. Interpret image as a multi-dimensional matrix.
  4. Store this multi-dimensional structure writing to some different local directory as specified from where it is extracted to be. copied.

Example: 

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Copy and Paste an image in OpenCV 
  
// Importing all input output java classes
import java.io.*;
// Importing OpenCV modules
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.core.Mat;
import org.opencv.core.Core;
  
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Loading OpenCV core library
        System.loadLibrary(Core.Native_Library_Name);
  
        // Read image from file and
        // Store it in a Matrix object
        String f = "C:/opencv/gfgarticleimg.png";
  
        // Creation of a Matrix object
        Mat m = Imgcodecs.imread(f);
  
        // Display message
        System.out.println(
            "Your Image has been Loaded.......");
  
        // Take another file for generating output image
        String f2 = "C:/opencv/gfgarticleimgResaved.png";
  
        // Write the image
        Imgcodecs.imwrite(f2, m);
  
        // Display message
        System.out.println(
            "congrats! your image has been saved........");
    }
}

chevron_right


Output :

Local directory to where above sample shown below is pasted: “C:/opencv/gfgarticleimgResaved.png”


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :