You can perform scaling on an image using the resize() method of the imgproc class. Following is the syntax of this method.
resize(Mat src, Mat dst, Size dsize, double fx, double fy, int interpolation)
This method accepts the following parameters −
src − A Mat object representing the source (input image) for this operation.
dst − A Mat object representing the destination (output image) for this operation.
dsize − A Size object representing the size of the output image.
fx − A variable of the type double representing the scale factor along the horizontal axis.
fy − A variable of the type double representing the scale factor along the vertical axis.
Interpolation − An integer variable representing interpolation method.
The following program demonstrates how to apply scale transformation to an image.
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 Scaling { public static void main(String args[]) { // Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); // Reading the Image from the file and storing it in to a Matrix object String file ="E:/OpenCV/chap24/transform_input.jpg"; Mat src = Imgcodecs.imread(file); // Creating an empty matrix to store the result Mat dst = new Mat(); // Creating the Size object Size size = new Size(src.rows()*2, src.rows()*2); // Scaling the Image Imgproc.resize(src, dst, size, 0, 0, Imgproc.INTER_AREA); // Writing the image Imgcodecs.imwrite("E:/OpenCV/chap24/scale_output.jpg", dst); System.out.println("Image Processed"); } }
Assume that following is the input image transform_input.jpg specified in the above program (size − Width:300px and height:300px).
On executing the program, you will get the following output −
Image Processed
If you open the specified path, you can observe the output image as follows (size − Width:600px and height:600px) −