OpenCV - Mise à l'échelle

Vous pouvez effectuer scaling sur une image à l'aide du resize() méthode de la imgprocclasse. Voici la syntaxe de cette méthode.

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

Cette méthode accepte les paramètres suivants -

  • src - Un Mat objet représentant la source (image d'entrée) de cette opération.

  • dst - Un Mat objet représentant la destination (image de sortie) de cette opération.

  • dsize - Un Size objet représentant la taille de l'image de sortie.

  • fx - Une variable de type double représentant le facteur d'échelle le long de l'axe horizontal.

  • fy - Une variable de type double représentant le facteur d'échelle le long de l'axe vertical.

  • Interpolation - Une variable entière représentant la méthode d'interpolation.

Exemple

Le programme suivant montre comment postuler scale transformation à une 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");
   }
}

Supposons que ce qui suit est l'image d'entrée transform_input.jpg spécifié dans le programme ci-dessus (taille - Largeur: 300px et hauteur: 300px).

Production

Lors de l'exécution du programme, vous obtiendrez la sortie suivante -

Image Processed

Si vous ouvrez le chemin spécifié, vous pouvez observer l'image de sortie comme suit (taille - Largeur: 600px et hauteur: 600px) -