Skip to content
Related Articles

Related Articles

Scaling Images using OpenCV in Java
  • Last Updated : 08 Dec, 2020

Scaling of Image refers to the resizing of images. It is useful in image processing and manipulation in machine learning applications as it can reduce the time of training as less number of pixels, less is the complexity of the model. Imgproc module of OpenCV library provides an adequate interpolation method for resizing an image.

Imgproc.resize Method of Imgproc module of OpenCV can be used to resize the image. This function resizes the image src down to or up to the specified size. It provides all the features to resize the image i.e. either shrink it or zoom it to meet the size requirements.

Syntax:

resize(
                src: &Mat, 
                dst: &mut Mat, 
                  dsize: Size, 
                     fx: f64, 
                     fy: f64, 
              interpolation: i32
    )

Parameters: 

NameDefinition 
srcIt is the input image of the MAT type.
dstIt is the output image of MAT type
dsizeIt is the size of the output image of Size type
fxScale factor along X-axis of double type
 fyScale factor along Y-axis of double type
interpolation

It is the method for image decimation. Default interpolation is INTER_LINEAR interpolation



Algorithm:

  1. Load OpenCV modules.
  2. Read the image(2D matrix) from the directory.
  3. Store image/2D matrix as a Mat object.
  4. Create an image/2D matrix using the object.
  5. Scale and write using the above object created.

Samples:

Suppose the user wants to resize src so that it fits the pre-created destination file, then the syntax for calling the function is as follows

resize(src, dst, dst.size(), 0, 0, interpolation);

Suppose the user wants to reduce the image by a factor of 2 in each direction, then the syntax for calling the function is as follows

resize(src, dst, Size(), 0.5, 0.5, interpolation);

Implementation: Considering the below image as an input image that is supposed to be scaled producing a new output image. Here the input image is supposed to be scaled down which is illustrated with help of an example.

Input Image

Java




// Importing openCV modules
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
  
public class GFG {
  
    // Main driver code
    public static void main(String args[])
    {
  
        // Load OpenCV library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  
        // Reading the Image from the file/ directory
        String image_location
            = "C:\\Users\\user\\Downloads\\InputImage.jpg";
  
        // Storing the image in a Matrix object
        // of Mat type
        Mat src = Imgcodecs.imread(image_location);
  
        // New matrix to store the final image
        // where the input image is supposed to be written
        Mat dst = new Mat();
  
        // Scaling the Image using Resize function
        Imgproc.resize(src, dst, new Size(0, 0), 0.1, 0.1,
                       Imgproc.INTER_AREA);
  
        // Writing the image from src to destination
        Imgcodecs.imwrite("D:/resized_image.jpg", dst);
  
        // Display message to show that
        // image has been scaled
        System.out.println("Image Processed");
    }
}

Output:

Final Image

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :