可以使用imgproc
类的resize()
方法对图像执行缩放。 以下是此方法的语法。
Imgproc.resize(Mat src, Mat dst, Size dsize, double fx, double fy, int interpolation)
该方法接受以下参数 -
Mat
对象。Mat
对象。Size
对象,表示输出图像的大小。double
类型的变量表示横轴上的比例因子。double
类型的变量表示垂直轴上的比例因子。示例
以下程序演示如何缩放图像。
package com.zyiz.geometric; 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 ="F:/worksp/opencv/images/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("F:/worksp/opencv/images/scale_output.jpg", dst); System.out.println("Image Processed"); } }
假定以下是上述程序中指定的输入图像:transform_input.jpg
。
执行上面示例代码,得到以下结果 -