OpenCV - 縮放



您可以使用 imgproc 類的 resize() 方法對影像執行縮放操作。以下是此方法的語法。

resize(Mat src, Mat dst, Size dsize, double fx, double fy, int interpolation)

此方法接受以下引數:

  • src - 表示此操作的源(輸入影像)的 Mat 物件。

  • dst - 表示此操作的目標(輸出影像)的 Mat 物件。

  • dsize - 表示輸出影像大小的 Size 物件。

  • fx - 表示水平軸縮放因子的雙精度型別變數。

  • fy - 表示垂直軸縮放因子的雙精度型別變數。

  • Interpolation - 表示插值方法的整型變數。

示例

以下程式演示瞭如何將縮放變換應用於影像。

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");
   }
}

假設以上程式中指定的輸入影像為 transform_input.jpg(大小 - 寬度:300px,高度:300px)。

Transform Input

輸出

執行程式後,您將獲得以下輸出:

Image Processed

如果您開啟指定的路徑,您可以觀察到輸出影像如下(大小 - 寬度:600px,高度:600px):

Scale Output
廣告

© . All rights reserved.